From 497ebc949f5aceba2dabca2412d0cbdbe687596f Mon Sep 17 00:00:00 2001 From: Rama Priyanto Date: Tue, 11 Feb 2025 13:01:48 +0700 Subject: [PATCH] fix:edit user --- .../admin/master-user/edit/[id]/page.tsx | 11 + components/form/form-master-user-edit.tsx | 421 ++++++++++++++++++ components/form/form-master-user.tsx | 1 + components/landing/BodyLayout.tsx | 2 +- components/landing/SidebarNav.tsx | 2 +- components/layout/sidebar/sidebar.tsx | 5 +- service/master-user.ts | 14 + service/third-party-service.ts | 4 +- 8 files changed, 455 insertions(+), 5 deletions(-) create mode 100644 app/(admin)/admin/master-user/edit/[id]/page.tsx create mode 100644 components/form/form-master-user-edit.tsx diff --git a/app/(admin)/admin/master-user/edit/[id]/page.tsx b/app/(admin)/admin/master-user/edit/[id]/page.tsx new file mode 100644 index 0000000..eb182b5 --- /dev/null +++ b/app/(admin)/admin/master-user/edit/[id]/page.tsx @@ -0,0 +1,11 @@ +import FormMasterUser from "@/components/form/form-master-user"; +import FormMasterUserEdit from "@/components/form/form-master-user-edit"; +import { Card } from "@nextui-org/react"; + +export default function CreateMasterUserPage() { + return ( + + + + ); +} diff --git a/components/form/form-master-user-edit.tsx b/components/form/form-master-user-edit.tsx new file mode 100644 index 0000000..e311887 --- /dev/null +++ b/components/form/form-master-user-edit.tsx @@ -0,0 +1,421 @@ +"use client"; +import { error } from "@/config/swal"; +import { + createMasterUser, + editMasterUsers, + getDetailMasterUsers, +} from "@/service/master-user"; +import { MasterUser } from "@/types/globals"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { + Button, + Card, + Input, + Radio, + RadioGroup, + Select, + SelectItem, + Selection, + Textarea, +} from "@nextui-org/react"; +import Link from "next/link"; +import { useParams, useRouter } from "next/navigation"; +import React, { useEffect, useState } from "react"; +import { Controller, useForm } from "react-hook-form"; +import Datepicker from "react-tailwindcss-datepicker"; +import Swal from "sweetalert2"; +import withReactContent from "sweetalert2-react-content"; +import { z } from "zod"; +import { EyeFilledIcon, EyeSlashFilledIcon } from "../icons"; +import ReactPasswordChecklist from "react-password-checklist"; + +const masterUserSchema = z.object({ + fullname: z.string().min(1, { message: "Required" }), + username: z.string().min(1, { message: "Required" }), + email: z.string().min(1, { message: "Required" }), + identityNumber: z.string().min(1, { message: "Required" }), + genderType: z.string().min(1, { message: "Required" }), + phoneNumber: z.string().min(1, { message: "Required" }), + address: z.string().min(1, { message: "Required" }), +}); + +export default function FormMasterUserEdit() { + const router = useRouter(); + const MySwal = withReactContent(Swal); + const [isVisible, setIsVisible] = useState([false, false]); + const [isValidPassword, setIsValidPassword] = useState(false); + const params = useParams(); + const id = params?.id; + + const toggleVisibility = (type: number) => { + setIsVisible( + type === 0 ? [!isVisible[0], isVisible[1]] : [isVisible[0], !isVisible[1]] + ); + }; + + const formOptions = { + resolver: zodResolver(masterUserSchema), + }; + type MicroIssueSchema = z.infer; + const { + control, + handleSubmit, + formState: { errors }, + setError, + watch, + setValue, + } = useForm(formOptions); + + async function save(data: z.infer) { + const formData = { + address: data.address, + email: data.email, + fullname: data.fullname, + genderType: data.genderType, + identityNumber: data.identityNumber, + phoneNumber: data.phoneNumber, + userLevelId: 2, + userRoleId: 2, + username: data.username, + }; + + const response = await editMasterUsers(formData); + + if (response?.error) { + error(response.message); + return false; + } + + successSubmit("/admin/master-user"); + } + + function successSubmit(redirect: any) { + MySwal.fire({ + title: "Sukses", + icon: "success", + confirmButtonColor: "#3085d6", + confirmButtonText: "OK", + }).then((result) => { + if (result.isConfirmed) { + router.push(redirect); + } + }); + } + + async function onSubmit(data: z.infer) { + MySwal.fire({ + title: "Simpan Data", + text: "", + icon: "warning", + showCancelButton: true, + cancelButtonColor: "#d33", + confirmButtonColor: "#3085d6", + confirmButtonText: "Simpan", + }).then((result) => { + if (result.isConfirmed) { + save(data); + } + }); + } + + useEffect(() => { + initFetch(); + }, [id]); + + const initFetch = async () => { + const res = await getDetailMasterUsers(String(id)); + const profile = res?.data?.data; + setValue("fullname", profile?.fullname); + setValue("username", profile?.username); + setValue("email", profile?.email); + setValue("address", profile?.address); + setValue("identityNumber", profile?.identityNumber); + setValue("genderType", profile?.genderType); + setValue("phoneNumber", profile?.phoneNumber); + }; + + return ( +
+
+ + ( + + )} + /> + {errors.fullname?.message && ( +

{errors.fullname?.message}

+ )} + ( + + )} + /> + {errors.username?.message && ( +

{errors.username?.message}

+ )} + + ( + + )} + /> + {errors.email?.message && ( +

{errors.email?.message}

+ )} + + {/* ( + + )} + /> + {errors.identityType?.message && ( +

+ {errors.identityType?.message} +

+ )} */} + ( + + )} + /> + {errors.identityNumber?.message && ( +

+ {errors.identityNumber?.message} +

+ )} + + ( +