actions.js 3.6 KB

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