import { Button, DrawerFooter, FormControl, FormErrorMessage, FormLabel, Input, InputGroup, InputRightElement, Modal, ModalBody, ModalCloseButton, ModalContent, ModalHeader, ModalOverlay, Stack, useToast, } from "@chakra-ui/react"; import { yupResolver } from "@hookform/resolvers/yup"; import React, { useState } from "react"; import { useForm } from "react-hook-form"; import * as yup from "yup"; import CustomAlertDialog from "../Components/CustomAlertDialog"; import ToastBox from "../Components/ToastBox"; import { useUpdatePasswordMutation } from "../Services/change.password.service"; // Validation schema const passwordSchema = yup.object().shape({ oldPassword: yup.string().required("Current Password is required"), newPassword: yup .string() .required("New Password is required") .min(8, "Password must be at least 8 characters long") .max(16, "Password must be at most 50 characters long") .matches(/[A-Z]/, "Password must contain at least one uppercase letter") .matches(/[a-z]/, "Password must contain at least one lowercase letter") .matches(/[0-9]/, "Password must contain at least one number") .matches( /[@$!%*?&#]/, "Password must contain at least one special character" ), confirmNewPassword: yup .string() .required("Confirm New Password is required") .oneOf([yup.ref("newPassword")], "Password do not match"), }); const ChangePassword = ({ isOpen, onClose, firstField, actionId, setActionId, }) => { const initialValue = { oldPassword: "", newPassword: "", confirmNewPassword: "", }; const [isLoading, setIsLoading] = useState(false); const [alert, setAlert] = useState(false); const [showCurrentPassword, setShowCurrentPassword] = useState(false); const [showNewPassword, setShowNewPassword] = useState(false); const [showConfirmPassword, setShowConfirmPassword] = useState(false); const toast = useToast(); const [input, setInput] = useState(initialValue); const [updatePassword] = useUpdatePasswordMutation(); // Form handling const { register, handleSubmit, reset, formState: { errors }, } = useForm({ resolver: yupResolver(passwordSchema), mode: "all", }); // Form submit handler const onSubmit = async () => { setIsLoading(true); try { const res = await updatePassword(input); // Assuming API request works as expected if (res?.data?.statusCode === 200) { toast({ render: () => , }); handleClose(); } else if (res?.error) { toast({ render: () => ( ), }); setAlert(false); } } catch (error) { console.error(error); } finally { setIsLoading(false); } }; const handleSubmitFrom = (data) => { setAlert(true); setInput(data); }; // Handle modal close const handleClose = () => { setAlert(false); onClose(); reset(); }; return ( <> Change Password {/* Current Password */} Current Password {errors.oldPassword?.message} {/* New Password */} New Password {errors.newPassword?.message} {/* Confirm Password */} Confirm New Password {errors.confirmNewPassword?.message} setAlert(false)} alertHandler={onSubmit} message={"Are you sure you want to change the password?"} isLoading={isLoading} /> ); }; export default ChangePassword;