added seed data; image uploads via server actions; infinite article grid
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import CategoryCard from "@/components/category/category-card";
|
||||
import { Category } from "@/server/db/schema";
|
||||
import React from "react";
|
||||
import { Skeleton } from "../ui/skeleton";
|
||||
|
||||
const GRID_CLASS = "grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3";
|
||||
|
||||
function CategoriesGrid({ categories }: { categories: Category[] }) {
|
||||
return (
|
||||
<menu className={GRID_CLASS}>
|
||||
{categories.map((category) => (
|
||||
<li key={category.id}>
|
||||
<CategoryCard {...category} />
|
||||
</li>
|
||||
))}
|
||||
</menu>
|
||||
);
|
||||
}
|
||||
|
||||
export default CategoriesGrid;
|
||||
|
||||
export function CategoriesGridSkeleton() {
|
||||
const range = Array.from(new Array(6).keys());
|
||||
return (
|
||||
<ul className={GRID_CLASS}>
|
||||
{range.map((i) => (
|
||||
<li key={i}>
|
||||
<Skeleton className="h-12 w-full" />
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { appRoutes } from "@/config";
|
||||
import { Category } from "@/server/db/schema";
|
||||
import Link from "next/link";
|
||||
import React from "react";
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardFooter,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from "@/components/ui/card";
|
||||
import { AspectRatio } from "../ui/aspect-ratio";
|
||||
import { CATEGORY_BANNER_ASPECT_RATIO } from "./category-form";
|
||||
import Image from "next/image";
|
||||
import { Icons } from "../icons";
|
||||
|
||||
function CategoryCard({
|
||||
name,
|
||||
slug,
|
||||
createdAt,
|
||||
image,
|
||||
}: Pick<Category, "name" | "slug" | "createdAt" | "image">) {
|
||||
return (
|
||||
<Link href={appRoutes.category(slug)}>
|
||||
<Card className="overflow-hidden">
|
||||
{/* <AspectRatio
|
||||
className="flex items-center justify-center bg-muted text-muted-foreground"
|
||||
ratio={CATEGORY_BANNER_ASPECT_RATIO}
|
||||
>
|
||||
{image?.length ? (
|
||||
<Image
|
||||
src={image}
|
||||
alt="Kategorie-bild"
|
||||
fill
|
||||
className="object-cover"
|
||||
/>
|
||||
) : (
|
||||
<Icons.logo />
|
||||
)}
|
||||
</AspectRatio> */}
|
||||
<CardHeader>
|
||||
<CardTitle>{name}</CardTitle>
|
||||
</CardHeader>
|
||||
</Card>
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
|
||||
export default CategoryCard;
|
||||
@@ -0,0 +1,120 @@
|
||||
"use client";
|
||||
import React from "react";
|
||||
import { z } from "zod";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormDescription,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { categorySchema } from "@/lib/validation/zod/category";
|
||||
import { Category } from "@/server/db/schema";
|
||||
import { Textarea } from "../ui/textarea";
|
||||
import { updateCategory } from "@/server/actions/category";
|
||||
import { toast } from "sonner";
|
||||
import ImageUploadButton from "../image-upload-form";
|
||||
import Image from "next/image";
|
||||
import { AspectRatio } from "../ui/aspect-ratio";
|
||||
|
||||
export const CATEGORY_BANNER_ASPECT_RATIO = 20 / 3;
|
||||
|
||||
function CategoryForm({ server_category }: { server_category: Category }) {
|
||||
const form = useForm<z.infer<typeof categorySchema>>({
|
||||
resolver: zodResolver(categorySchema),
|
||||
defaultValues: {
|
||||
name: server_category?.name ?? "",
|
||||
description: server_category?.description ?? "",
|
||||
image: server_category?.image ?? "",
|
||||
},
|
||||
});
|
||||
|
||||
// 2. Define a submit handler.
|
||||
async function onSubmit(values: z.infer<typeof categorySchema>) {
|
||||
const success = await updateCategory(values, server_category.id);
|
||||
if (success) {
|
||||
toast.success("Kategorie gespeichert.");
|
||||
form.reset();
|
||||
} else toast.error("Speichern fehlgeschlagen.");
|
||||
}
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="name"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Name der Kategorie</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="Name der Kategorie" {...field} />
|
||||
</FormControl>
|
||||
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="description"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Beschreibung der Kategorie</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
placeholder="Das ist eine kurze beschreibung"
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="image"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Kategorie Banner</FormLabel>
|
||||
<FormControl>
|
||||
<ImageUploadButton
|
||||
onUploaded={(url) => {
|
||||
field.onChange(url);
|
||||
form.handleSubmit(onSubmit)();
|
||||
}}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
|
||||
{field.value?.length ? (
|
||||
<AspectRatio
|
||||
ratio={CATEGORY_BANNER_ASPECT_RATIO}
|
||||
className="w-full max-w-lg bg-muted"
|
||||
>
|
||||
<Image
|
||||
src={field.value}
|
||||
alt="Kategorie-bild"
|
||||
fill
|
||||
className="h-full w-full rounded-md object-cover"
|
||||
/>
|
||||
</AspectRatio>
|
||||
) : null}
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<Button type="submit" disabled={!form.formState.isDirty}>
|
||||
Kategorie Speichern
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
|
||||
export default CategoryForm;
|
||||
@@ -0,0 +1,81 @@
|
||||
"use client";
|
||||
import React from "react";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog";
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { categorySchema } from "@/lib/validation/zod/category";
|
||||
import { createCategory } from "@/server/actions/category";
|
||||
|
||||
const formSchema = categorySchema.pick({
|
||||
name: true,
|
||||
});
|
||||
|
||||
function CreateCategoryDialog() {
|
||||
const [open, setOpen] = React.useState<boolean>(false);
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: {
|
||||
name: "",
|
||||
},
|
||||
});
|
||||
|
||||
// 2. Define a submit handler.
|
||||
async function onSubmit(values: z.infer<typeof formSchema>) {
|
||||
setOpen(false);
|
||||
await createCategory(values);
|
||||
}
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<Button asChild>
|
||||
<DialogTrigger className="w-full">Kategorie Erstellen</DialogTrigger>
|
||||
</Button>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>Neue Kategorie Erstellen</DialogTitle>
|
||||
<DialogDescription></DialogDescription>
|
||||
</DialogHeader>
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="name"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Name</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="Kategorie Name" {...field} />
|
||||
</FormControl>
|
||||
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<Button type="submit">Artikel Erstellen</Button>
|
||||
</form>
|
||||
</Form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
export default CreateCategoryDialog;
|
||||
Reference in New Issue
Block a user