actions.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, experimentId }) {
  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. screen
  51. })
  52. })
  53. },
  54. sendMessage({ state, getters: { getHostURI } }, { msgId, msg = undefined }) {
  55. fetch(`${getHostURI}${API_ROUTES.experimentCollect}`, {
  56. method: 'POST',
  57. headers: {
  58. 'Content-Type': 'application/json'
  59. },
  60. body: JSON.stringify({
  61. uuid: state.uuid,
  62. userId: state.userId,
  63. experimentId: state.experimentId,
  64. msgId,
  65. msg
  66. })
  67. })
  68. },
  69. async loadScenesList({ getters: { isHostConfigured, getHostURI }, commit }) {
  70. if (!isHostConfigured) throw new Error('Host is not configured.')
  71. const scenes = await fetch(`${getHostURI}${API_ROUTES.listScenes}`).then(res => res.json())
  72. commit('setListScenes', scenes.data)
  73. },
  74. setExperimentProgress({ commit }, { experimentName, sceneName, data }) {
  75. commit('setExperimentProgress', { experimentName, sceneName, data })
  76. },
  77. setExperimentDone({ commit }, { experimentName, sceneName, done = true }) {
  78. commit('setExperimentDone', { experimentName, sceneName, done })
  79. }
  80. }