projects.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. getGridPos: (state) => (index) => {
  26. let popped = state.projects[index].grid.pop()
  27. console.log('getGridPos: popped', popped)
  28. return popped
  29. }
  30. },
  31. // mutations
  32. mutations: {
  33. setProjects (state, projects) {
  34. // record the projects
  35. state.projects = projects
  36. },
  37. increaseTotalW (state, w) {
  38. state.totalW += w
  39. }
  40. },
  41. // actions
  42. actions: {
  43. // async get authors
  44. loadProjects ({ dispatch, commit, state }) {
  45. // GRAPHQL
  46. GRAPHQL.post('', { query: `query {
  47. projects(sort: "Weight:asc") {
  48. id
  49. Weight
  50. Titre
  51. }
  52. }` })
  53. .then(({ data: { data: { projects } = null } }) => {
  54. console.log('graphql projects', projects)
  55. dispatch('computeProjects', projects)
  56. })
  57. .catch((error) => {
  58. console.warn('Issue with getProjects', error)
  59. Promise.reject(error)
  60. })
  61. // REST
  62. // let params = {
  63. // _sort: `weight:ASC`
  64. // }
  65. // let q = qs.stringify(params)
  66. // REST.get(`projects?` + q, {})
  67. // .then(({ data }) => {
  68. // console.log('projects getProjects REST: data', data)
  69. // commit('setProjects', data)
  70. // })
  71. // .catch((error) => {
  72. // console.warn('Issue with getProjects', error)
  73. // Promise.reject(error)
  74. // })
  75. },
  76. computeProjects ({ dispatch, commit, state }, projects) {
  77. console.log('computeProjects', this)
  78. // register a new module only if doesn't exist
  79. let name, size, w
  80. // console.log('state.marginBetweenBuildings', state.marginBetweenBuildings)
  81. // console.log('state.count', state.count)
  82. commit('increaseTotalW', state.marginBetweenBuildings * (projects.length - 1))
  83. console.log('PROJECTS state.totalW', state.totalW)
  84. projects.forEach((project, i) => {
  85. name = `project:${project.id}`
  86. // convert to number the project id
  87. projects[i].id = parseInt(projects[i].id, 10)
  88. if (!(this.state && this.state[name])) {
  89. // Randomly Define the size and position of each building as here we need to know the totalW (not so clean)
  90. w = Math.round(21 + Math.random() * 15)
  91. console.log('w', w)
  92. commit('increaseTotalW', w)
  93. size = {
  94. x: w,
  95. y: Math.round(100 + Math.random() * 250),
  96. z: Math.round(10 + Math.random() * 30)
  97. }
  98. // convert to number the project id
  99. project.id = parseInt(project.id, 10)
  100. // fill the dynamic store module with data
  101. // merge default sate with loaded data, with calculated size, with index
  102. projectMod.state = { ...projectMod.state, ...project, size: size, index: i }
  103. // register the module
  104. this.registerModule(name, projectMod)
  105. } else {
  106. // re-use the already existing module
  107. console.log(`reusing module project:${project.id}`)
  108. }
  109. })
  110. // record loaded projects
  111. commit('setProjects', projects)
  112. // init each dynamic modules
  113. state.projects.forEach((project, i) => {
  114. dispatch(`project:${project.id}/init`, null, { root: true })
  115. })
  116. }
  117. }
  118. }