diff --git a/components/form/form-master-user.tsx b/components/form/form-master-user.tsx index 1803621..3c0e0b8 100644 --- a/components/form/form-master-user.tsx +++ b/components/form/form-master-user.tsx @@ -23,6 +23,7 @@ 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" }), @@ -42,17 +43,17 @@ const masterUserSchema = z.object({ passwordValidate: z.string().min(1, { message: "Required" }), email: z.string().min(1, { message: "Required" }), - identityType: 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" }), + // 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" }), + // 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" }), - }), + // birthDate: z.object({ + // startDate: z.string().min(1, { message: "Required" }), + // endDate: z.string().min(1, { message: "Required" }), + // }), }); const typeIdentity = [ @@ -128,6 +129,8 @@ 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]] @@ -136,10 +139,7 @@ export default function FormMasterUser() { const formOptions = { resolver: zodResolver(masterUserSchema), - defaultValues: { - identityType: "KTP", - birthDate: { stardDate: "", endDate: "" }, - }, + defaultValues: { password: "", passwordValidate: "" }, }; type MicroIssueSchema = z.infer; const { @@ -147,24 +147,25 @@ export default function FormMasterUser() { 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, - dateOfBirt: data.birthDate, password: data.password, email: data.email, fullname: data.fullname, genderType: data.genderType, identityNumber: data.identityNumber, - identityType: data.identityType, - lastEducation: data.lastEducation, phoneNumber: data.phoneNumber, userLevelId: 2, userRoleId: 2, username: data.username, - workType: data.workType, }; const response = await createMasterUser(formData); @@ -174,7 +175,7 @@ export default function FormMasterUser() { return false; } - successSubmit("/admin/article"); + successSubmit("/admin/master-user"); } function successSubmit(redirect: any) { @@ -213,6 +214,40 @@ export default function FormMasterUser() { } } + 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 (
@@ -224,12 +259,18 @@ export default function FormMasterUser() { )} @@ -250,6 +291,12 @@ export default function FormMasterUser() { onChange={onChange} labelPlacement="outside" className="w-full" + classNames={{ + inputWrapper: [ + "border-1 rounded-lg", + "dark:group-data-[focused=false]:bg-transparent !border-1 dark:!border-gray-400", + ], + }} variant="bordered" /> )} @@ -257,6 +304,220 @@ export default function FormMasterUser() { {errors.username?.message && (

{errors.username?.message}

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

{errors.email?.message}

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

+ {errors.identityType?.message} +

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

+ {errors.identityNumber?.message} +

+ )} + + ( +