aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-12-07 18:03:04 +0900
committernsfisis <nsfisis@gmail.com>2025-12-07 18:03:04 +0900
commit83789dd12efe82b645445fbb46d98bcb2a003b57 (patch)
tree984040fe8408b6e3dafcfac933c0495d719559c8
parent0b7acece277f80f1baeb7bb419544cdd11f7817f (diff)
downloadkioku-83789dd12efe82b645445fbb46d98bcb2a003b57.tar.gz
kioku-83789dd12efe82b645445fbb46d98bcb2a003b57.tar.zst
kioku-83789dd12efe82b645445fbb46d98bcb2a003b57.zip
feat(client): add delete deck modal with confirmation
Add DeleteDeckModal component that prompts users for confirmation before deleting a deck. Includes warning about permanent deletion and all associated cards. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
-rw-r--r--docs/dev/roadmap.md2
-rw-r--r--src/client/components/DeleteDeckModal.test.tsx266
-rw-r--r--src/client/components/DeleteDeckModal.tsx159
-rw-r--r--src/client/pages/HomePage.tsx35
4 files changed, 454 insertions, 8 deletions
diff --git a/docs/dev/roadmap.md b/docs/dev/roadmap.md
index 86e9c05..f159084 100644
--- a/docs/dev/roadmap.md
+++ b/docs/dev/roadmap.md
@@ -87,7 +87,7 @@ Smaller features first to enable early MVP validation.
- [x] Deck list page (empty state, list view)
- [x] Create deck modal/form
- [x] Edit deck
-- [ ] Delete deck (with confirmation)
+- [x] Delete deck (with confirmation)
- [ ] Add tests
**✅ Milestone**: Users can create and manage decks
diff --git a/src/client/components/DeleteDeckModal.test.tsx b/src/client/components/DeleteDeckModal.test.tsx
new file mode 100644
index 0000000..ad1463d
--- /dev/null
+++ b/src/client/components/DeleteDeckModal.test.tsx
@@ -0,0 +1,266 @@
+/**
+ * @vitest-environment jsdom
+ */
+import { cleanup, render, screen, waitFor } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { apiClient } from "../api/client";
+
+vi.mock("../api/client", () => ({
+ apiClient: {
+ getAuthHeader: vi.fn(),
+ },
+ ApiClientError: class ApiClientError extends Error {
+ constructor(
+ message: string,
+ public status: number,
+ public code?: string,
+ ) {
+ super(message);
+ this.name = "ApiClientError";
+ }
+ },
+}));
+
+// Import after mock is set up
+import { DeleteDeckModal } from "./DeleteDeckModal";
+
+// Mock fetch globally
+const mockFetch = vi.fn();
+global.fetch = mockFetch;
+
+describe("DeleteDeckModal", () => {
+ const mockDeck = {
+ id: "deck-123",
+ name: "Test Deck",
+ };
+
+ const defaultProps = {
+ isOpen: true,
+ deck: mockDeck,
+ onClose: vi.fn(),
+ onDeckDeleted: vi.fn(),
+ };
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ vi.mocked(apiClient.getAuthHeader).mockReturnValue({
+ Authorization: "Bearer access-token",
+ });
+ });
+
+ afterEach(() => {
+ cleanup();
+ vi.restoreAllMocks();
+ });
+
+ it("does not render when closed", () => {
+ render(<DeleteDeckModal {...defaultProps} isOpen={false} />);
+
+ expect(screen.queryByRole("dialog")).toBeNull();
+ });
+
+ it("does not render when deck is null", () => {
+ render(<DeleteDeckModal {...defaultProps} deck={null} />);
+
+ expect(screen.queryByRole("dialog")).toBeNull();
+ });
+
+ it("renders modal when open with deck", () => {
+ render(<DeleteDeckModal {...defaultProps} />);
+
+ expect(screen.getByRole("dialog")).toBeDefined();
+ expect(screen.getByRole("heading", { name: "Delete Deck" })).toBeDefined();
+ expect(screen.getByRole("button", { name: "Cancel" })).toBeDefined();
+ expect(screen.getByRole("button", { name: "Delete" })).toBeDefined();
+ });
+
+ it("displays confirmation message with deck name", () => {
+ render(<DeleteDeckModal {...defaultProps} />);
+
+ expect(screen.getByText(/Are you sure you want to delete/)).toBeDefined();
+ expect(screen.getByText("Test Deck")).toBeDefined();
+ });
+
+ it("displays warning about permanent deletion", () => {
+ render(<DeleteDeckModal {...defaultProps} />);
+
+ expect(screen.getByText(/This action cannot be undone/)).toBeDefined();
+ });
+
+ it("calls onClose when Cancel is clicked", async () => {
+ const user = userEvent.setup();
+ const onClose = vi.fn();
+ render(<DeleteDeckModal {...defaultProps} onClose={onClose} />);
+
+ await user.click(screen.getByRole("button", { name: "Cancel" }));
+
+ expect(onClose).toHaveBeenCalledTimes(1);
+ });
+
+ it("calls onClose when clicking outside the modal", async () => {
+ const user = userEvent.setup();
+ const onClose = vi.fn();
+ render(<DeleteDeckModal {...defaultProps} onClose={onClose} />);
+
+ // Click on the backdrop (the dialog element itself)
+ const dialog = screen.getByRole("dialog");
+ await user.click(dialog);
+
+ expect(onClose).toHaveBeenCalledTimes(1);
+ });
+
+ it("does not call onClose when clicking inside the modal content", async () => {
+ const user = userEvent.setup();
+ const onClose = vi.fn();
+ render(<DeleteDeckModal {...defaultProps} onClose={onClose} />);
+
+ // Click on an element inside the modal
+ await user.click(screen.getByText("Test Deck"));
+
+ expect(onClose).not.toHaveBeenCalled();
+ });
+
+ it("deletes deck when Delete is clicked", async () => {
+ const user = userEvent.setup();
+ const onClose = vi.fn();
+ const onDeckDeleted = vi.fn();
+
+ mockFetch.mockResolvedValue({
+ ok: true,
+ json: async () => ({}),
+ });
+
+ render(
+ <DeleteDeckModal
+ isOpen={true}
+ deck={mockDeck}
+ onClose={onClose}
+ onDeckDeleted={onDeckDeleted}
+ />,
+ );
+
+ await user.click(screen.getByRole("button", { name: "Delete" }));
+
+ await waitFor(() => {
+ expect(mockFetch).toHaveBeenCalledWith("/api/decks/deck-123", {
+ method: "DELETE",
+ headers: {
+ Authorization: "Bearer access-token",
+ },
+ });
+ });
+
+ expect(onDeckDeleted).toHaveBeenCalledTimes(1);
+ expect(onClose).toHaveBeenCalledTimes(1);
+ });
+
+ it("shows loading state during deletion", async () => {
+ const user = userEvent.setup();
+
+ mockFetch.mockImplementation(() => new Promise(() => {})); // Never resolves
+
+ render(<DeleteDeckModal {...defaultProps} />);
+
+ await user.click(screen.getByRole("button", { name: "Delete" }));
+
+ expect(screen.getByRole("button", { name: "Deleting..." })).toBeDefined();
+ expect(screen.getByRole("button", { name: "Deleting..." })).toHaveProperty(
+ "disabled",
+ true,
+ );
+ expect(screen.getByRole("button", { name: "Cancel" })).toHaveProperty(
+ "disabled",
+ true,
+ );
+ });
+
+ it("displays API error message", async () => {
+ const user = userEvent.setup();
+
+ mockFetch.mockResolvedValue({
+ ok: false,
+ status: 404,
+ json: async () => ({ error: "Deck not found" }),
+ });
+
+ render(<DeleteDeckModal {...defaultProps} />);
+
+ await user.click(screen.getByRole("button", { name: "Delete" }));
+
+ await waitFor(() => {
+ expect(screen.getByRole("alert").textContent).toContain("Deck not found");
+ });
+ });
+
+ it("displays generic error on unexpected failure", async () => {
+ const user = userEvent.setup();
+
+ mockFetch.mockRejectedValue(new Error("Network error"));
+
+ render(<DeleteDeckModal {...defaultProps} />);
+
+ await user.click(screen.getByRole("button", { name: "Delete" }));
+
+ await waitFor(() => {
+ expect(screen.getByRole("alert").textContent).toContain(
+ "Failed to delete deck. Please try again.",
+ );
+ });
+ });
+
+ it("displays error when not authenticated", async () => {
+ const user = userEvent.setup();
+
+ vi.mocked(apiClient.getAuthHeader).mockReturnValue(undefined);
+
+ render(<DeleteDeckModal {...defaultProps} />);
+
+ await user.click(screen.getByRole("button", { name: "Delete" }));
+
+ await waitFor(() => {
+ expect(screen.getByRole("alert").textContent).toContain(
+ "Not authenticated",
+ );
+ });
+ });
+
+ it("clears error when modal is closed", async () => {
+ const user = userEvent.setup();
+ const onClose = vi.fn();
+
+ mockFetch.mockResolvedValue({
+ ok: false,
+ status: 404,
+ json: async () => ({ error: "Some error" }),
+ });
+
+ const { rerender } = render(
+ <DeleteDeckModal {...defaultProps} onClose={onClose} />,
+ );
+
+ // Trigger error
+ await user.click(screen.getByRole("button", { name: "Delete" }));
+ await waitFor(() => {
+ expect(screen.getByRole("alert")).toBeDefined();
+ });
+
+ // Close and reopen the modal
+ await user.click(screen.getByRole("button", { name: "Cancel" }));
+ rerender(<DeleteDeckModal {...defaultProps} onClose={onClose} />);
+
+ // Error should be cleared
+ expect(screen.queryByRole("alert")).toBeNull();
+ });
+
+ it("displays deck name correctly when changed", () => {
+ const { rerender } = render(<DeleteDeckModal {...defaultProps} />);
+
+ expect(screen.getByText("Test Deck")).toBeDefined();
+
+ const newDeck = { id: "deck-456", name: "Another Deck" };
+ rerender(<DeleteDeckModal {...defaultProps} deck={newDeck} />);
+
+ expect(screen.getByText("Another Deck")).toBeDefined();
+ });
+});
diff --git a/src/client/components/DeleteDeckModal.tsx b/src/client/components/DeleteDeckModal.tsx
new file mode 100644
index 0000000..307451c
--- /dev/null
+++ b/src/client/components/DeleteDeckModal.tsx
@@ -0,0 +1,159 @@
+import { useState } from "react";
+import { ApiClientError, apiClient } from "../api";
+
+interface Deck {
+ id: string;
+ name: string;
+}
+
+interface DeleteDeckModalProps {
+ isOpen: boolean;
+ deck: Deck | null;
+ onClose: () => void;
+ onDeckDeleted: () => void;
+}
+
+export function DeleteDeckModal({
+ isOpen,
+ deck,
+ onClose,
+ onDeckDeleted,
+}: DeleteDeckModalProps) {
+ const [error, setError] = useState<string | null>(null);
+ const [isDeleting, setIsDeleting] = useState(false);
+
+ const handleClose = () => {
+ setError(null);
+ onClose();
+ };
+
+ const handleDelete = async () => {
+ if (!deck) return;
+
+ setError(null);
+ setIsDeleting(true);
+
+ try {
+ const authHeader = apiClient.getAuthHeader();
+ if (!authHeader) {
+ throw new ApiClientError("Not authenticated", 401);
+ }
+
+ const res = await fetch(`/api/decks/${deck.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,
+ );
+ }
+
+ onDeckDeleted();
+ onClose();
+ } catch (err) {
+ if (err instanceof ApiClientError) {
+ setError(err.message);
+ } else {
+ setError("Failed to delete deck. Please try again.");
+ }
+ } finally {
+ setIsDeleting(false);
+ }
+ };
+
+ if (!isOpen || !deck) {
+ return null;
+ }
+
+ return (
+ <div
+ role="dialog"
+ aria-modal="true"
+ aria-labelledby="delete-deck-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-deck-title" style={{ marginTop: 0 }}>
+ Delete Deck
+ </h2>
+
+ {error && (
+ <div role="alert" style={{ color: "red", marginBottom: "1rem" }}>
+ {error}
+ </div>
+ )}
+
+ <p>
+ Are you sure you want to delete <strong>{deck.name}</strong>?
+ </p>
+ <p style={{ color: "#666" }}>
+ This action cannot be undone. All cards in this deck will also be
+ deleted.
+ </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>
+ );
+}
diff --git a/src/client/pages/HomePage.tsx b/src/client/pages/HomePage.tsx
index 08eccaa..a51dfc1 100644
--- a/src/client/pages/HomePage.tsx
+++ b/src/client/pages/HomePage.tsx
@@ -1,6 +1,7 @@
import { useCallback, useEffect, useState } from "react";
import { ApiClientError, apiClient } from "../api";
import { CreateDeckModal } from "../components/CreateDeckModal";
+import { DeleteDeckModal } from "../components/DeleteDeckModal";
import { EditDeckModal } from "../components/EditDeckModal";
import { useAuth } from "../stores";
@@ -20,6 +21,7 @@ export function HomePage() {
const [error, setError] = useState<string | null>(null);
const [isCreateModalOpen, setIsCreateModalOpen] = useState(false);
const [editingDeck, setEditingDeck] = useState<Deck | null>(null);
+ const [deletingDeck, setDeletingDeck] = useState<Deck | null>(null);
const fetchDecks = useCallback(async () => {
setIsLoading(true);
@@ -136,13 +138,25 @@ export function HomePage() {
</p>
)}
</div>
- <button
- type="button"
- onClick={() => setEditingDeck(deck)}
- style={{ marginLeft: "1rem" }}
- >
- Edit
- </button>
+ <div style={{ display: "flex", gap: "0.5rem" }}>
+ <button type="button" onClick={() => setEditingDeck(deck)}>
+ Edit
+ </button>
+ <button
+ type="button"
+ onClick={() => setDeletingDeck(deck)}
+ style={{
+ backgroundColor: "#dc3545",
+ color: "white",
+ border: "none",
+ padding: "0.25rem 0.5rem",
+ borderRadius: "4px",
+ cursor: "pointer",
+ }}
+ >
+ Delete
+ </button>
+ </div>
</div>
</li>
))}
@@ -162,6 +176,13 @@ export function HomePage() {
onClose={() => setEditingDeck(null)}
onDeckUpdated={fetchDecks}
/>
+
+ <DeleteDeckModal
+ isOpen={deletingDeck !== null}
+ deck={deletingDeck}
+ onClose={() => setDeletingDeck(null)}
+ onDeckDeleted={fetchDecks}
+ />
</div>
);
}