traxxx/assets/js/tags/actions.js

172 lines
4.4 KiB
JavaScript

import { graphql, get } from '../api';
import {
releaseFields,
} from '../fragments';
import { curateTag, curateRelease } from '../curate';
import getDateRange from '../get-date-range';
function initTagsActions(store, _router) {
async function fetchTagBySlug({ _commit }, {
tagSlug,
pageNumber = 1,
limit = 100,
range = 'latest',
}) {
const { before, after, orderBy } = getDateRange(range);
const { tagBySlug } = await graphql(`
query Tag(
$tagSlug:String!
$offset: Int = 0,
$limit:Int = 1000,
$after:Datetime = "1900-01-01",
$before:Datetime = "2100-01-01",
$orderBy: [ReleasesOrderBy!],
$exclude: [String!]
) {
tagBySlug(slug:$tagSlug) {
id
name
slug
description
group {
name
slug
}
poster: tagsPosterByTagId {
media {
id
thumbnail
path
comment
sfw: sfwMedia {
id
thumbnail
path
comment
}
}
}
photos: tagsPhotos(orderBy: MEDIA_BY_MEDIA_ID__INDEX_ASC) {
media {
id
thumbnail
lazy
path
comment
sfw: sfwMedia {
id
thumbnail
lazy
path
comment
}
}
}
scenesConnection(
filter: {
date: {
lessThan: $before,
greaterThan: $after,
},
releasesTagsConnection: {
none: {
tag: {
slug: {
in: $exclude
}
}
}
}
},
first: $limit,
orderBy: $orderBy,
offset: $offset
) {
releases: nodes {
${releaseFields}
}
totalCount
}
}
}
`, {
tagSlug,
limit,
after,
before,
orderBy: orderBy === 'DATE_DESC' ? 'DATE_DESC' : 'DATE_ASC',
offset: Math.max(0, (pageNumber - 1)) * limit,
exclude: store.state.ui.filter,
});
return {
tag: curateTag(tagBySlug, null, curateRelease),
releases: tagBySlug.scenesConnection.releases.map(release => curateRelease(release)),
totalCount: tagBySlug.scenesConnection.totalCount,
};
}
async function fetchTags({ _commit }, {
limit = 100,
slugs = [],
_group,
_priority,
}) {
const { tags } = await graphql(`
query Tags(
$slugs: [String!] = [],
$limit: Int = 100
) {
tags(
filter: { slug: { in: $slugs } },
first: $limit
) {
id
name
slug
poster: tagsPosterByTagId {
media {
thumbnail
comment
lazy
sfw: sfwMedia {
thumbnail
comment
lazy
}
}
}
group {
name
slug
}
}
}
`, {
slugs,
limit,
});
return tags.map(tag => curateTag(tag, store.state.ui.sfw));
}
async function fetchTagReleases({ _commit }, tagId) {
const releases = await get(`/tags/${tagId}/releases`, {
filter: store.state.ui.filter,
after: store.getters.after,
before: store.getters.before,
});
return releases;
}
return {
fetchTagBySlug,
fetchTags,
fetchTagReleases,
};
}
export default initTagsActions;