Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
438 changes: 282 additions & 156 deletions bun.lock

Large diffs are not rendered by default.

18 changes: 4 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,10 @@
"e2e:ui": "playwright test --ui"
},
"dependencies": {
"@hookform/resolvers": "^3.10.0",
"@plaiceholder/next": "^3.0.0",
"@radix-ui/react-accordion": "^1.2.12",
"@radix-ui/react-checkbox": "^1.3.3",
"@radix-ui/react-dialog": "^1.1.15",
"@radix-ui/react-dropdown-menu": "^2.1.16",
"@radix-ui/react-icons": "^1.3.2",
"@radix-ui/react-label": "^2.1.8",
"@radix-ui/react-popover": "^1.1.15",
"@radix-ui/react-radio-group": "^1.3.8",
"@radix-ui/react-scroll-area": "^1.2.10",
"@radix-ui/react-slot": "^1.2.4",
"@radix-ui/react-toast": "^1.2.15",
"@radix-ui/react-tooltip": "^1.2.8",
"@t3-oss/env-nextjs": "^0.13.10",
"@tanstack/react-form": "^1.32.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"cmdk": "^1.1.1",
Expand All @@ -52,9 +41,9 @@
"next-plausible": "^3.12.5",
"next-themes": "^0.4.6",
"plaiceholder": "^3.0.0",
"radix-ui": "^1.4.3",
"react": "19.2.5",
"react-dom": "19.2.5",
"react-hook-form": "^7.68.0",
"react-icons": "^5.5.0",
"reading-time": "^1.5.0",
"recma-mdx-import-react": "^1.2.2",
Expand All @@ -67,6 +56,7 @@
"remark-math": "^6.0.0",
"remark-smartypants": "^3.0.2",
"rss": "^1.2.2",
"sonner": "^2.0.7",
"tailwind-merge": "^2.6.0",
"tailwindcss-animate": "^1.0.7",
"tw-animate-css": "^1.4.0",
Expand Down Expand Up @@ -96,7 +86,7 @@
"jsdom": "^29.1.1",
"postcss": "^8.5.6",
"rehype-parse": "^9.0.0",
"rehype-stringify": "^9.0.0",
"rehype-stringify": "^10.0.1",
"tailwindcss": "^4.2.1",
"typescript": "5.8.2",
"vitest": "4.1.4"
Expand Down
219 changes: 105 additions & 114 deletions src/app/admin/guestbook.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
"use client";

import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "@tanstack/react-form";
import Link from "next/link";
import { useForm } from "react-hook-form";
import type z from "zod";

import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { useToast } from "@/components/ui/use-toast";
Field,
FieldDescription,
FieldError,
FieldGroup,
FieldLabel,
FieldLegend,
FieldSet,
} from "@/components/ui/field";
import { deleteGuestbookEntryDataFormSchema } from "@/config/schema";
import type { EntryData } from "@/config/types";
import { deleteGuestbookEntriesAction } from "@/lib/server-helper";
Expand All @@ -30,112 +27,106 @@ import { cn } from "@/lib/utils";
* @param props.entries The list of guestbook entries to manage.
*/
export function GuestbookAdminForm({ entries }: { entries: EntryData[] }) {
const { toast } = useToast();

const form = useForm<z.infer<typeof deleteGuestbookEntryDataFormSchema>>({
resolver: zodResolver(deleteGuestbookEntryDataFormSchema),
const form = useForm({
defaultValues: {
items: [],
items: [] as number[],
},
});

const onSubmit = async (
values: z.infer<typeof deleteGuestbookEntryDataFormSchema>,
) => {
const deleted = await deleteGuestbookEntriesAction({
entries: values.items,
});
if (!deleted.ok) {
toast({
title: "Error deleting entries.",
description: deleted.error.message,
variant: "destructive",
});
return;
} else {
toast({
title: "Entries deleted.",
description: "The selected entries have been deleted successfully.",
variant: "default",
validators: {
onSubmit: deleteGuestbookEntryDataFormSchema,
},
onSubmit: async ({ value }) => {
const deleted = await deleteGuestbookEntriesAction({
entries: value.items,
});
}
form.reset();
};
if (!deleted.ok) {
toast.error(`Error deleting entries. ${deleted.error.message}`);
return;
} else {
toast.info("The selected entries have been deleted successfully.");
}
form.reset();
},
});

return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="items"
render={() => (
<FormItem>
<FormLabel className="text-foreground">Delete Entries</FormLabel>
<FormDescription>
<form
onSubmit={(e) => {
e.preventDefault();
e.stopPropagation();
form.handleSubmit();
}}
className="space-y-4"
>
<form.Field name="items" mode="array">
{(field) => {
const isInvalid =
field.state.meta.isTouched && !field.state.meta.isValid;
return (
<FieldSet>
<FieldLegend variant="label" className="text-foreground">
Delete Entries
</FieldLegend>
<FieldDescription>
Delete the entries that you do not want on guestbook.
</FormDescription>
{entries.map((entry) => (
<FormField
key={entry.id}
control={form.control}
name="items"
render={({ field }) => {
return (
<FormItem
key={entry.id}
className="flex flex-row items-start space-x-3 space-y-0"
>
<FormControl>
<Checkbox
id={entry.id.toString()}
className="mt-1"
checked={field.value?.includes(entry.id)}
onCheckedChange={(checked) => {
return checked
? field.onChange([...field.value, entry.id])
: field.onChange(
field.value?.filter(
(value) => value !== entry.id,
),
);
}}
/>
</FormControl>
<FormLabel className="text-md font-normal">
<div className="wrap-break-word">
<span
className={cn(
"mr-1 font-bold tracking-tight",
entry.color === "" || entry.color === null
? "text-foreground"
: `text-${entry.color}`,
)}
>
{entry.createdBy}:
</span>
<span className="text-foreground">
{entry.body}
</span>
</div>
</FormLabel>
</FormItem>
);
}}
/>
))}
<FormMessage />
</FormItem>
)}
/>
<div className="flex flex-row items-center gap-2">
<Button type="submit" size="default" variant="destructive">
Delete Entries
</Button>
<Button asChild variant="ghost" size="default">
<Link href="/guestbook">Return to Guest Book</Link>
</Button>
</div>
</form>
</Form>
</FieldDescription>
<FieldGroup data-slot="checkbox-group">
{entries.map((entry) => (
<Field
key={entry.id}
orientation="horizontal"
data-invalid={isInvalid}
className="grid-cols-[auto_1fr] space-x-3 gap-0"
>
<Checkbox
id={entry.id.toString()}
className="mt-1"
aria-invalid={isInvalid}
checked={field.state.value.includes(entry.id)}
onCheckedChange={(checked) => {
if (checked) {
field.pushValue(entry.id);
} else {
const index = field.state.value.indexOf(entry.id);
if (index > -1) {
field.removeValue(index);
}
}
}}
/>
<FieldLabel
htmlFor={entry.id.toString()}
className="text-md font-normal cursor-pointer"
>
<div className="wrap-break-word">
<span
className={cn(
"mr-1 font-bold tracking-tight",
entry.color === "" || entry.color === null
? "text-foreground"
: `text-${entry.color}`,
)}
>
{entry.createdBy}:
</span>
<span className="text-foreground">{entry.body}</span>
</div>
</FieldLabel>
</Field>
))}
</FieldGroup>
{isInvalid && <FieldError errors={field.state.meta.errors} />}
</FieldSet>
);
}}
</form.Field>
<div className="flex flex-row items-center gap-2">
<Button type="submit" size="default" variant="destructive">
Delete Entries
</Button>
<Button asChild variant="ghost" size="default">
<Link href="/guestbook">Return to Guest Book</Link>
</Button>
</div>
</form>
);
}
Loading
Loading