added delete
This commit is contained in:
@@ -125,34 +125,51 @@ export function InputGroupDemo({ onSent }): {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function NotesList({ notes }: { notes: Note[] }) {
|
function NotesList({
|
||||||
|
notes,
|
||||||
|
onDelete,
|
||||||
|
deletingIds,
|
||||||
|
}: {
|
||||||
|
notes: Note[];
|
||||||
|
onDelete?: (id: string) => void;
|
||||||
|
deletingIds?: Set<string>;
|
||||||
|
}) {
|
||||||
if (!notes.length) {
|
if (!notes.length) {
|
||||||
return <p className="text-center">No notes yet.</p>;
|
return <p className="text-center">No notes yet.</p>;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
{notes.map((note) => (
|
{notes.map((note) => {
|
||||||
<Card key={note.id}>
|
const isDeleting = deletingIds?.has(note.id) ?? false;
|
||||||
<CardHeader className="justify-end-safe">
|
return (
|
||||||
<Button size="icon-sm" aria-label="Delete note" title="Delete note">
|
<Card key={note.id}>
|
||||||
<Trash2Icon />
|
<CardHeader className="justify-end-safe">
|
||||||
</Button>
|
<Button
|
||||||
</CardHeader>
|
size="icon-sm"
|
||||||
<CardContent>
|
aria-label="Delete note"
|
||||||
<ReactMarkdown remarkPlugins={[remarkGfm]}>
|
title="Delete note"
|
||||||
{note.content}
|
onClick={() => onDelete?.(note.id)}
|
||||||
</ReactMarkdown>
|
disabled={isDeleting}
|
||||||
</CardContent>
|
>
|
||||||
<CardFooter>
|
<Trash2Icon />
|
||||||
<p className="text-sm text-muted-foreground">
|
</Button>
|
||||||
{note.created_at
|
</CardHeader>
|
||||||
? new Date(note.created_at).toLocaleString()
|
<CardContent>
|
||||||
: "Unknown"}
|
<ReactMarkdown remarkPlugins={[remarkGfm]}>
|
||||||
</p>
|
{note.content}
|
||||||
</CardFooter>
|
</ReactMarkdown>
|
||||||
</Card>
|
</CardContent>
|
||||||
))}
|
<CardFooter>
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
{note.created_at
|
||||||
|
? new Date(note.created_at).toLocaleString()
|
||||||
|
: "Unknown"}
|
||||||
|
</p>
|
||||||
|
</CardFooter>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
})}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -161,6 +178,7 @@ function App() {
|
|||||||
const [notes, setNotes] = useState<Note[]>([]);
|
const [notes, setNotes] = useState<Note[]>([]);
|
||||||
const [loading, setLoading] = useState<boolean>(false);
|
const [loading, setLoading] = useState<boolean>(false);
|
||||||
const [loadError, setLoadError] = useState<string | null>(null);
|
const [loadError, setLoadError] = useState<string | null>(null);
|
||||||
|
const [deletingIds, setDeletingIds] = useState<Set<string>>(new Set());
|
||||||
|
|
||||||
async function fetchUserNotes() {
|
async function fetchUserNotes() {
|
||||||
const resp = await fetch(`${baseUrl}/content/${username}`);
|
const resp = await fetch(`${baseUrl}/content/${username}`);
|
||||||
@@ -193,6 +211,34 @@ function App() {
|
|||||||
loadNotes();
|
loadNotes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleDelete(id: string) {
|
||||||
|
if (deletingIds.has(id)) return;
|
||||||
|
|
||||||
|
const prevNotes = notes;
|
||||||
|
setNotes((prev) => prev.filter((n) => n.id !== id));
|
||||||
|
setDeletingIds((prev) => {
|
||||||
|
const next = new Set(prev);
|
||||||
|
next.add(id);
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fetch(`${baseUrl}/content/${id}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
});
|
||||||
|
setNotes((prev) => prev.filter((n) => n.id !== id));
|
||||||
|
} catch (e) {
|
||||||
|
const msg = e instanceof Error ? e.message : "Unknown error";
|
||||||
|
setLoadError(msg);
|
||||||
|
} finally {
|
||||||
|
setDeletingIds((prev) => {
|
||||||
|
const next = new Set(prev);
|
||||||
|
next.delete(id);
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadNotes();
|
loadNotes();
|
||||||
}, []);
|
}, []);
|
||||||
@@ -215,7 +261,11 @@ function App() {
|
|||||||
Failed to load notes: {loadError}
|
Failed to load notes: {loadError}
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
<NotesList notes={notes} />
|
<NotesList
|
||||||
|
notes={notes}
|
||||||
|
onDelete={handleDelete}
|
||||||
|
deletingIds={deletingIds}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user