actions.js 3.7 KB

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