projects.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { REST } from 'api/rest-axios'
  2. export default {
  3. namespaced: true,
  4. // initial state
  5. state: {
  6. projects: []
  7. },
  8. // getters
  9. getters: {},
  10. // mutations
  11. mutations: {
  12. setProjects (state, projects) {
  13. // record the retrieved data
  14. state.projects = projects
  15. // randomly define building sizes
  16. let totalW = 0
  17. for (var i = 0; i < state.projects.length; i++) {
  18. let w = Math.round(21 + Math.random() * 15)
  19. totalW += w
  20. state.projects[i].size = {
  21. x: w,
  22. y: Math.round(100 + Math.random() * 250),
  23. z: Math.round(10 + Math.random() * 30)
  24. }
  25. }
  26. // positioning buildings on x regarding the widths
  27. let margin = 5
  28. totalW += margin * (state.projects.length - 1)
  29. for (var j = 0; j < state.projects.length; j++) {
  30. let x = -1 * totalW / 2// + state.projects[0].size.x / 2
  31. for (var k = 0; k < j; k++) {
  32. x += state.projects[k].size.x
  33. }
  34. x += margin * j
  35. x += state.projects[j].size.x / 2
  36. state.projects[j].position = {
  37. x: x,
  38. y: -1 * state.projects[j].size.y / 2 + 10 + Math.random() * 30, // -10 + Math.random() * this.size.y / 2
  39. z: -10 + Math.random() * 10
  40. }
  41. }
  42. }
  43. },
  44. // actions
  45. actions: {
  46. // async get authors
  47. getProjects ({ dispatch, commit, state }) {
  48. REST.get(`projects`, {})
  49. .then(({ data }) => {
  50. console.log('projects getProjects REST: data', data)
  51. commit('setProjects', data)
  52. })
  53. .catch((error) => {
  54. console.warn('Issue with getProjects', error)
  55. Promise.reject(error)
  56. })
  57. }
  58. }
  59. }