From c2127389ed523f79139f02103524bf6ba47c15b7 Mon Sep 17 00:00:00 2001 From: axolotle Date: Wed, 24 Feb 2021 17:30:58 +0100 Subject: [PATCH] add action GET_TREE to resolve d3 ready text hierachy --- src/store/index.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/store/index.js b/src/store/index.js index 567d537..3b03edd 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -4,6 +4,7 @@ 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) @@ -20,8 +21,32 @@ export default new Vuex.Store({ }, actions: { - 'GET_TEXT' ({ state }, id) { - return api.post('', { query: print(TextRef), variables: { id } }) + '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 parse(data.data.textref) + }) } } }) + +// Temp data processing +function parse (d) { + const child = { + name: d.title, + type: d.__typename.toLowerCase() + } + let children = [] + for (const key of ['text_en_rebond', 'text_produits']) { + if (d[key]) { + children = [...children, ...d[key].map(text => parse(text))] + } + } + if (children.length) { + child.children = children + } + return child +}