actions.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { API_ROUTES, buildURI } from '../functions'
  2. export default {
  3. resetApp({ commit }, { hostConfig = false, progression = false }) {
  4. commit('resetApp', { hostConfig, progression })
  5. },
  6. async setHostConfig({ commit }, { protocol, host, port }) {
  7. // Timeout after 1s
  8. const controller = new AbortController()
  9. const signal = controller.signal
  10. setTimeout(() => controller.abort(), 1500)
  11. const URI = buildURI(protocol, host, port, API_ROUTES.ping())
  12. return fetch(URI, { signal })
  13. .then(async res => {
  14. if (res.status !== 200) throw new Error(`Received wrong HTTP status code : ${res.status} (Need 200).`)
  15. const content = await res.text()
  16. if (content !== 'pong') throw new Error('Received wrong web content (Need to receive "pong").')
  17. // Configuration is valid
  18. commit('setHostConfig', { protocol, host, port })
  19. })
  20. .catch(err => {
  21. // Host not reachable or invalid HTTP status code
  22. throw new Error(`Invalid configuration "${URI}". ${!err.message.includes('aborted') ? err.message : ''}`)
  23. })
  24. },
  25. async loadScenesList({ getters: { isHostConfigured, getHostURI }, commit }) {
  26. if (!isHostConfigured) throw new Error('Host is not configured.')
  27. const URI = getHostURI
  28. const scenes = await fetch(`${URI}${API_ROUTES.listScenes()}`).then(res => res.json())
  29. commit('setListScenes', scenes.data)
  30. },
  31. setExperimentProgress({ commit }, { experimentName, sceneName, data }) {
  32. commit('setExperimentProgress', { experimentName, sceneName, data })
  33. },
  34. setExperimentDone({ commit }, { experimentName, sceneName, done = true }) {
  35. commit('setExperimentDone', { experimentName, sceneName, done })
  36. }
  37. }