add action GET_TREE to resolve d3 ready text hierachy

This commit is contained in:
axolotle
2021-02-24 17:30:58 +01:00
parent 11ca71e37d
commit c2127389ed
+27 -2
View File
@@ -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
}