2023-06-14 11:39:00 -04:00
|
|
|
import { error } from "./error"
|
|
|
|
|
|
|
|
export interface ITrack {
|
|
|
|
id: number
|
|
|
|
name: String
|
|
|
|
description: String
|
|
|
|
icon: String
|
|
|
|
enabled: number
|
|
|
|
multiple_entries_per_day?: number
|
|
|
|
color?: number
|
|
|
|
order?: number
|
|
|
|
ticks?: Array<Tick>
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Track implements ITrack {
|
|
|
|
id: number
|
|
|
|
name: String
|
|
|
|
description: String
|
|
|
|
icon: String
|
|
|
|
enabled: number
|
|
|
|
multiple_entries_per_day?: number
|
|
|
|
color?: number
|
|
|
|
order?: number
|
|
|
|
ticks?: Array<Tick>
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
id: number,
|
|
|
|
name: String,
|
|
|
|
description: String,
|
|
|
|
icon: String,
|
|
|
|
enabled: number,
|
|
|
|
multiple_entries_per_day?: number,
|
|
|
|
color?: number,
|
|
|
|
order?: number,
|
|
|
|
ticks?: Array<ITick>
|
|
|
|
) {
|
|
|
|
this.id = id
|
|
|
|
this.name = name
|
|
|
|
this.description = description
|
|
|
|
this.icon = icon
|
|
|
|
this.enabled = enabled
|
|
|
|
this.multiple_entries_per_day = multiple_entries_per_day
|
|
|
|
this.color = color
|
|
|
|
this.order = order
|
|
|
|
this.ticks = ticks?.map(tick => Tick.fromJSON(tick))
|
|
|
|
this.isSetOn = this.isSetOn.bind(this)
|
|
|
|
this.fetchTicks = this.fetchTicks.bind(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromJSON(track: ITrack): Track {
|
|
|
|
return new Track(track.id, track.name, track.description, track.icon, track.enabled, track.multiple_entries_per_day, track.color, track.order)
|
|
|
|
}
|
|
|
|
|
|
|
|
isSetOn(date: Date): boolean {
|
|
|
|
for (var tick of (this.ticks ?? [])) {
|
|
|
|
if (
|
|
|
|
date.getUTCFullYear() == tick.year &&
|
2023-06-20 08:40:35 -04:00
|
|
|
(date.getUTCMonth() + 1) == tick.month &&
|
|
|
|
// Javascript Date ^^^ uses 0-index for dates of months 🤦
|
2023-06-14 11:39:00 -04:00
|
|
|
date.getDate() == tick.day
|
|
|
|
) return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchTicks(): Promise<Track> {
|
|
|
|
const response = await fetch(`/api/v1/tracks/${this.id}/ticks`)
|
|
|
|
if (response.ok) {
|
|
|
|
this.ticks = await response.json()
|
|
|
|
} else {
|
|
|
|
throw new Error(`error fetching ticks: ${response.statusText} (${response.status})`)
|
|
|
|
}
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
static async fetchAll(): Promise<Array<Track>> {
|
|
|
|
const result = await fetch('/api/v1/tracks')
|
|
|
|
if (result.ok) {
|
|
|
|
try {
|
|
|
|
const body = await result.text();
|
|
|
|
try {
|
|
|
|
const tracks = Array.prototype.map.call(JSON.parse(body), Track.fromJSON) as Array<Track>
|
|
|
|
return Promise.all(tracks.map((track: Track) => track.fetchTicks()))
|
|
|
|
} catch (e) {
|
|
|
|
console.error('error parsing body from JSON')
|
|
|
|
console.error(e)
|
|
|
|
console.debug(body)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
result.text()
|
|
|
|
.then(console.debug)
|
|
|
|
.catch(console.error)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error(`error fetching tracks: ${result.statusText} (${result.status})`)
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|