actions.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import Vue from 'vue'
  2. import router from '../router'
  3. import { API_ROUTES, buildURI, buildWsURI, delay } 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. async connectToWs({ state, getters }) {
  44. if (state.socket.isConnected) return /*eslint-disable-line */
  45. else if (getters.isHostConfigured) {
  46. this._vm.$connect(getters.getHostWsURI)
  47. // $connect does not return a Promise, so we wait to know if it worked
  48. await delay(300)
  49. if (!state.socket.isConnected)
  50. throw new Error('Could not connect to remote WebSocket server.')
  51. }
  52. else throw new Error('Could not connect to WebSocket server. Host is not configured.')
  53. },
  54. async collectUserData({ state, getters }) {
  55. return fetch(getters.getHostURI + API_ROUTES.dataCollect(), {
  56. method: 'POST',
  57. headers: {
  58. 'Content-Type': 'application/json'
  59. },
  60. body: JSON.stringify({
  61. uuid: state.uuid,
  62. viewport: {
  63. x: window.innerWidth,
  64. y: window.innerHeight
  65. }
  66. })
  67. })
  68. },
  69. sendMessage({ state }, { msgId, msg = undefined }) {
  70. Vue.prototype.$socket.send(JSON.stringify({ uuid: state.uuid, msgId, msg }))
  71. },
  72. async loadScenesList({ getters: { isHostConfigured, getHostURI }, commit }) {
  73. if (!isHostConfigured) throw new Error('Host is not configured.')
  74. const URI = getHostURI
  75. const scenes = await fetch(`${URI}${API_ROUTES.listScenes()}`).then(res => res.json())
  76. commit('setListScenes', scenes.data)
  77. },
  78. setExperimentProgress({ commit }, { experimentName, sceneName, data }) {
  79. commit('setExperimentProgress', { experimentName, sceneName, data })
  80. },
  81. setExperimentDone({ commit }, { experimentName, sceneName, done = true }) {
  82. commit('setExperimentDone', { experimentName, sceneName, done })
  83. }
  84. }