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
|
import { useState } from "react";
import { ApiClientError, apiClient } from "../api";
interface Card {
id: string;
front: string;
}
interface DeleteCardModalProps {
isOpen: boolean;
deckId: string;
card: Card | null;
onClose: () => void;
onCardDeleted: () => void;
}
export function DeleteCardModal({
isOpen,
deckId,
card,
onClose,
onCardDeleted,
}: DeleteCardModalProps) {
const [error, setError] = useState<string | null>(null);
const [isDeleting, setIsDeleting] = useState(false);
const handleClose = () => {
setError(null);
onClose();
};
const handleDelete = async () => {
if (!card) return;
setError(null);
setIsDeleting(true);
try {
const authHeader = apiClient.getAuthHeader();
if (!authHeader) {
throw new ApiClientError("Not authenticated", 401);
}
const res = await fetch(`/api/decks/${deckId}/cards/${card.id}`, {
method: "DELETE",
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,
);
}
onCardDeleted();
onClose();
} catch (err) {
if (err instanceof ApiClientError) {
setError(err.message);
} else {
setError("Failed to delete card. Please try again.");
}
} finally {
setIsDeleting(false);
}
};
if (!isOpen || !card) {
return null;
}
// Truncate front text for display if too long
const displayFront =
card.front.length > 50 ? `${card.front.slice(0, 50)}...` : card.front;
return (
<div
role="dialog"
aria-modal="true"
aria-labelledby="delete-card-title"
style={{
position: "fixed",
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: "rgba(0, 0, 0, 0.5)",
display: "flex",
alignItems: "center",
justifyContent: "center",
zIndex: 1000,
}}
onClick={(e) => {
if (e.target === e.currentTarget) {
handleClose();
}
}}
onKeyDown={(e) => {
if (e.key === "Escape") {
handleClose();
}
}}
>
<div
style={{
backgroundColor: "white",
padding: "1.5rem",
borderRadius: "8px",
width: "100%",
maxWidth: "400px",
margin: "1rem",
}}
>
<h2 id="delete-card-title" style={{ marginTop: 0 }}>
Delete Card
</h2>
{error && (
<div role="alert" style={{ color: "red", marginBottom: "1rem" }}>
{error}
</div>
)}
<p>Are you sure you want to delete this card?</p>
<p style={{ color: "#666", fontStyle: "italic" }}>"{displayFront}"</p>
<p style={{ color: "#666" }}>This action cannot be undone.</p>
<div
style={{
display: "flex",
gap: "0.5rem",
justifyContent: "flex-end",
marginTop: "1.5rem",
}}
>
<button type="button" onClick={handleClose} disabled={isDeleting}>
Cancel
</button>
<button
type="button"
onClick={handleDelete}
disabled={isDeleting}
style={{
backgroundColor: "#dc3545",
color: "white",
border: "none",
padding: "0.5rem 1rem",
borderRadius: "4px",
cursor: isDeleting ? "not-allowed" : "pointer",
}}
>
{isDeleting ? "Deleting..." : "Delete"}
</button>
</div>
</div>
</div>
);
}
|