2023-06-17 13:20:34 -04:00
|
|
|
import { reactive } from "vue"
|
|
|
|
import { Track } from "./track"
|
|
|
|
import { error } from "./error"
|
|
|
|
|
2023-06-20 08:40:35 -04:00
|
|
|
enum State {
|
|
|
|
Unfetched,
|
|
|
|
Fetching,
|
|
|
|
Fetched,
|
|
|
|
}
|
|
|
|
|
2023-06-17 13:20:34 -04:00
|
|
|
export const state = reactive({
|
|
|
|
tracks: new Array<Track>,
|
2023-06-20 08:40:35 -04:00
|
|
|
state: State.Unfetched,
|
|
|
|
streamUpdatesFromServer() {
|
|
|
|
const source = new EventSource("/api/v1/updates")
|
|
|
|
source.addEventListener("open", () => console.debug("opened event source"))
|
|
|
|
source.addEventListener('message', event => console.log(event))
|
|
|
|
source.addEventListener('TickAdded', event => {
|
|
|
|
console.log(event)
|
|
|
|
const tick: Tick = JSON.parse(event.data)
|
2023-06-20 08:54:03 -04:00
|
|
|
// for (const track of this.tracks) {
|
2023-06-20 08:40:35 -04:00
|
|
|
// if (track.id === tick.track_id) {
|
2023-06-20 08:54:03 -04:00
|
|
|
// console.debug('pushing tick')
|
|
|
|
// track.ticks?.push(tick)
|
2023-06-20 08:40:35 -04:00
|
|
|
// }
|
2023-06-20 08:54:03 -04:00
|
|
|
// }
|
|
|
|
const tracks = this.tracks.map(track => {
|
|
|
|
if (track.id === tick.track_id) {
|
|
|
|
const ticks = track.ticks ?? []
|
|
|
|
ticks.push(tick)
|
|
|
|
track.ticks = ticks
|
|
|
|
|
|
|
|
}
|
|
|
|
return track
|
|
|
|
})
|
|
|
|
console.debug(tracks)
|
|
|
|
this.tracks = tracks
|
2023-06-20 08:40:35 -04:00
|
|
|
})
|
|
|
|
source.addEventListener('TickDropped', event => {
|
|
|
|
console.log(event)
|
|
|
|
const tick: Tick = JSON.parse(event.data)
|
2023-06-20 08:54:03 -04:00
|
|
|
// for (const track of this.tracks)
|
|
|
|
// if (track.id === tick.track_id)
|
2023-06-20 08:40:35 -04:00
|
|
|
// track.ticks = track.ticks?.filter($tick => $tick.id === tick.id)
|
2023-06-20 08:54:03 -04:00
|
|
|
const tracks = this.tracks.map(track => {
|
|
|
|
if (track.id === tick.track_id) {
|
|
|
|
track.ticks = track.ticks?.filter($tick => $tick.id !== tick.id)
|
|
|
|
}
|
|
|
|
return track
|
|
|
|
})
|
|
|
|
console.debug(tracks)
|
|
|
|
this.tracks = tracks
|
2023-06-20 08:40:35 -04:00
|
|
|
})
|
|
|
|
source.addEventListener('Lagged', event => {
|
|
|
|
console.log(event)
|
|
|
|
// Refresh the page, refetching the list of tracks and ticks
|
|
|
|
window.location = window.location
|
|
|
|
})
|
|
|
|
source.addEventListener('error', event => {
|
|
|
|
error(event)
|
|
|
|
window.location = window.location
|
|
|
|
})
|
|
|
|
},
|
|
|
|
async repopulate() {
|
|
|
|
this.state = State.Fetching
|
2023-06-17 13:20:34 -04:00
|
|
|
this.tracks = await Track.fetchAll()
|
|
|
|
},
|
2023-06-20 08:40:35 -04:00
|
|
|
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> {
|
|
|
|
let query = new URLSearchParams()
|
|
|
|
query.append("year", date.getUTCFullYear().toString())
|
|
|
|
query.append("month", (date.getUTCMonth() + 1).toString())
|
|
|
|
// good thing I still had this ^^^^^^^^^^^^^^ in mind when I wrote this 😬
|
|
|
|
query.append("day", date.getUTCDate().toString())
|
|
|
|
const response: Response = await fetch(`/api/v1/tracks/${track.id}/ticked?${query.toString()}`, { method: "PATCH" })
|
|
|
|
const body = await response.text()
|
|
|
|
if (!response.ok) {
|
2023-06-17 13:20:34 -04:00
|
|
|
error(body)
|
2023-06-20 08:40:35 -04:00
|
|
|
throw new Error(`error setting tick for track ${track.id} ("${track.name}"): ${response.status} ${response.statusText}`)
|
2023-06-17 13:20:34 -04:00
|
|
|
}
|
2023-06-20 08:40:35 -04:00
|
|
|
return JSON.parse(body)
|
2023-06-17 13:20:34 -04:00
|
|
|
},
|
2023-06-20 08:40:35 -04:00
|
|
|
async taskMarkedIncomplete(track: Track) {
|
|
|
|
const { ok, status, statusText } = await fetch(`/api/v1/tracks/${track.id}/all-ticks`, { method: 'DELETE' })
|
2023-06-17 13:20:34 -04:00
|
|
|
if (!ok)
|
2023-06-20 08:40:35 -04:00
|
|
|
error(`error deleting ticks for ${track.id}: ${statusText} (${status})`)
|
2023-06-17 13:20:34 -04:00
|
|
|
}
|
2023-06-20 08:40:35 -04:00
|
|
|
})
|