traxxx/assets/js/entities/actions.js

200 lines
4.4 KiB
JavaScript
Raw Normal View History

import { graphql } from '../api';
// import { sitesFragment, releaseFields } from '../fragments';
import { releaseFields } from '../fragments';
import { curateEntity } from '../curate';
import getDateRange from '../get-date-range';
function initEntitiesActions(store, _router) {
async function fetchEntityBySlugAndType({ _commit }, {
entitySlug,
entityType,
limit = 10,
pageNumber = 1,
range = 'latest',
}) {
const { before, after, orderBy } = getDateRange(range);
const { entity, connection: { releases, totalCount } } = await graphql(`
query Entity(
$entitySlug: String!
$entityType: String! = "channel"
$limit: Int = 10,
$offset: Int = 0,
$after: Date = "1900-01-01",
$before: Date = "2100-01-01",
$afterTime: Datetime = "1900-01-01",
$beforeTime: Datetime = "2100-01-01",
$orderBy: [ReleasesOrderBy!]
$exclude: [String!]
) {
entity: entityBySlugAndType(slug: $entitySlug, type: $entityType) {
id
name
slug
url
hasLogo
children: childEntities(
orderBy: [PRIORITY_DESC, NAME_ASC],
) {
id
name
slug
url
type
priority
hasLogo
}
parent {
id
name
slug
type
url
hasLogo
}
}
connection: releasesConnection(
first: $limit
offset: $offset
orderBy: $orderBy
filter: {
entity: {
or: [
{ slug: { equalTo: $entitySlug } },
{ parent: { slug: { equalTo: $entitySlug } } },
{ parent: { parent: { slug: { equalTo: $entitySlug } } } }
]
}
or: [
{
date: {
lessThan: $before,
greaterThan: $after
}
},
{
date: {
isNull: true
},
createdAt: {
lessThan: $beforeTime,
greaterThan: $afterTime,
}
}
]
releasesTagsConnection: {
none: {
tag: {
slug: {
in: $exclude
}
}
}
}
}
) {
releases: nodes {
${releaseFields}
}
totalCount
}
}
`, {
entitySlug,
entityType,
limit,
offset: Math.max(0, (pageNumber - 1)) * limit,
after,
before,
orderBy,
afterTime: store.getters.after,
beforeTime: store.getters.before,
exclude: store.state.ui.filter,
});
return {
entity: curateEntity(entity, null, releases),
totalCount,
};
}
async function fetchEntities({ _commit }, { type, entitySlugs }) {
const { entities } = await graphql(`
query Entities(
$type: String! = "network"
$entitySlugs: [String!] = []
) {
entities(
orderBy: NAME_ASC
filter: {
or: [
{
type: {
equalTo: $type
}
}
{
slug: {
in: $entitySlugs
}
}
]
}
) {
id
name
slug
type
url
hasLogo
}
}
`, {
type,
entitySlugs,
});
return entities.map(entity => curateEntity(entity));
}
async function searchEntities({ _commit }, { query, limit = 20 }) {
const { entities } = await graphql(`
query SearchEntities(
$query: String!
$limit:Int = 20,
) {
entities: searchEntities(
search: $query,
first: $limit
) {
name
slug
type
url
hasLogo
parent {
name
slug
type
url
hasLogo
}
}
}
`, {
query,
limit,
});
return entities.map(entity => curateEntity(entity));
}
return {
fetchEntityBySlugAndType,
fetchEntities,
searchEntities,
};
}
export default initEntitiesActions;