import { graphql } from '../api'; import { releasesFragment, releaseFragment } from '../fragments'; import { curateRelease } from '../curate'; import getDateRange from '../get-date-range'; function initReleasesActions(store, _router) { async function fetchReleases({ _commit }, { limit = 10, pageNumber = 1, range = 'latest' }) { const { before, after, orderBy } = getDateRange(range); const { connection: { releases, totalCount } } = await graphql(` query Releases( $limit:Int = 1000, $offset:Int = 0, $after:Datetime = "1900-01-01", $before:Datetime = "2100-01-01", $orderBy:[ReleasesOrderBy!], $exclude: [String!] ) { ${releasesFragment} } `, { limit, offset: Math.max(0, (pageNumber - 1)) * limit, after, before, orderBy, exclude: store.state.ui.filter, }); return { releases: releases.map(release => curateRelease(release)), totalCount, }; } async function fetchReleaseById({ _commit }, releaseId) { // const release = await get(`/releases/${releaseId}`); const { release } = await graphql(` query Release($releaseId:Int!) { ${releaseFragment} } `, { releaseId: Number(releaseId), }); return curateRelease(release); } async function fetchMovies({ _commit }, { limit = 10, pageNumber = 1 }) { const { connection: { movies, totalCount } } = await graphql(` query Movies( $limit:Int = 1000, $offset:Int = 0, ) { connection: moviesConnection( first: $limit offset: $offset orderBy: DATE_ASC ) { movies: nodes { id title url slug date datePrecision entity { id name slug } covers: moviesCoversByReleaseId { media { id path thumbnail } } } totalCount } } `, { limit, offset: Math.max(0, (pageNumber - 1)) * limit, }); return { movies: movies.map(release => curateRelease(release)), totalCount, }; } return { fetchReleases, fetchReleaseById, fetchMovies, }; } export default initReleasesActions;