From c2609af9d8bac65d3e70b3860160ac8bfe097241 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 7 Dec 2025 18:33:16 +0900 Subject: feat(client): add delete card modal with confirmation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Completes Phase 5 card management by adding the ability to delete cards with a confirmation dialog. Includes unit tests for the modal component and integration tests for the delete flow in DeckDetailPage. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- src/client/components/DeleteCardModal.tsx | 161 ++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 src/client/components/DeleteCardModal.tsx (limited to 'src/client/components/DeleteCardModal.tsx') diff --git a/src/client/components/DeleteCardModal.tsx b/src/client/components/DeleteCardModal.tsx new file mode 100644 index 0000000..99abbd0 --- /dev/null +++ b/src/client/components/DeleteCardModal.tsx @@ -0,0 +1,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(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 ( +
{ + if (e.target === e.currentTarget) { + handleClose(); + } + }} + onKeyDown={(e) => { + if (e.key === "Escape") { + handleClose(); + } + }} + > +
+

+ Delete Card +

+ + {error && ( +
+ {error} +
+ )} + +

Are you sure you want to delete this card?

+

"{displayFront}"

+

This action cannot be undone.

+ +
+ + +
+
+
+ ); +} -- cgit v1.2.3-70-g09d2