actions.js 3.1 KB

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