feature: add store for tracking network request to API

This commit is contained in:
Ondřej Synáček 2020-12-20 12:07:37 +01:00
parent 4fad8e442d
commit 128b03344b
5 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,5 @@
import Request from './request'
export {
Request,
}

View File

@ -0,0 +1,9 @@
export default class Request {
constructor({
id,
error,
}) {
this.id = id
this.error = error
}
}

View File

@ -1,7 +1,9 @@
import configStore from './configStore' import configStore from './configStore'
import eventStore from './eventStore' import eventStore from './eventStore'
import requestStore from './requestStore'
export { export {
configStore, configStore,
eventStore, eventStore,
requestStore,
} }

View File

@ -0,0 +1,3 @@
import { writable } from 'svelte/store'
export default writable(null)

View File

@ -9,8 +9,16 @@ import { extractEventDataFromHTML } from '../../lib/services/ics-retriever'
import generateICS from '../../lib/services/ics-generator' import generateICS from '../../lib/services/ics-generator'
import boot from '../frontend' import boot from '../frontend'
import { requestStore } from '../frontend/stores'
import { Request } from '../frontend/records'
(() => { (() => {
let request = null
requestStore.subscribe((updatedRequest) => {
request = updatedRequest
})
document.addEventListener('DOMContentLoaded', boot) document.addEventListener('DOMContentLoaded', boot)
const createRecord = (uri, summary, startTime) => { const createRecord = (uri, summary, startTime) => {
@ -73,17 +81,29 @@ import boot from '../frontend'
input.disabled = true input.disabled = true
submitButton.disabled = true submitButton.disabled = true
setStatusDownloading() setStatusDownloading()
requestStore.set(new Request({
id: uuidv4(),
error: null,
}))
} }
const finishedRequest = () => { const finishedRequest = () => {
input.disabled = false input.disabled = false
submitButton.disabled = false submitButton.disabled = false
clearStatuses() clearStatuses()
requestStore.set(null)
} }
const handleError = (error) => { const handleError = (error) => {
finishedRequest() finishedRequest()
setStatusError(error) setStatusError(error)
requestStore.update((prevRequest) => {
prevRequest.error = error
return prevRequest
})
} }
const postURL = (data) => { const postURL = (data) => {