traxxx/assets/js/entities/actions.js

266 lines
5.4 KiB
JavaScript
Raw Normal View History

import { graphql } from '../api';
// import { sitesFragment, releaseFields } from '../fragments';
2023-06-15 22:47:19 +00:00
import { releaseFields, batchFragment, campaignsFragment } from '../fragments';
import { curateEntity } from '../curate';
import getDateRange from '../get-date-range';
2020-09-10 01:17:19 +00:00
function initEntitiesActions(store, router) {
async function fetchEntityBySlugAndType({ _commit }, {
entitySlug,
entityType,
limit = 10,
pageNumber = 1,
range = 'latest',
}) {
const { before, after, orderBy } = getDateRange(range);
2023-06-18 00:29:10 +00:00
const { entity, connection, batches: [lastBatch] } = await graphql(`
query Entity(
$entitySlug: String!
$entityType: String! = "channel"
$limit: Int = 10,
$offset: Int = 0,
$after: Datetime = "1900-01-01",
$before: Datetime = "2100-01-01",
$orderBy: [ReleasesOrderBy!]
$exclude: [String!]
$hasAuth: Boolean!
$userId: Int
) {
entity: entityBySlugAndType(slug: $entitySlug, type: $entityType) {
id
name
slug
url
independent
hasLogo
tags: entitiesTags {
tag {
id
name
slug
}
}
sceneTags {
id
name
slug
}
children: childEntitiesConnection(
orderBy: [PRIORITY_DESC, NAME_ASC],
filter: {
type: {
notEqualTo: "info"
}
visible: {
equalTo: true
}
}
) {
nodes {
id
name
slug
url
type
priority
independent
hasLogo
${campaignsFragment}
2022-01-26 12:52:39 +00:00
sceneTotal
children: childEntitiesConnection {
totalCount
}
}
}
${campaignsFragment}
parent {
id
name
slug
type
url
independent
hasLogo
${campaignsFragment}
}
2023-06-18 00:29:10 +00:00
}
connection: releasesConnection(
first: $limit
offset: $offset
orderBy: $orderBy
filter: {
entity: {
or: [
{
slug: { equalTo: $entitySlug }
type: { equalTo: $entityType }
}
{
parent:{
slug: { equalTo: $entitySlug }
type: { equalTo: $entityType }
}
}
{
parent:{
parent: {
slug: { equalTo: $entitySlug }
type: { equalTo: $entityType }
}
}
}
]
}
2023-06-18 00:29:10 +00:00
effectiveDate: {
lessThan: $before,
greaterThan: $after
}
releasesTagsConnection: {
none: {
tag: {
slug: {
in: $exclude
}
}
}
}
}
2023-06-18 00:29:10 +00:00
) {
releases: nodes {
${releaseFields}
}
totalCount
}
2023-06-15 22:47:19 +00:00
${batchFragment}
}
`, {
entitySlug,
entityType,
limit,
offset: Math.max(0, (pageNumber - 1)) * limit,
after,
before,
orderBy,
2021-01-03 21:53:51 +00:00
exclude: store.state.ui.tagFilter,
hasAuth: !!store.state.auth.user,
userId: store.state.auth.user?.id,
});
2020-09-10 01:17:19 +00:00
if (!entity) {
router.replace('/not-found');
return null;
}
return {
2023-06-21 23:31:36 +00:00
entity: curateEntity(entity, null, connection.releases, { lastBatch: lastBatch?.id }),
2023-06-18 00:29:10 +00:00
totalCount: connection.totalCount,
};
}
async function fetchEntities({ _commit }, { type, entitySlugs }) {
const { entities } = await graphql(`
query Entities(
$entitySlugs: [String!] = []
) {
entities(
orderBy: SLUG_ASC
filter: {
or: [
{
type: {
equalTo: "network"
}
childEntitiesConnection: {
some: {
type: {
equalTo: "channel"
}
independent: {
notEqualTo: true
}
}
}
}
{
independent: {
equalTo: true
}
}
{
type: {
equalTo: "channel"
}
parentExists: false
}
{
slug: {
in: $entitySlugs
}
}
],
}
) {
id
name
slug
type
url
independent
hasLogo
children: childEntitiesConnection {
totalCount
}
}
}
`, {
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
) {
id
name
slug
type
url
independent
hasLogo
parent {
name
slug
type
url
hasLogo
}
}
}
`, {
query,
limit,
});
return entities.map((entity) => curateEntity(entity));
}
return {
fetchEntityBySlugAndType,
fetchEntities,
searchEntities,
};
}
export default initEntitiesActions;