projects.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. }
  39. }
  40. }
  41. },
  42. // actions
  43. actions: {
  44. // async get authors
  45. getProjects ({ dispatch, commit, state }) {
  46. REST.get(`projects`, {})
  47. .then(({ data }) => {
  48. console.log('projects getProjects REST: data', data)
  49. commit('setProjects', data)
  50. })
  51. .catch((error) => {
  52. console.warn('Issue with getProjects', error)
  53. Promise.reject(error)
  54. })
  55. }
  56. }
  57. }