add tests

This commit is contained in:
D. Scott Boggs 2023-07-22 15:46:52 -04:00
parent 1c400e7ffa
commit a8e4e5145b
17 changed files with 283 additions and 66 deletions

View file

@ -26,6 +26,7 @@ class AppState {
tracks: Array<Track>
state: State
user?: LoggedInUser
source?: EventSource
constructor() {
this.tracks = new Array<Track>
@ -79,16 +80,22 @@ class AppState {
window.location = window.location
})
window.addEventListener('beforeunload', () => source.close())
this.source = source
}
async repopulate() {
if (!this.user) {
this.tracks = []
return
}
this.state = State.Fetching
this.tracks = await Track.fetchAll()
this.source?.close()
this.streamUpdatesFromServer()
this.state = State.Fetched
}
async populate() {
if (this.state != State.Unfetched) return
await this.repopulate()
this.streamUpdatesFromServer()
this.state = State.Fetched
}
async taskCompleted(track: Track, date: Date): Promise<Tick> {
const query = dateQuery(date)

View file

@ -1,4 +1,5 @@
import { error } from "./error"
import { Tick, ITick } from './ticks'
export interface ITrack {
id?: number
@ -97,4 +98,4 @@ export class Track implements ITrack {
}
return []
}
}
}

View file

@ -1,6 +1,7 @@
<script setup lang="ts">
import { ref } from 'vue'
import { state } from '../state';
import router from '../router'
const name = ref("")
const password = ref("")
@ -9,10 +10,13 @@ async function signUp() {
const $name = name.value
const result = await fetch("/api/v1/auth", {
method: 'POST',
body: JSON.stringify({ name: $name, password: password.value })
body: JSON.stringify({ name: $name, password: password.value }),
headers: {'Content-Type': 'application/json'}
})
if (result.ok) {
state.user = { name: $name }
await state.repopulate()
router.push("/")
}
}
@ -20,10 +24,13 @@ async function login() {
const $name = name.value
const result = await fetch("/api/v1/auth", {
method: 'PUT',
body: JSON.stringify({ name: $name, password: password.value })
body: JSON.stringify({ name: $name, password: password.value }),
headers: {'Content-Type': 'application/json'}
})
if (result.ok) {
state.user = { name: $name }
await state.repopulate()
router.push("/")
}
}
@ -66,4 +73,4 @@ async function login() {
.button.submit {
margin-left: 10px;
}
</style>
</style>

View file

@ -1,5 +1,10 @@
<script setup lang="ts">
import Table from "../components/Table.vue";
import { state } from '../state.ts'
import router from '../router.ts'
if(!state.user) router.push('/login')
</script>
<template>