show today's assignments

This commit is contained in:
Yisroel Baum 2026-08-15 22:02:19 +03:00
parent e1d2262579
commit 85c9f4bbc9
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 282 additions and 5 deletions

View file

@ -26,6 +26,15 @@ const scheduleAssignmentSchema = z.object({
}),
})
export const assignmentForDateSchema = scheduleAssignmentSchema.extend({
schedule: z.object({
id: z.number().int().positive(),
set: z.object({
name: z.string().min(1),
}),
}),
})
export const scheduleDetailSchema = scheduleSummarySchema.extend({
days: z.array(
z.object({
@ -43,12 +52,18 @@ const scheduleResponseSchema = z.object({
schedule: scheduleDetailSchema,
})
const assignmentsForDateResponseSchema = z.object({
date: isoDateSchema,
assignments: z.array(assignmentForDateSchema),
})
const errorResponseSchema = z.object({
error: z.string().min(1),
})
export type ScheduleSummary = z.infer<typeof scheduleSummarySchema>
export type ScheduleDetail = z.infer<typeof scheduleDetailSchema>
export type AssignmentForDate = z.infer<typeof assignmentForDateSchema>
export type CreateScheduleInput = {
setId: number
levelId: number
@ -59,6 +74,7 @@ export type CreateScheduleInput = {
const LIST_ERROR = "We couldn't load your schedules."
const DETAIL_ERROR = "We couldn't load this schedule."
const CREATE_ERROR = "We couldn't create this schedule."
const ASSIGNMENTS_ERROR = "We couldn't load today's assignments."
export const useSchedulesStore = defineStore('schedules', () => {
const schedules = ref<ScheduleSummary[]>([])
@ -70,7 +86,11 @@ export const useSchedulesStore = defineStore('schedules', () => {
const detailNotFound = ref(false)
const creating = ref(false)
const createError = ref<string | null>(null)
const assignmentsForDate = ref<AssignmentForDate[]>([])
const assignmentsLoading = ref(false)
const assignmentsError = ref<string | null>(null)
let activeDetailRequestId = 0
let assignmentsRequestId = 0
async function fetchSchedules(): Promise<boolean> {
listLoading.value = true
@ -202,6 +222,58 @@ export const useSchedulesStore = defineStore('schedules', () => {
}
}
async function fetchAssignmentsForDate(date: string): Promise<boolean> {
const requestId = ++assignmentsRequestId
assignmentsForDate.value = []
assignmentsLoading.value = true
assignmentsError.value = null
try {
const query = new URLSearchParams({ date })
const response = await fetch(`${API_BASE}/api/assignments?${query.toString()}`, {
method: 'GET',
credentials: 'include',
headers: {
Accept: 'application/json',
},
})
if (requestId !== assignmentsRequestId) {
return false
}
if (response.status !== 200) {
assignmentsError.value = ASSIGNMENTS_ERROR
return false
}
const responseBody: unknown = await response.json()
if (requestId !== assignmentsRequestId) {
return false
}
const parsedResponse = assignmentsForDateResponseSchema.parse(responseBody)
if (parsedResponse.date !== date) {
throw new Error('assignment response did not match requested date')
}
assignmentsForDate.value = parsedResponse.assignments
return true
} catch {
if (requestId === assignmentsRequestId) {
assignmentsForDate.value = []
assignmentsError.value = ASSIGNMENTS_ERROR
}
return false
} finally {
if (requestId === assignmentsRequestId) {
assignmentsLoading.value = false
}
}
}
return {
schedules,
listLoading,
@ -212,8 +284,12 @@ export const useSchedulesStore = defineStore('schedules', () => {
detailNotFound,
creating,
createError,
assignmentsForDate,
assignmentsLoading,
assignmentsError,
fetchSchedules,
fetchSchedule,
createSchedule,
fetchAssignmentsForDate,
}
})