chore: add and edit roles is working now

This commit is contained in:
Pablo Lara
2024-12-10 10:44:34 +01:00
parent c1a8d47e5b
commit 577530ac69
4 changed files with 69 additions and 15 deletions
@@ -3,7 +3,7 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { Checkbox } from "@nextui-org/react";
import { SaveIcon } from "lucide-react";
import { useRouter, useSearchParams } from "next/navigation";
import { useRouter } from "next/navigation";
import { useEffect } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";
@@ -16,17 +16,19 @@ import { ApiError, editRoleFormSchema } from "@/types";
export type FormValues = z.infer<typeof editRoleFormSchema>;
export const EditRoleForm = ({ roleId }: { roleId: string }) => {
export const EditRoleForm = ({
roleId,
roleData,
}: {
roleId: string;
roleData: FormValues;
}) => {
const { toast } = useToast();
const router = useRouter();
const searchParams = useSearchParams();
const searchRoleId = searchParams.get("roleId") || roleId;
const form = useForm<FormValues>({
resolver: zodResolver(editRoleFormSchema),
defaultValues: {
roleId: roleId,
},
defaultValues: roleData,
});
const { watch, setValue } = form;
@@ -66,7 +68,7 @@ export const EditRoleForm = ({ roleId }: { roleId: string }) => {
};
const onSubmitClient = async (values: FormValues) => {
if (!searchRoleId) {
if (!roleId) {
toast({
variant: "destructive",
title: "Error",
@@ -89,7 +91,7 @@ export const EditRoleForm = ({ roleId }: { roleId: string }) => {
);
try {
const data = await updateRole(formData, searchRoleId);
const data = await updateRole(formData, roleId);
if (data?.errors && data.errors.length > 0) {
data.errors.forEach((error: ApiError) => {