aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/pages/DeckDetailPage.tsx
blob: c713ab07cf1a11db96b9dea194072eee5caf36af (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { useCallback, useEffect, useState } from "react";
import { Link, useParams } from "wouter";
import { ApiClientError, apiClient } from "../api";

interface Card {
	id: string;
	deckId: string;
	front: string;
	back: string;
	state: number;
	due: string;
	reps: number;
	lapses: number;
	createdAt: string;
	updatedAt: string;
}

interface Deck {
	id: string;
	name: string;
	description: string | null;
}

const CardStateLabels: Record<number, string> = {
	0: "New",
	1: "Learning",
	2: "Review",
	3: "Relearning",
};

export function DeckDetailPage() {
	const { deckId } = useParams<{ deckId: string }>();
	const [deck, setDeck] = useState<Deck | null>(null);
	const [cards, setCards] = useState<Card[]>([]);
	const [isLoading, setIsLoading] = useState(true);
	const [error, setError] = useState<string | null>(null);

	const fetchDeck = useCallback(async () => {
		if (!deckId) return;

		const authHeader = apiClient.getAuthHeader();
		if (!authHeader) {
			throw new ApiClientError("Not authenticated", 401);
		}

		const res = await fetch(`/api/decks/${deckId}`, {
			headers: authHeader,
		});

		if (!res.ok) {
			const errorBody = await res.json().catch(() => ({}));
			throw new ApiClientError(
				(errorBody as { error?: string }).error ||
					`Request failed with status ${res.status}`,
				res.status,
			);
		}

		const data = await res.json();
		setDeck(data.deck);
	}, [deckId]);

	const fetchCards = useCallback(async () => {
		if (!deckId) return;

		const authHeader = apiClient.getAuthHeader();
		if (!authHeader) {
			throw new ApiClientError("Not authenticated", 401);
		}

		const res = await fetch(`/api/decks/${deckId}/cards`, {
			headers: authHeader,
		});

		if (!res.ok) {
			const errorBody = await res.json().catch(() => ({}));
			throw new ApiClientError(
				(errorBody as { error?: string }).error ||
					`Request failed with status ${res.status}`,
				res.status,
			);
		}

		const data = await res.json();
		setCards(data.cards);
	}, [deckId]);

	const fetchData = useCallback(async () => {
		setIsLoading(true);
		setError(null);

		try {
			await Promise.all([fetchDeck(), fetchCards()]);
		} catch (err) {
			if (err instanceof ApiClientError) {
				setError(err.message);
			} else {
				setError("Failed to load data. Please try again.");
			}
		} finally {
			setIsLoading(false);
		}
	}, [fetchDeck, fetchCards]);

	useEffect(() => {
		fetchData();
	}, [fetchData]);

	if (!deckId) {
		return (
			<div>
				<p>Invalid deck ID</p>
				<Link href="/">Back to decks</Link>
			</div>
		);
	}

	return (
		<div>
			<header style={{ marginBottom: "1rem" }}>
				<Link href="/" style={{ textDecoration: "none" }}>
					&larr; Back to Decks
				</Link>
			</header>

			{isLoading && <p>Loading...</p>}

			{error && (
				<div role="alert" style={{ color: "red" }}>
					{error}
					<button
						type="button"
						onClick={fetchData}
						style={{ marginLeft: "0.5rem" }}
					>
						Retry
					</button>
				</div>
			)}

			{!isLoading && !error && deck && (
				<main>
					<div style={{ marginBottom: "1.5rem" }}>
						<h1 style={{ margin: 0 }}>{deck.name}</h1>
						{deck.description && (
							<p style={{ margin: "0.5rem 0 0 0", color: "#666" }}>
								{deck.description}
							</p>
						)}
					</div>

					<div
						style={{
							display: "flex",
							justifyContent: "space-between",
							alignItems: "center",
							marginBottom: "1rem",
						}}
					>
						<h2 style={{ margin: 0 }}>Cards ({cards.length})</h2>
					</div>

					{cards.length === 0 && (
						<div>
							<p>This deck has no cards yet.</p>
							<p>Add cards to start studying!</p>
						</div>
					)}

					{cards.length > 0 && (
						<ul style={{ listStyle: "none", padding: 0 }}>
							{cards.map((card) => (
								<li
									key={card.id}
									style={{
										border: "1px solid #ccc",
										padding: "1rem",
										marginBottom: "0.5rem",
										borderRadius: "4px",
									}}
								>
									<div
										style={{
											display: "flex",
											justifyContent: "space-between",
											alignItems: "flex-start",
										}}
									>
										<div style={{ flex: 1, minWidth: 0 }}>
											<div
												style={{
													display: "flex",
													gap: "1rem",
													marginBottom: "0.5rem",
												}}
											>
												<div style={{ flex: 1, minWidth: 0 }}>
													<strong>Front:</strong>
													<p
														style={{
															margin: "0.25rem 0 0 0",
															whiteSpace: "pre-wrap",
															wordBreak: "break-word",
														}}
													>
														{card.front}
													</p>
												</div>
												<div style={{ flex: 1, minWidth: 0 }}>
													<strong>Back:</strong>
													<p
														style={{
															margin: "0.25rem 0 0 0",
															whiteSpace: "pre-wrap",
															wordBreak: "break-word",
														}}
													>
														{card.back}
													</p>
												</div>
											</div>
											<div
												style={{
													display: "flex",
													gap: "1rem",
													fontSize: "0.875rem",
													color: "#666",
												}}
											>
												<span>
													State: {CardStateLabels[card.state] || "Unknown"}
												</span>
												<span>Reviews: {card.reps}</span>
												<span>Lapses: {card.lapses}</span>
											</div>
										</div>
									</div>
								</li>
							))}
						</ul>
					)}
				</main>
			)}
		</div>
	);
}