prod
This commit is contained in:
@@ -15,25 +15,44 @@ import Link from "next/link";
|
||||
import { useState } from "react";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { PageType } from "@/lib/pages/types";
|
||||
import { useAction } from "@/hooks/useAction";
|
||||
|
||||
type Draft = { id: string; type: PageType; title: string; slug: string; updatedAt: Date | null };
|
||||
|
||||
export default function DraftsDialog({ drafts }: { drafts: Draft[]}) {
|
||||
const [isDeletingId, setIsDeletingId] = useState("");
|
||||
function DraftItem({ draft, onDeleted }: { draft: Draft; onDeleted: (id: string) => void }) {
|
||||
const { run, isPending } = useAction(deletePageAction, {
|
||||
successMessage: "Draft deleted",
|
||||
onSuccess: () => onDeleted(draft.id),
|
||||
});
|
||||
|
||||
async function deletePage(id: string, type: PageType) {
|
||||
setIsDeletingId(id);
|
||||
try {
|
||||
await deletePageAction(id, type);
|
||||
} finally {
|
||||
setIsDeletingId("");
|
||||
}
|
||||
return (
|
||||
<div className="w-full flex flex-row items-center justify-between">
|
||||
<h3 className="w-64 truncate">
|
||||
{draft.title}
|
||||
</h3>
|
||||
<div className="flex flex-row items-center gap-2">
|
||||
<Link href={`/news/${draft.slug}/edit`}>
|
||||
<Button variant="secondary" size="icon-sm"><PencilIcon /></Button>
|
||||
</Link>
|
||||
<Button disabled={isPending} variant="secondary" size="icon-sm" onClick={() => run(draft.id, draft.type)}>
|
||||
{isPending ? <Spinner /> : <XIcon />}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function DraftsDialog({ drafts }: { drafts: Draft[]}) {
|
||||
const [items, setItems] = useState(drafts);
|
||||
|
||||
function handleDeleted(id: string) {
|
||||
setItems((prev) => prev.filter((d) => d.id !== id));
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog>
|
||||
<DialogTrigger asChild>
|
||||
<Button variant="secondary" className="flex flex-row items-center gap-2">Drafts<div className="flex items-center justify-center bg-black/50 text-white w-3.5 h-3.5 text-xs rounded-full">{drafts.length}</div></Button>
|
||||
<Button variant="secondary" className="flex flex-row items-center gap-2">Drafts<div className="flex items-center justify-center bg-black/50 text-white w-3.5 h-3.5 text-xs rounded-full">{items.length}</div></Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-md">
|
||||
<DialogHeader>
|
||||
@@ -41,22 +60,10 @@ export default function DraftsDialog({ drafts }: { drafts: Draft[]}) {
|
||||
<DialogDescription>Here are drafts of your pages</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
{drafts.length > 0 ? (
|
||||
{items.length > 0 ? (
|
||||
<div className="flex flex-col items-center gap-2">
|
||||
{drafts.map((draft) => (
|
||||
<div className="w-full flex flex-row items-center justify-between" key={draft.id}>
|
||||
<h3 className="w-64 truncate">
|
||||
{draft.title}
|
||||
</h3>
|
||||
<div className="flex flex-row items-center gap-2">
|
||||
<Link href={`/news/${draft.slug}/edit`}>
|
||||
<Button variant="secondary" size="icon-sm"><PencilIcon /></Button>
|
||||
</Link>
|
||||
<Button disabled={isDeletingId === draft.id} variant="secondary" size="icon-sm" onClick={() => deletePage(draft.id, draft.type)}>
|
||||
{isDeletingId === draft.id ? <Spinner /> : <XIcon />}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
{items.map((draft) => (
|
||||
<DraftItem key={draft.id} draft={draft} onDeleted={handleDeleted} />
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
@@ -65,4 +72,4 @@ export default function DraftsDialog({ drafts }: { drafts: Draft[]}) {
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user