diff options
Diffstat (limited to 'src/client/atoms/decks.ts')
| -rw-r--r-- | src/client/atoms/decks.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/client/atoms/decks.ts b/src/client/atoms/decks.ts new file mode 100644 index 0000000..57abef4 --- /dev/null +++ b/src/client/atoms/decks.ts @@ -0,0 +1,37 @@ +import { apiClient } from "../api/client"; +import { createReloadableAtom, createReloadableAtomFamily } from "./utils"; + +export interface Deck { + id: string; + name: string; + description: string | null; + newCardsPerDay: number; + createdAt: string; + updatedAt: string; +} + +// ===================== +// Decks List - Suspense-compatible +// ===================== + +export const decksAtom = createReloadableAtom(async () => { + const res = await apiClient.rpc.api.decks.$get(undefined, { + headers: apiClient.getAuthHeader(), + }); + const data = await apiClient.handleResponse<{ decks: Deck[] }>(res); + return data.decks; +}); + +// ===================== +// Single Deck by ID - Suspense-compatible +// ===================== + +export const deckByIdAtomFamily = createReloadableAtomFamily( + async (deckId: string) => { + const res = await apiClient.rpc.api.decks[":id"].$get({ + param: { id: deckId }, + }); + const data = await apiClient.handleResponse<{ deck: Deck }>(res); + return data.deck; + }, +); |
