projects.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // REST
  58. // let params = {
  59. // _sort: `weight:ASC`
  60. // }
  61. // let q = qs.stringify(params)
  62. // REST.get(`projects?` + q, {})
  63. // .then(({ data }) => {
  64. // console.log('projects getProjects REST: data', data)
  65. // commit('setProjects', data)
  66. // })
  67. // .catch((error) => {
  68. // console.warn('Issue with getProjects', error)
  69. // Promise.reject(error)
  70. // })
  71. },
  72. computeProjects ({ dispatch, commit, state }, projects) {
  73. console.log('computeProjects', this)
  74. // register a new module only if doesn't exist
  75. let name, size, w
  76. // console.log('state.marginBetweenBuildings', state.marginBetweenBuildings)
  77. // console.log('state.count', state.count)
  78. commit('increaseTotalW', state.marginBetweenBuildings * (projects.length - 1))
  79. // console.log('PROJECTS state.totalW', state.totalW)
  80. projects.forEach((project, i) => {
  81. name = `project:${project.id}`
  82. // convert to number the project id
  83. projects[i].id = parseInt(projects[i].id, 10)
  84. if (!(this.state && this.state[name])) {
  85. // Randomly Define the size and position of each building as here we need to know the totalW (not so clean)
  86. w = Math.round(21 + Math.random() * 15)
  87. // console.log('w', w)
  88. commit('increaseTotalW', w)
  89. size = {
  90. x: w,
  91. y: Math.round(110 + Math.random() * 100),
  92. z: Math.round(10 + Math.random() * 30)
  93. }
  94. // convert to number the project id
  95. project.id = parseInt(project.id, 10)
  96. // fill the dynamic store module with data
  97. // merge default sate with loaded data, with calculated size, with index
  98. projectMod.state = { ...projectMod.state, ...project, size: size, index: i }
  99. // register the module
  100. this.registerModule(name, projectMod)
  101. } else {
  102. // re-use the already existing module
  103. console.log(`reusing module project:${project.id}`)
  104. }
  105. })
  106. // record loaded projects
  107. commit('setProjects', projects)
  108. // init each dynamic modules
  109. state.projects.forEach((project, i) => {
  110. dispatch(`project:${project.id}/init`, null, { root: true })
  111. })
  112. }
  113. }
  114. }