show sets on dashboard

This commit is contained in:
Yisroel Baum 2026-08-03 20:31:59 +03:00
parent 2072166bdd
commit 321c1b7bb0
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
4 changed files with 221 additions and 21 deletions

View file

@ -0,0 +1,65 @@
import { ref } from 'vue'
import { defineStore } from 'pinia'
import { z } from 'zod'
import { API_BASE } from '@/utils/apiBase'
export const setSummarySchema = z.object({
id: z.number().int().positive(),
name: z.string().min(1),
})
const setsResponseSchema = z.object({
sets: z.array(setSummarySchema),
})
export type SetSummary = z.infer<typeof setSummarySchema>
const LOAD_ERROR = "We couldn't load the available sets."
export const useSetsStore = defineStore('sets', () => {
const sets = ref<SetSummary[]>([])
const loading = ref(false)
const error = ref<string | null>(null)
async function fetchSets(): Promise<boolean> {
loading.value = true
error.value = null
try {
const response = await fetch(`${API_BASE}/api/sets`, {
method: 'GET',
credentials: 'include',
headers: {
Accept: 'application/json',
},
})
if (response.status !== 200) {
sets.value = []
error.value = LOAD_ERROR
return false
}
const responseBody: unknown = await response.json()
sets.value = setsResponseSchema.parse(responseBody).sets
return true
} catch {
sets.value = []
error.value = LOAD_ERROR
return false
} finally {
loading.value = false
}
}
return {
sets,
loading,
error,
fetchSets,
}
})