浏览代码

add action GET_TREE to resolve d3 ready text hierachy

axolotle 3 年之前
父节点
当前提交
c2127389ed
共有 1 个文件被更改,包括 27 次插入2 次删除
  1. 27 2
      src/store/index.js

+ 27 - 2
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
+}