aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/components/CreateCardModal.tsx
blob: 3913e829c1593f4f1d17e7e6536877bdc0480c19 (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
import { type FormEvent, useState } from "react";
import { ApiClientError, apiClient } from "../api";

interface CreateCardModalProps {
	isOpen: boolean;
	deckId: string;
	onClose: () => void;
	onCardCreated: () => void;
}

export function CreateCardModal({
	isOpen,
	deckId,
	onClose,
	onCardCreated,
}: CreateCardModalProps) {
	const [front, setFront] = useState("");
	const [back, setBack] = useState("");
	const [error, setError] = useState<string | null>(null);
	const [isSubmitting, setIsSubmitting] = useState(false);

	const resetForm = () => {
		setFront("");
		setBack("");
		setError(null);
	};

	const handleClose = () => {
		resetForm();
		onClose();
	};

	const handleSubmit = async (e: FormEvent) => {
		e.preventDefault();
		setError(null);
		setIsSubmitting(true);

		try {
			const res = await apiClient.rpc.api.decks[":deckId"].cards.$post(
				{
					param: { deckId },
					json: {
						front: front.trim(),
						back: back.trim(),
					},
				},
				{
					headers: apiClient.getAuthHeader(),
				},
			);

			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,
				);
			}

			resetForm();
			onCardCreated();
			onClose();
		} catch (err) {
			if (err instanceof ApiClientError) {
				setError(err.message);
			} else {
				setError("Failed to create card. Please try again.");
			}
		} finally {
			setIsSubmitting(false);
		}
	};

	if (!isOpen) {
		return null;
	}

	const isFormValid = front.trim() && back.trim();

	return (
		<div
			role="dialog"
			aria-modal="true"
			aria-labelledby="create-card-title"
			className="fixed inset-0 bg-ink/40 backdrop-blur-sm flex items-center justify-center z-50 p-4 animate-fade-in"
			onClick={(e) => {
				if (e.target === e.currentTarget) {
					handleClose();
				}
			}}
			onKeyDown={(e) => {
				if (e.key === "Escape") {
					handleClose();
				}
			}}
		>
			<div className="bg-white rounded-2xl shadow-xl w-full max-w-lg animate-scale-in">
				<div className="p-6">
					<h2
						id="create-card-title"
						className="font-display text-xl font-medium text-ink mb-6"
					>
						Create New Card
					</h2>

					<form onSubmit={handleSubmit} className="space-y-4">
						{error && (
							<div
								role="alert"
								className="bg-error/5 text-error text-sm px-4 py-3 rounded-lg border border-error/20"
							>
								{error}
							</div>
						)}

						<div>
							<label
								htmlFor="card-front"
								className="block text-sm font-medium text-slate mb-1.5"
							>
								Front
							</label>
							<textarea
								id="card-front"
								value={front}
								onChange={(e) => setFront(e.target.value)}
								required
								disabled={isSubmitting}
								rows={3}
								placeholder="Question or prompt"
								className="w-full px-4 py-2.5 bg-ivory border border-border rounded-lg text-slate placeholder-muted transition-all duration-200 hover:border-muted focus:border-primary focus:ring-2 focus:ring-primary/10 disabled:opacity-50 disabled:cursor-not-allowed resize-none"
							/>
						</div>

						<div>
							<label
								htmlFor="card-back"
								className="block text-sm font-medium text-slate mb-1.5"
							>
								Back
							</label>
							<textarea
								id="card-back"
								value={back}
								onChange={(e) => setBack(e.target.value)}
								required
								disabled={isSubmitting}
								rows={3}
								placeholder="Answer or explanation"
								className="w-full px-4 py-2.5 bg-ivory border border-border rounded-lg text-slate placeholder-muted transition-all duration-200 hover:border-muted focus:border-primary focus:ring-2 focus:ring-primary/10 disabled:opacity-50 disabled:cursor-not-allowed resize-none"
							/>
						</div>

						<div className="flex gap-3 justify-end pt-2">
							<button
								type="button"
								onClick={handleClose}
								disabled={isSubmitting}
								className="px-4 py-2 text-slate hover:bg-ivory rounded-lg transition-colors disabled:opacity-50"
							>
								Cancel
							</button>
							<button
								type="submit"
								disabled={isSubmitting || !isFormValid}
								className="px-4 py-2 bg-primary hover:bg-primary-dark text-white font-medium rounded-lg transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed"
							>
								{isSubmitting ? "Creating..." : "Create Card"}
							</button>
						</div>
					</form>
				</div>
			</div>
		</div>
	);
}