projects.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // import qs from 'querystring'
  2. // import { REST } from 'api/rest-axios'
  3. import { GRAPHQL } from 'api/graphql-axios'
  4. import projectMod from 'store/modules/project'
  5. export default {
  6. namespaced: true,
  7. // initial state
  8. state: {
  9. projects: [],
  10. marginBetweenBuildings: 5,
  11. totalW: 0
  12. },
  13. // getters
  14. getters: {
  15. totalW: (state) => {
  16. return state.totalW
  17. },
  18. marginBetweenBuildings: (state) => {
  19. return state.marginBetweenBuildings
  20. },
  21. projectID: (state) => (index) => {
  22. // console.log('getter projectID index', index)
  23. return state.projects[index].id
  24. }
  25. },
  26. // mutations
  27. mutations: {
  28. setProjects (state, projects) {
  29. // record the projects
  30. state.projects = projects
  31. },
  32. increaseTotalW (state, w) {
  33. state.totalW += w
  34. }
  35. },
  36. // actions
  37. actions: {
  38. // async load projects
  39. loadProjects ({ dispatch, commit, state }) {
  40. // GRAPHQL
  41. GRAPHQL.post('', { query: `query {
  42. projects(sort: "Weight:asc") {
  43. id
  44. Weight
  45. Titre
  46. Faces_opaques
  47. }
  48. }` })
  49. .then(({ data: { data: { projects } = null } }) => {
  50. console.log('graphql projects', projects)
  51. dispatch('computeProjects', projects)
  52. })
  53. .catch((error) => {
  54. console.warn('Issue with loadProjects', error)
  55. Promise.reject(error)
  56. })
  57. },
  58. computeProjects ({ dispatch, commit, state }, projects) {
  59. console.log('computeProjects', this)
  60. // register a new module only if doesn't exist
  61. let name, size, w
  62. // console.log('state.marginBetweenBuildings', state.marginBetweenBuildings)
  63. // console.log('state.count', state.count)
  64. commit('increaseTotalW', state.marginBetweenBuildings * (projects.length - 1))
  65. // console.log('PROJECTS state.totalW', state.totalW)
  66. projects.forEach((project, i) => {
  67. name = `project:${project.id}`
  68. // convert to number the project id
  69. projects[i].id = parseInt(projects[i].id, 10)
  70. if (!(this.state && this.state[name])) {
  71. // Randomly Define the size and position of each building as here we need to know the totalW (not so clean)
  72. w = Math.round(21 + Math.random() * 15)
  73. // console.log('w', w)
  74. commit('increaseTotalW', w)
  75. size = {
  76. x: w,
  77. y: Math.round(110 + Math.random() * 100),
  78. z: Math.round(21 + Math.random() * 20)
  79. }
  80. // convert to number the project id
  81. project.id = parseInt(project.id, 10)
  82. // fill the dynamic store module with data
  83. // merge default sate with loaded data, with calculated size, with index
  84. projectMod.state = { ...projectMod.state, ...project, size: size, index: i }
  85. // register the module
  86. this.registerModule(name, projectMod)
  87. } else {
  88. // re-use the already existing module
  89. console.log(`reusing module project:${project.id}`)
  90. }
  91. })
  92. // record loaded projects
  93. commit('setProjects', projects)
  94. // init each dynamic modules
  95. state.projects.forEach((project, i) => {
  96. dispatch(`project:${project.id}/init`, null, { root: true })
  97. })
  98. }
  99. }
  100. }