From 3553743a9a904802caa5a0e3a974dec66f6aecd7 Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Sat, 17 Jun 2023 13:20:34 -0400 Subject: [PATCH] use global state and add code for toggling task completeness --- client/src/components/Table.vue | 13 +++------- client/src/components/TickComponent.vue | 18 ++++++++++++-- client/src/state.ts | 33 +++++++++++++++++++++++++ client/src/track.ts | 1 - 4 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 client/src/state.ts diff --git a/client/src/components/Table.vue b/client/src/components/Table.vue index aa5f918..c92b646 100644 --- a/client/src/components/Table.vue +++ b/client/src/components/Table.vue @@ -2,13 +2,13 @@ - + - @@ -16,11 +16,8 @@ \ No newline at end of file diff --git a/client/src/components/TickComponent.vue b/client/src/components/TickComponent.vue index 8681322..cd3ffcb 100644 --- a/client/src/components/TickComponent.vue +++ b/client/src/components/TickComponent.vue @@ -1,9 +1,23 @@ \ No newline at end of file diff --git a/client/src/state.ts b/client/src/state.ts new file mode 100644 index 0000000..2b03738 --- /dev/null +++ b/client/src/state.ts @@ -0,0 +1,33 @@ +import { reactive } from "vue" +import { Track } from "./track" +import { error } from "./error" + +export const state = reactive({ + tracks: new Array, + isPopulating: false, + async populate() { + if (this.isPopulating) return + this.isPopulating = true + this.tracks = await Track.fetchAll() + this.isPopulating = false + }, + async taskCompleted(track: Track): Promise { + const result = await fetch(`/api/v1/tracks/${track.id}/ticked`, { method: "PATCH" }) + const body = await result.text() + if (!result.ok) { + error(body) + throw new Error(`error setting tick for track ${track.id} ("${track.name}"): ${result.status} ${result.statusText}`) + } + const tick: Tick = JSON.parse(body) + track.ticks = track.ticks ?? [] + track.ticks.push(tick) + const tracks = this.tracks.map($track => track.id === $track.id ? track : $track) + this.tracks = tracks + return tick + }, + async taskMarkedIncomplete(tick: Tick) { + const { ok, status, statusText } = await fetch(`/api/v1/ticks/${tick.id}`, { method: 'DELETE' }) + if (!ok) + error(`error deleting tick ${tick.id}: ${statusText} (${status})`) + } +}) \ No newline at end of file diff --git a/client/src/track.ts b/client/src/track.ts index 4ce889d..13c2174 100644 --- a/client/src/track.ts +++ b/client/src/track.ts @@ -85,7 +85,6 @@ export class Track implements ITrack { console.error(e) console.debug(body) } - } catch (err) { console.error(err) result.text()
Date{{ track.icon }}{{ track.icon }}
{{ dateString(date) }} - + +