From 9ee6febd638ef88bcfd0253ca5e146a10094888a Mon Sep 17 00:00:00 2001 From: Carlos Pavajeau Date: Tue, 14 May 2024 18:18:46 -0500 Subject: [PATCH] feat: add update employee --- .../dashboard/employees/columns.tsx | 7 + .../dashboard/stores/update.modal.tsx | 136 ++++++++++++++++++ src/server/api/routers/employees.ts | 13 +- src/server/api/schemas/employees.ts | 7 + 4 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 src/app/(dashboard)/dashboard/stores/update.modal.tsx diff --git a/src/app/(dashboard)/dashboard/employees/columns.tsx b/src/app/(dashboard)/dashboard/employees/columns.tsx index f3ae8846..644719c4 100644 --- a/src/app/(dashboard)/dashboard/employees/columns.tsx +++ b/src/app/(dashboard)/dashboard/employees/columns.tsx @@ -1,6 +1,7 @@ "use client"; import { type ColumnDef } from "@tanstack/react-table"; +import UpdateEmployeeModal from "~/app/(dashboard)/dashboard/stores/update.modal"; import { type RouterOutputs } from "~/trpc/shared"; export type Employee = RouterOutputs["employee"]["byStore"][number]; @@ -18,4 +19,10 @@ export const columns: ColumnDef[] = [ accessorKey: "employee.phone", header: "Teléfono", }, + { + id: "actions", + cell: ({ row }) => { + return ; + }, + }, ]; diff --git a/src/app/(dashboard)/dashboard/stores/update.modal.tsx b/src/app/(dashboard)/dashboard/stores/update.modal.tsx new file mode 100644 index 00000000..e9f11d6c --- /dev/null +++ b/src/app/(dashboard)/dashboard/stores/update.modal.tsx @@ -0,0 +1,136 @@ +import { zodResolver } from "@hookform/resolvers/zod"; +import { RotateCw, SquarePen } from "lucide-react"; +import { useEffect, useState } from "react"; +import { useForm, type SubmitHandler } from "react-hook-form"; +import type { TypeOf } from "zod"; +import { Button } from "~/components/ui/button"; +import { + Dialog, + DialogContent, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "~/components/ui/dialog"; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "~/components/ui/form"; +import { Input } from "~/components/ui/input"; +import { useToast } from "~/components/ui/use-toast"; +import { updateEmployeeInput } from "~/server/api/schemas/employees"; +import { api } from "~/trpc/react"; +import type { RouterOutputs } from "~/trpc/shared"; + +type Props = { + employee: NonNullable; +}; + +type FormValues = TypeOf; + +const UpdateEmployeeModal = ({ employee }: Props) => { + const [isOpen, setIsOpen] = useState(false); + const form = useForm({ + defaultValues: { + id: employee.employee.id, + name: employee.employee.name, + email: employee.employee.email, + phone: employee.employee.phone ?? undefined, + }, + resolver: zodResolver(updateEmployeeInput), + }); + + const updateEmployee = api.employee.update.useMutation(); + const onSubmit: SubmitHandler = (values) => { + updateEmployee.mutate(values); + }; + + const utils = api.useUtils(); + const { toast } = useToast(); + useEffect(() => { + if (updateEmployee.isSuccess) { + toast({ + title: "Éxito", + description: "Empleado actualizado correctamente", + }); + + void utils.employee.byStore.invalidate(); + setIsOpen(false); + } + }, [updateEmployee.isSuccess]); + + return ( + + + + + + + Editar empleado + +
+ + ( + + Nombre + + + + + + + )} + /> + + ( + + Correo + + + + + + + )} + /> + + ( + + Teléfono + + + + + + + )} + /> + + + + +
+
+ ); +}; + +export default UpdateEmployeeModal; diff --git a/src/server/api/routers/employees.ts b/src/server/api/routers/employees.ts index dd8be3b5..a116c427 100644 --- a/src/server/api/routers/employees.ts +++ b/src/server/api/routers/employees.ts @@ -2,7 +2,10 @@ import { eq } from "drizzle-orm"; import { z } from "zod"; import { byStoreInput } from "~/server/api/schemas/common"; -import { createEmployeeInput } from "~/server/api/schemas/employees"; +import { + createEmployeeInput, + updateEmployeeInput, +} from "~/server/api/schemas/employees"; import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc"; import { employees, employeeStore } from "~/server/db/schema"; @@ -62,4 +65,12 @@ export const employeesRouter = createTRPCRouter({ where: eq(employeeStore.storeId, input.storeId), }); }), + update: protectedProcedure + .input(updateEmployeeInput) + .mutation(async ({ ctx, input }) => { + await ctx.db + .update(employees) + .set(input) + .where(eq(employees.id, input.id)); + }), }); diff --git a/src/server/api/schemas/employees.ts b/src/server/api/schemas/employees.ts index 24754cad..01bd30bc 100644 --- a/src/server/api/schemas/employees.ts +++ b/src/server/api/schemas/employees.ts @@ -7,3 +7,10 @@ export const createEmployeeInput = z.object({ phone: z.string().max(32).optional(), storeId: z.string().min(1).max(36), }); + +export const updateEmployeeInput = z.object({ + id: z.string().min(1).max(32), + name: z.string().min(1).max(128), + email: z.string().max(255), + phone: z.string().max(32).optional(), +});