actions.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import router from '../router'
  2. import { API_ROUTES, serialize } from '../functions'
  3. export default {
  4. setGdprValidated({ commit }) {
  5. commit('setGdprValidated')
  6. router.push('/hostConfig')
  7. },
  8. setAppUniqueId({ state, commit }) {
  9. if (!state.uuid) commit('setAppUniqueId')
  10. },
  11. async resetApp({ dispatch, commit }, { gdprConsent = false, hostConfig = false, progression = false, scenesList = false }) {
  12. if (!gdprConsent && !hostConfig && scenesList) {
  13. await dispatch('loadScenesList')
  14. router.go()
  15. }
  16. commit('resetApp', { gdprConsent, hostConfig, progression })
  17. },
  18. async setHostConfig({ commit, dispatch }, hostConfig) {
  19. // Timeout after 1.5s
  20. const controller = new AbortController()
  21. const signal = controller.signal
  22. setTimeout(() => controller.abort(), 1500)
  23. const URI = `${hostConfig}${API_ROUTES.ping}`
  24. return fetch(URI, { signal })
  25. .then(async res => {
  26. if (res.status !== 200) throw new Error(`Received wrong HTTP status code : ${res.status} (Need 200).`)
  27. const content = await res.text()
  28. if (content !== 'pong') throw new Error('Received wrong web content (Need to receive "pong").')
  29. // Configuration is valid
  30. commit('setHostConfig', hostConfig)
  31. router.push('/experiments')
  32. dispatch('collectUserData')
  33. })
  34. .catch(err => {
  35. // Host not reachable or invalid HTTP status code
  36. throw new Error(`Invalid configuration "${URI}". ${!err.message.includes('aborted') ? err.message : ''}`)
  37. })
  38. },
  39. setUserExperimentId({ commit }, { userId = null, experimentId = null }) {
  40. commit('setUserExperimentId', { userId, experimentId })
  41. },
  42. async collectUserData({ state, getters }) {
  43. let screen = serialize(window.screen)
  44. screen.orientation = serialize(window.screen.orientation)
  45. return fetch(getters.getHostURI + API_ROUTES.dataCollect, {
  46. method: 'POST',
  47. headers: {
  48. 'Content-Type': 'application/json'
  49. },
  50. body: JSON.stringify({
  51. uuid: state.uuid,
  52. userId: state.userId,
  53. experimentId: state.experimentId,
  54. screen
  55. })
  56. })
  57. },
  58. sendMessage({ state, getters: { getHostURI } }, { msgId, msg = undefined }) {
  59. fetch(`${getHostURI}${API_ROUTES.experimentCollect}`, {
  60. method: 'POST',
  61. headers: {
  62. 'Content-Type': 'application/json'
  63. },
  64. body: JSON.stringify({
  65. uuid: state.uuid,
  66. userId: state.userId,
  67. experimentId: state.experimentId,
  68. msgId,
  69. msg
  70. })
  71. })
  72. },
  73. async loadScenesList({ getters: { isHostConfigured, getHostURI }, commit }) {
  74. if (!isHostConfigured) throw new Error('Host is not configured.')
  75. const scenes = await fetch(`${getHostURI}${API_ROUTES.listScenes}`).then(res => res.json())
  76. commit('setListScenes', scenes.data)
  77. },
  78. setExperimentProgress({ commit }, { experimentName, sceneName, data }) {
  79. commit('setExperimentProgress', { experimentName, sceneName, data })
  80. },
  81. setExperimentDone({ commit }, { experimentName, sceneName, done = true }) {
  82. commit('setExperimentDone', { experimentName, sceneName, done })
  83. }
  84. }