index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import api from '@/api'
  4. import { print } from 'graphql/language/printer'
  5. import TextRef from '@/api/queries/TextRef.gql'
  6. import TextdepartRecursive from '@/api/queries/TextdepartRecursive.gql'
  7. Vue.use(Vuex)
  8. export default new Vuex.Store({
  9. modules: {
  10. },
  11. state: {
  12. },
  13. mutations: {
  14. },
  15. actions: {
  16. 'GET_TEXT' ({ state }, { id }) {
  17. return api.post('', { query: print(TextRef), variables: { id } }).then(data => (data.data.data))
  18. },
  19. 'GET_TREE' ({ dispatch }, id) {
  20. return api.post('', { query: print(TextdepartRecursive), variables: { id } }).then(({ data }) => {
  21. const text = data.data.textref
  22. return parse(text, text.id)
  23. })
  24. }
  25. }
  26. })
  27. // Temp data processing
  28. function parse (d, originalId) {
  29. const child = {
  30. name: d.title,
  31. type: d.__typename.toLowerCase(),
  32. class: 'family-' + d.familles[0].id
  33. }
  34. if (d.id === originalId) {
  35. child.class += ' first'
  36. }
  37. let children = []
  38. for (const key of ['text_en_rebond', 'text_produits']) {
  39. if (d[key]) {
  40. children = [...children, ...d[key].filter(text => text.id !== originalId)]
  41. }
  42. }
  43. if (children.length) {
  44. child.children = children.map(child => parse(child, originalId))
  45. }
  46. return child
  47. }