index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. return data.data.textref
  22. })
  23. }
  24. }
  25. })
  26. // Temp data processing
  27. function parse (d, originalId) {
  28. const child = {
  29. name: d.title,
  30. type: d.__typename.toLowerCase(),
  31. class: 'family-' + d.familles[0].id
  32. }
  33. if (d.id === originalId) {
  34. child.class += ' first'
  35. }
  36. let children = []
  37. for (const key of ['text_en_rebond', 'text_produits']) {
  38. if (d[key]) {
  39. children = [...children, ...d[key].filter(text => text.id !== originalId)]
  40. }
  41. }
  42. if (children.length) {
  43. child.children = children.map(child => parse(child, originalId))
  44. }
  45. return child
  46. }