actions.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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: Object.keys(Object.getPrototypeOf(window.screen)).reduce((acc, x) => ((acc[x] = window.screen[x]), acc), {})
  63. })
  64. })
  65. },
  66. sendMessage({ state }, { msgId, msg = undefined }) {
  67. Vue.prototype.$socket.send(JSON.stringify({ uuid: state.uuid, msgId, msg }))
  68. },
  69. async loadScenesList({ getters: { isHostConfigured, getHostURI }, commit }) {
  70. if (!isHostConfigured) throw new Error('Host is not configured.')
  71. const URI = getHostURI
  72. const scenes = await fetch(`${URI}${API_ROUTES.listScenes()}`).then(res => res.json())
  73. commit('setListScenes', scenes.data)
  74. },
  75. setExperimentProgress({ commit }, { experimentName, sceneName, data }) {
  76. commit('setExperimentProgress', { experimentName, sceneName, data })
  77. },
  78. setExperimentDone({ commit }, { experimentName, sceneName, done = true }) {
  79. commit('setExperimentDone', { experimentName, sceneName, done })
  80. }
  81. }