projects.js 3.6 KB

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