nodes.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import Vue from 'vue'
  2. import api from '@/api'
  3. import {
  4. DATA_LEVELS,
  5. ID_VARIANTS,
  6. RELATIONS // FIXME REMOVE WHEN DB MAJ
  7. } from '@/store/utils'
  8. import {
  9. AllNodesOfVariant
  10. } from '@/api/queries'
  11. export default {
  12. state: {
  13. nodes: {},
  14. history: [],
  15. // IDS
  16. ids: {
  17. // depart: undefined,
  18. // kit: undefined,
  19. // creation: undefined
  20. }
  21. },
  22. mutations: {
  23. 'SET_ALL_NODES_IDS' (state, [variant, nodes]) {
  24. Vue.set(state.ids, variant, nodes.map(({ id }) => id))
  25. },
  26. 'ADD_NODES' (state, [nodes, dataLevel]) {
  27. for (const node of nodes) {
  28. const stateNode = node.id in state.nodes ? state.nodes[node.id] : undefined
  29. // FIXME UPDATE AFTER DB MAJ
  30. if (node.variant === null || Array.isArray(node.variant)) {
  31. node.variant = node.variant !== null ? ID_VARIANTS[node.variant[0].id] : 'dark'
  32. }
  33. if (node.type) {
  34. node.type = node.type === 'texte' ? 'ref' : 'prod'
  35. }
  36. // FIXME REMOVE AFTER DB MAJ
  37. for (const relation of RELATIONS) {
  38. if (relation in node && node[relation]) {
  39. node[relation] = node[relation].map(subNode => {
  40. if (!(subNode.id in state.nodes)) {
  41. subNode.variant = subNode.variant !== null ? ID_VARIANTS[subNode.variant[0].id] : 'black'
  42. subNode.dataLevel = -1
  43. Vue.set(state.nodes, subNode.id, subNode)
  44. }
  45. return state.nodes[subNode.id]
  46. })
  47. }
  48. if (node[relation] === null) {
  49. node[relation] = []
  50. }
  51. }
  52. if (!stateNode || stateNode.dataLevel < dataLevel) {
  53. node.dataLevel = dataLevel
  54. }
  55. Vue.set(state.nodes, node.id, stateNode ? { ...stateNode, ...node } : node)
  56. }
  57. }
  58. },
  59. actions: {
  60. async 'GET_ALL_NODES_IDS' ({ state, commit }, variant) {
  61. if (state.ids[variant] === undefined) {
  62. // FIXME UPDATE WHEN QUERY OK
  63. // await api.query(AllNodesOfVariant, { variantId: VARIANT_IDS[variant] }).then(data => {
  64. await api.query(AllNodesOfVariant).then(data => {
  65. commit('SET_ALL_NODES_IDS', [variant, data.nodes])
  66. })
  67. }
  68. return state.ids[variant]
  69. },
  70. async 'GET_NODES' ({ state, commit, getters }, { ids, dataLevel = 'partial' }) {
  71. const lvl = DATA_LEVELS[dataLevel]
  72. const nodesIdsToQuery = []
  73. let lowestLvl = lvl
  74. for (const id of ids) {
  75. const nodeLvl = id in state.nodes ? state.nodes[id].dataLevel : -1
  76. if (nodeLvl < lvl) {
  77. nodesIdsToQuery.push(id)
  78. }
  79. if (nodeLvl < lowestLvl) {
  80. lowestLvl = nodeLvl
  81. }
  82. }
  83. if (nodesIdsToQuery.length) {
  84. const levelsToQuery = Array(lvl - lowestLvl).fill(lowestLvl + 1).map((v, i) => v + i)
  85. await api.queryNodes(nodesIdsToQuery, levelsToQuery).then(data => {
  86. commit('ADD_NODES', [data.nodes, lvl])
  87. })
  88. }
  89. return getters.nodes(ids)
  90. },
  91. async 'GET_NODE' ({ state, commit, getters }, { id, dataLevel = 'full' }) {
  92. const lvl = DATA_LEVELS[dataLevel]
  93. const nodeLvl = id in state.nodes ? state.nodes[id].dataLevel : -1
  94. if (nodeLvl < lvl) {
  95. const levelsToQuery = Array(lvl - nodeLvl).fill(nodeLvl + 1).map((v, i) => v + i)
  96. await api.queryNode(id, levelsToQuery).then(data => {
  97. commit('ADD_NODES', [[data.node], lvl])
  98. })
  99. }
  100. return getters.node(id)
  101. }
  102. },
  103. getters: {
  104. allNodes: state => state.nodes,
  105. // Args getters
  106. nodes: state => ids => ids.map(id => state.nodes[id]),
  107. node: state => id => state.nodes[id]
  108. }
  109. }