mutations.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import defaultState from '@/store/state'
  2. import Experiments from '@/router/experiments'
  3. const checkProgression = (state, experimentName, sceneName) => {
  4. if (!state.progression[experimentName])
  5. state.progression[experimentName] = {}
  6. if (!state.progression[experimentName][sceneName])
  7. state.progression[experimentName][sceneName] = { done: false, data: {} }
  8. }
  9. const createProgressionObj = (state, scenes) => {
  10. const progressionObj = Experiments.reduce((accExpe, expe) => {
  11. const scenesProgressObj = scenes.reduce((accScene, scene) => {
  12. // Do not overwrite current progression
  13. if (state.progression[expe.name] && state.progression[expe.name][scene])
  14. accScene[scene] = state.progression[expe.name][scene]
  15. else accScene[scene] = { done: false, data: {} }
  16. return accScene
  17. }, {})
  18. accExpe[expe.name] = scenesProgressObj
  19. return accExpe
  20. }, {})
  21. state.progression = progressionObj
  22. }
  23. export default {
  24. setCustomLinkData(state, data) {
  25. state.customLinkData = JSON.parse(atob(data))
  26. },
  27. clearCustomLinkData(state) {
  28. state.customLinkData = null
  29. },
  30. setGdprValidated(state) {
  31. state.gdprConsent = true
  32. },
  33. setAppUniqueId(state) {
  34. state.uuid = [...Array(30)].map(() => Math.random().toString(36)[2]).join('')
  35. },
  36. resetApp(state, { gdprConsent, hostConfig, progression }) {
  37. const defaultStateObj = defaultState()
  38. if (gdprConsent) {
  39. state.gdprConsent = false
  40. state.hostConfig = defaultStateObj.hostConfig
  41. state.userId = defaultStateObj.userId
  42. state.experimentId = defaultStateObj.experimentId
  43. state.progression = defaultStateObj.progression
  44. state.scenesList = defaultStateObj.scenesList
  45. return
  46. }
  47. if (hostConfig) state.hostConfig = defaultStateObj.hostConfig
  48. if (progression) {
  49. // Reset progression and recreate the progression object
  50. state.progression = defaultStateObj.progression
  51. if (state.scenesList) createProgressionObj(state, state.scenesList)
  52. }
  53. },
  54. setHostConfig(state, newConfig) {
  55. state.hostConfig = newConfig
  56. },
  57. setUserExperimentId(state, { userId, experimentId }) {
  58. state.userId = userId
  59. state.experimentId = experimentId
  60. },
  61. setListScenes(state, scenes) {
  62. state.scenesList = scenes
  63. createProgressionObj(state, scenes)
  64. },
  65. setExperimentProgress(state, { experimentName, sceneName, data }) {
  66. checkProgression(state, experimentName, sceneName)
  67. state.progression[experimentName][sceneName].data = data
  68. },
  69. setExperimentDone(state, { experimentName, sceneName, done }) {
  70. checkProgression(state, experimentName, sceneName)
  71. state.progression[experimentName][sceneName].done = done
  72. }
  73. }