Files
DungeonsDragons/frontend/src/components/CharacterCard.tsx
T
Thorsten a58730fce7 Let owners delete their games and characters, with confirmation
Add DELETE /api/games/{id} (creator-only) and DELETE
/api/characters/{id} (owner-only), each 403ing for anyone else.

Deleting a game relies on the existing cascade FKs (game_participants,
messages, world_state, adventure_chunks all cascade on games.id) — a
plain session.delete() is enough.

Deleting a character is trickier: game_participants.character_id and
messages.character_id reference it with no ON DELETE clause (a
character can outlive a game and vice versa), so deleting one that's
been played would hit a FK violation. Null out both references first
— game and message history stay intact, just detached from the
now-gone character, same as how a participant with no character
selected already renders.

Frontend: a reusable ConfirmDialog component, a "Löschen" button on
GameCard/CharacterCard visible only to the owner (checked against
useAuth()'s user id), wired through GamesList/CharactersList so the
list updates locally after a successful delete.

Verified: isolated backend test proved the character delete's FK
nulling works without violation and preserves the game/message rows;
live in the browser, deleting a game removed it from the list with no
orphaned rows left in any of the four related tables, and the
character delete dialog's cancel path correctly leaves everything
untouched. Ownership scoping confirmed live too — no delete button
appears on other users' games.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-03 12:14:48 +02:00

61 lines
1.8 KiB
TypeScript

import { useState } from "react";
import { Link } from "react-router-dom";
import { Character } from "../api/characters";
import { useAuth } from "../hooks/useAuth";
import ConfirmDialog from "./ConfirmDialog";
export default function CharacterCard({
character,
onDelete,
}: {
character: Character;
onDelete?: (id: string) => void;
}) {
const { user } = useAuth();
const [confirming, setConfirming] = useState(false);
const isOwner = user?.id === character.owner_id;
function handleDeleteClick(e: React.MouseEvent) {
e.preventDefault();
e.stopPropagation();
setConfirming(true);
}
return (
<>
<Link
to={`/characters/${character.id}`}
className="block rounded-lg border border-slate-800 bg-slate-900 p-4 hover:border-amber-500"
>
<div className="flex items-start justify-between gap-4">
<h2 className="text-lg font-semibold text-slate-100">{character.name}</h2>
{isOwner && onDelete && (
<button
onClick={handleDeleteClick}
className="shrink-0 rounded px-2 py-1 text-xs text-red-400 hover:bg-red-500/10 hover:text-red-300"
>
Löschen
</button>
)}
</div>
<p className="text-sm text-slate-400">
{character.race ?? "?"} · {character.char_class ?? "?"} · Stufe {character.level}
</p>
</Link>
<ConfirmDialog
open={confirming}
title="Charakter löschen?"
message={`„${character.name}" wird endgültig gelöscht. Spiele, in denen der Charakter mitgespielt hat, bleiben erhalten, verlieren aber die Verknüpfung zu ihm.`}
confirmLabel="Endgültig löschen"
onCancel={() => setConfirming(false)}
onConfirm={() => {
setConfirming(false);
onDelete?.(character.id);
}}
/>
</>
);
}