"use client"; import { error } from "@/config/swal"; import { createMasterUser } 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 { useRouter } from "next/navigation"; import React, { 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" }), password: z .string() .min(8, "Password harus memiliki minimal 8 karakter.") .refine((password) => /[A-Z]/.test(password), { message: "Password harus memiliki minimal satu huruf kapital.", }) .refine((password) => /[0-9]/.test(password), { message: "Password harus memiliki minimal satu angka.", }) .refine((password) => /[!@#$%^&*(),.?":{}|<>]/.test(password), { message: "Password harus memiliki minimal satu simbol.", }), passwordValidate: z.string().min(1, { message: "Required" }), email: z.string().min(1, { message: "Required" }), // identityType: z.string().min(1, { message: "Required" }), identityNumber: z.string().min(1, { message: "Required" }), // lastEducation: z.string().min(1, { message: "Required" }), genderType: z.string().min(1, { message: "Required" }), phoneNumber: z.string().min(1, { message: "Required" }), // workType: z.string().min(1, { message: "Required" }), address: z.string().min(1, { message: "Required" }), // birthDate: z.object({ // startDate: z.string().min(1, { message: "Required" }), // endDate: z.string().min(1, { message: "Required" }), // }), }); const typeIdentity = [ { id: 1, value: "KTP", }, { id: 2, value: "SIM", }, { id: 3, value: "Passport", }, ]; const workingBackground = [ { id: 1, value: "Pegawai Negri Sipil", }, { id: 2, value: "Wiraswasta", }, { id: 3, value: "Guru", }, { id: 4, value: "Dokter", }, ]; const educationGrade = [ { id: 1, value: "SMA / Sederajat", }, { id: 2, value: "Diploma 1", }, { id: 3, value: "Diploma 2", }, { id: 4, value: "Diploma 3", }, { id: 5, value: "Diploma 4", }, { id: 6, value: "S1", }, { id: 7, value: "S2", }, { id: 8, value: "S3", }, ]; export default function FormMasterUser() { const router = useRouter(); const MySwal = withReactContent(Swal); const [isVisible, setIsVisible] = useState([false, false]); const [isValidPassword, setIsValidPassword] = useState(false); const toggleVisibility = (type: number) => { setIsVisible( type === 0 ? [!isVisible[0], isVisible[1]] : [isVisible[0], !isVisible[1]] ); }; const formOptions = { resolver: zodResolver(masterUserSchema), defaultValues: { password: "", passwordValidate: "" }, }; type MicroIssueSchema = z.infer; const { control, handleSubmit, formState: { errors }, setError, watch, setValue, } = useForm(formOptions); const passwordVal = watch("password"); const passwordConfVal = watch("passwordValidate"); async function save(data: z.infer) { const formData = { address: data.address, password: data.password, email: data.email, fullname: data.fullname, genderType: data.genderType, identityNumber: data.identityNumber, identityType: "nrp", phoneNumber: data.phoneNumber, userLevelId: 2, userRoleId: 2, username: data.username, }; const response = await createMasterUser(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) { if (data.password === data.passwordValidate) { MySwal.fire({ title: "Simpan Data", text: "", icon: "warning", showCancelButton: true, cancelButtonColor: "#d33", confirmButtonColor: "#3085d6", confirmButtonText: "Simpan", }).then((result) => { if (result.isConfirmed) { save(data); } }); } else { setError("passwordValidate", { type: "manual", message: "Password harus sama.", }); } } const generatePassword = () => { const length = Math.floor(Math.random() * 9) + 8; const upperCaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const lowerCaseChars = "abcdefghijklmnopqrstuvwxyz"; const numberChars = "0123456789"; const specialChars = "!@#$%^&*"; const allChars = upperCaseChars + lowerCaseChars + numberChars + specialChars; let generatedPassword = ""; generatedPassword += upperCaseChars[Math.floor(Math.random() * upperCaseChars.length)]; generatedPassword += specialChars[Math.floor(Math.random() * specialChars.length)]; generatedPassword += numberChars[Math.floor(Math.random() * numberChars.length)]; generatedPassword += lowerCaseChars[Math.floor(Math.random() * lowerCaseChars.length)]; for (let i = generatedPassword.length; i < length; i++) { generatedPassword += allChars[Math.floor(Math.random() * allChars.length)]; } generatedPassword = generatedPassword .split("") .sort(() => 0.5 - Math.random()) .join(""); setValue("password", generatedPassword); }; 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}

)} (