actions.js 1.6 KB

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