aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/atoms/decks.ts
blob: e9b0d03e881877a01d2907147c74be64f10d736a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { apiClient } from "../api/client";
import { createReloadableAtom, createReloadableAtomFamily } from "./utils";

export interface Deck {
	id: string;
	name: string;
	description: string | null;
	newCardsPerDay: number;
	dueCardCount: 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;
	},
);