From c6a819c11b9bd600289da173b2a25f76723a79f1 Mon Sep 17 00:00:00 2001 From: Citali Date: Sun, 6 Jul 2025 10:25:20 +0200 Subject: [PATCH] Change mode toggle. Add type edit --- .../items/commissions/types/updateType.ts | 46 ++++++++ .../commissions/types/[id]/edit/page.tsx | 34 ++++++ src/components/global/ModeToggle.tsx | 17 ++- src/components/global/TopNav.tsx | 5 + .../items/commissions/types/EditTypeForm.tsx | 105 ++++++++++++++++++ .../items/commissions/types/ListTypes.tsx | 14 ++- .../types/form/CommissionExtraField.tsx | 9 +- .../types/form/CommissionOptionField.tsx | 9 +- 8 files changed, 233 insertions(+), 6 deletions(-) create mode 100644 src/actions/items/commissions/types/updateType.ts create mode 100644 src/app/items/commissions/types/[id]/edit/page.tsx create mode 100644 src/components/items/commissions/types/EditTypeForm.tsx diff --git a/src/actions/items/commissions/types/updateType.ts b/src/actions/items/commissions/types/updateType.ts new file mode 100644 index 0000000..30195ea --- /dev/null +++ b/src/actions/items/commissions/types/updateType.ts @@ -0,0 +1,46 @@ +"use server" + +import prisma from "@/lib/prisma" +import { commissionTypeSchema } from "@/schemas/commissionType" +import * as z from "zod/v4" + +export async function updateCommissionType( + id: string, + rawData: z.infer +) { + const data = commissionTypeSchema.parse(rawData) + + const updated = await prisma.commissionType.update({ + where: { id }, + data: { + name: data.name, + description: data.description, + options: { + deleteMany: {}, + create: data.options?.map((opt, index) => ({ + option: { connect: { id: opt.optionId } }, + price: opt.price ?? null, + pricePercent: opt.pricePercent ?? null, + priceRange: opt.priceRange ?? null, + sortIndex: index, + })), + }, + extras: { + deleteMany: {}, + create: data.extras?.map((ext, index) => ({ + extra: { connect: { id: ext.extraId } }, + price: ext.price ?? null, + pricePercent: ext.pricePercent ?? null, + priceRange: ext.priceRange ?? null, + sortIndex: index, + })), + }, + }, + include: { + options: true, + extras: true, + }, + }) + + return updated +} diff --git a/src/app/items/commissions/types/[id]/edit/page.tsx b/src/app/items/commissions/types/[id]/edit/page.tsx new file mode 100644 index 0000000..5af5e96 --- /dev/null +++ b/src/app/items/commissions/types/[id]/edit/page.tsx @@ -0,0 +1,34 @@ +import EditTypeForm from "@/components/items/commissions/types/EditTypeForm"; +import prisma from "@/lib/prisma"; + +export default async function CommissionTypesEditPage({ params }: { params: { id: string } }) { + const { id } = await params; + const commissionType = await prisma.commissionType.findUnique({ + where: { + id, + }, + include: { + options: { include: { option: true }, orderBy: { sortIndex: "asc" } }, + extras: { include: { extra: true }, orderBy: { sortIndex: "asc" } }, + }, + }) + const options = await prisma.commissionOption.findMany({ + orderBy: [{ sortIndex: "asc" }, { name: "asc" }], + }); + const extras = await prisma.commissionExtra.findMany({ + orderBy: [{ sortIndex: "asc" }, { name: "asc" }], + }) + + if (!commissionType) { + return
Type not found
+ } + + return ( +
+
+

New Commission Type

+
+ +
+ ); +} \ No newline at end of file diff --git a/src/components/global/ModeToggle.tsx b/src/components/global/ModeToggle.tsx index 77d19fc..ff7544d 100644 --- a/src/components/global/ModeToggle.tsx +++ b/src/components/global/ModeToggle.tsx @@ -8,7 +8,7 @@ import { useEffect, useState } from "react" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../ui/select" const modes = ["light", "dark"] as const -const accents = ["zinc", "red", "rose", "orange", "green", "blue", "yellow", "violet", "default"] as const +const accents = ["zinc", "red", "rose", "orange", "green", "blue", "yellow", "violet"] as const const modeIcons = { light: , @@ -16,7 +16,7 @@ const modeIcons = { } export default function ModeToggle() { - const { setTheme, theme, resolvedTheme } = useTheme() + const { setTheme, theme } = useTheme() const [mode, setMode] = useState("dark") const [accent, setAccent] = useState("violet") @@ -33,6 +33,17 @@ export default function ModeToggle() { setTheme(fullTheme) } + const accentColorMap: Record = { + zinc: "text-zinc-600", + red: "text-red-600", + rose: "text-rose-600", + orange: "text-orange-600", + green: "text-green-600", + blue: "text-blue-600", + yellow: "text-yellow-600", + violet: "text-violet-600", + } + return (
+ + The name of the commission type. + + + )} + /> + ( + + Description + + + + Optional description. + + + )} + /> + + + + +
+ + +
+ + +
+ ); +} \ No newline at end of file diff --git a/src/components/items/commissions/types/ListTypes.tsx b/src/components/items/commissions/types/ListTypes.tsx index 589ee76..530b3a2 100644 --- a/src/components/items/commissions/types/ListTypes.tsx +++ b/src/components/items/commissions/types/ListTypes.tsx @@ -2,7 +2,7 @@ import { updateCommissionTypeSortOrder } from "@/actions/items/commissions/types/updateCommissionTypeSortOrder"; import SortableItemCard from "@/components/drag/SortableItemCard"; -import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; +import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import { CommissionExtra, CommissionOption, CommissionType, CommissionTypeExtra, CommissionTypeOption } from "@/generated/prisma"; import { closestCenter, @@ -17,6 +17,8 @@ import { rectSortingStrategy, SortableContext } from "@dnd-kit/sortable"; +import { PencilIcon } from "lucide-react"; +import Link from "next/link"; import { useEffect, useState } from "react"; type CommissionTypeWithItems = CommissionType & { @@ -66,6 +68,7 @@ export default function ListTypes({ types }: { types: CommissionTypeWithItems[] {type.name} {type.description} +
@@ -103,6 +106,15 @@ export default function ListTypes({ types }: { types: CommissionTypeWithItems[]
+ + + + Edit + + ))} diff --git a/src/components/items/commissions/types/form/CommissionExtraField.tsx b/src/components/items/commissions/types/form/CommissionExtraField.tsx index fa14594..f0bbd6b 100644 --- a/src/components/items/commissions/types/form/CommissionExtraField.tsx +++ b/src/components/items/commissions/types/form/CommissionExtraField.tsx @@ -24,7 +24,7 @@ import { SortableContext, verticalListSortingStrategy } from "@dnd-kit/sortable" -import { useState } from "react" +import { useEffect, useState } from "react" import { useFieldArray, useFormContext } from "react-hook-form" import { ComboboxCreateable } from "./ComboboxCreateable" @@ -33,12 +33,17 @@ type Props = { } export function CommissionExtraField({ extras: initialExtras }: Props) { + const [mounted, setMounted] = useState(false) const { control } = useFormContext() const { fields, append, remove, move } = useFieldArray({ control, name: "extras", }) + useEffect(() => { + setMounted(true) + }, []) + const [extras, setExtras] = useState(initialExtras) const sensors = useSensors(useSensor(PointerSensor)) @@ -58,6 +63,8 @@ export function CommissionExtraField({ extras: initialExtras }: Props) { } } + if (!mounted) return null + return (

Extras

diff --git a/src/components/items/commissions/types/form/CommissionOptionField.tsx b/src/components/items/commissions/types/form/CommissionOptionField.tsx index 4778fad..9853c49 100644 --- a/src/components/items/commissions/types/form/CommissionOptionField.tsx +++ b/src/components/items/commissions/types/form/CommissionOptionField.tsx @@ -24,7 +24,7 @@ import { SortableContext, verticalListSortingStrategy } from "@dnd-kit/sortable" -import { useState } from "react" +import { useEffect, useState } from "react" import { useFieldArray, useFormContext } from "react-hook-form" import { ComboboxCreateable } from "./ComboboxCreateable" @@ -33,12 +33,17 @@ type Props = { } export function CommissionOptionField({ options: initialOptions }: Props) { + const [mounted, setMounted] = useState(false) const { control } = useFormContext() const { fields, append, remove, move } = useFieldArray({ control, name: "options", }) + useEffect(() => { + setMounted(true) + }, []) + const [options, setOptions] = useState(initialOptions) const sensors = useSensors(useSensor(PointerSensor)) @@ -58,6 +63,8 @@ export function CommissionOptionField({ options: initialOptions }: Props) { } } + if (!mounted) return null + return (

Options