import Vue from 'vue' import Vuex from 'vuex' import api from '@/api' import { print } from 'graphql/language/printer' import TextRef from '@/api/queries/TextRef.gql' import TextdepartRecursive from '@/api/queries/TextdepartRecursive.gql' Vue.use(Vuex) export default new Vuex.Store({ modules: { }, state: { }, mutations: { }, actions: { 'GET_TEXT' ({ state }, { id }) { return api.post('', { query: print(TextRef), variables: { id } }).then(data => (data.data.data)) }, 'GET_TREE' ({ dispatch }, id) { return api.post('', { query: print(TextdepartRecursive), variables: { id } }).then(({ data }) => { return data.data.textref }) } } }) // Temp data processing function parse (d, originalId) { const child = { name: d.title, type: d.__typename.toLowerCase(), class: 'family-' + d.familles[0].id } if (d.id === originalId) { child.class += ' first' } let children = [] for (const key of ['text_en_rebond', 'text_produits']) { if (d[key]) { children = [...children, ...d[key].filter(text => text.id !== originalId)] } } if (children.length) { child.children = children.map(child => parse(child, originalId)) } return child }