import api from '@/api' import { print } from 'graphql/language/printer' import { TextsDepart, TextCard, TextdepartRecursive } from '@/api/queries' import TextdepartRecursiveWithDepth from '@/api/queries/TextdepartRecursiveWithDepth.gql' export default { state: { textsDepart: undefined }, mutations: { 'SET_TEXTS_DEPART' (state, texts) { state.textsDepart = texts } }, actions: { 'GET_TEXTS_DEPART' ({ state, commit }) { return api.post('', { query: print(TextsDepart) }).then(({ data }) => { commit('SET_TEXTS_DEPART', data.data.textsdepart) return state.textsDepart }) }, 'GET_TEXT' (store, { id }) { return api.post('', { query: print(TextCard), variables: { id } }) .then(data => (data.data.data.text)) }, 'GET_TREE' (store, id) { return api.post('', { query: print(TextdepartRecursive), variables: { id } }) .then(({ data }) => (data.data.textref)) }, 'GET_TREE_WITH_DEPTH' (store, { id, depth }) { const baseQuery = print(TextdepartRecursiveWithDepth) function formatQuery (str, depth) { if (depth > 0) { return formatQuery( str.replace('INPUT', '...TextrefTreeFields\nsiblings: text_en_rebond {\nINPUT\n}'), --depth ) } else { return str.replace('INPUT', '...TextrefTreeFields') } } return api.post('', { query: formatQuery(baseQuery, depth), variables: { id } }) .then(({ data }) => (data.data.textref)) } }, getters: { textsDepartOptions: state => { if (!state.textsDepart) return undefined return state.textsDepart.map(({ id, title }) => ({ value: id, text: `(${id}) ${title}` })) }, orderedParents: state => { // FIXME duplicates references if multiple authors ? if (!state.textsDepart) return undefined return state.textsDepart.sort((a, b) => { if (!b.authors) return -1 if (!a.authors) return +1 if (a.authors[0].name < b.authors[0].name) return -1 if (a.authors[0].name > b.authors[0].name) return 1 return 0 }).reduce((dict, text) => { const firstChar = text.authors ? text.authors[0].name[0] : '&' if (!(firstChar in dict)) dict[firstChar] = [] dict[firstChar].push(text) return dict }, {}) } } }