actions.js 3.0 KB

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