From a390f79a75e3e59e88ac4dc7c8d755e113ee418a Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Tue, 27 Jun 2023 05:59:46 -0400 Subject: [PATCH] Add Login view --- client/src/router.ts | 3 +- client/src/state.ts | 32 ++++++++++++------ client/src/views/Login.vue | 69 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 client/src/views/Login.vue diff --git a/client/src/router.ts b/client/src/router.ts index 806e60d..8d94d4c 100644 --- a/client/src/router.ts +++ b/client/src/router.ts @@ -6,7 +6,8 @@ const router = createRouter({ history: createWebHistory(), routes: [ { path: '/', component: TableView }, - { path: '/new-track', component: NewTrackView } + { path: '/new-track', component: NewTrackView }, + { path: '/login', component: import('./views/Login.vue') } // for other pages: // {path: '/', component: import('./views/TableView.vue')} ] diff --git a/client/src/state.ts b/client/src/state.ts index d449b60..13e3312 100644 --- a/client/src/state.ts +++ b/client/src/state.ts @@ -18,9 +18,19 @@ function dateQuery(date: Date): URLSearchParams { return query } -export const state = reactive({ - tracks: new Array, - state: State.Unfetched, +interface LoggedInUser { + name: string +} + +class AppState { + tracks: Array + state: State + user?: LoggedInUser + + constructor() { + this.tracks = new Array + this.state = State.Unfetched + } streamUpdatesFromServer() { const source = new EventSource("/api/v1/updates") source.addEventListener("open", () => console.debug("opened event source")) @@ -69,17 +79,17 @@ export const state = reactive({ window.location = window.location }) window.addEventListener('beforeunload', () => source.close()) - }, + } async repopulate() { this.state = State.Fetching this.tracks = await Track.fetchAll() - }, + } async populate() { if (this.state != State.Unfetched) return await this.repopulate() this.streamUpdatesFromServer() this.state = State.Fetched - }, + } async taskCompleted(track: Track, date: Date): Promise { const query = dateQuery(date) const response: Response = await fetch(`/api/v1/tracks/${track.id}/ticked?${query.toString()}`, { method: "PATCH" }) @@ -89,13 +99,13 @@ export const state = reactive({ throw new Error(`error setting tick for track ${track.id} ("${track.name}"): ${response.status} ${response.statusText}`) } return JSON.parse(body) - }, + } async taskMarkedIncomplete(track: Track, date: Date) { const query = dateQuery(date) const { ok, status, statusText } = await fetch(`/api/v1/tracks/${track.id}/all-ticks?${query.toString()}`, { method: 'DELETE' }) if (!ok) error(`error deleting ticks for ${track.id}: ${statusText} (${status})`) - }, + } async addTrack(track: Track): Promise { const response = await fetch('/api/v1/tracks', { method: "POST", @@ -105,9 +115,11 @@ export const state = reactive({ if (!response.ok) error(`error submitting track: ${track}: ${response.statusText} (${response.status})`) return response.ok - }, + } async removeTrack(trackID: number) { const response = await fetch(`/api/v1/tracks/${trackID}`, { method: "DELETE" }) if (!response.ok) error(`error deleting track with ID ${trackID}: ${response.statusText} (${response.status})`) } -}) +} + +export const state = reactive(new AppState) diff --git a/client/src/views/Login.vue b/client/src/views/Login.vue new file mode 100644 index 0000000..2ef2efe --- /dev/null +++ b/client/src/views/Login.vue @@ -0,0 +1,69 @@ + + + + \ No newline at end of file