Files
tanami-admin-panel/src/Pages/ChangePassword.jsx

246 lines
7.4 KiB
React
Raw Normal View History

2024-12-05 20:26:39 +05:30
import {
Button,
DrawerFooter,
FormControl,
FormErrorMessage,
FormLabel,
Input,
2024-12-06 16:11:18 +05:30
InputGroup,
InputRightElement,
2024-12-05 20:26:39 +05:30
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalHeader,
ModalOverlay,
Stack,
useToast,
} from "@chakra-ui/react";
import { yupResolver } from "@hookform/resolvers/yup";
2024-12-20 20:00:13 +05:30
import React, { useState } from "react";
import { useForm } from "react-hook-form";
import * as yup from "yup";
2024-12-05 20:26:39 +05:30
import CustomAlertDialog from "../Components/CustomAlertDialog";
import ToastBox from "../Components/ToastBox";
2024-12-06 20:04:20 +05:30
import { useUpdatePasswordMutation } from "../Services/change.password.service";
2024-12-05 20:26:39 +05:30
2024-12-09 20:10:47 +05:30
// Validation schema
const passwordSchema = yup.object().shape({
oldPassword: yup.string().required("Current Password is required"),
newPassword: yup
2024-12-05 20:26:39 +05:30
.string()
2024-12-09 20:10:47 +05:30
.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()
2024-12-20 16:22:29 +05:30
.required("Confirm New Password is required")
.oneOf([yup.ref("newPassword")], "Password do not match"),
2024-12-05 20:26:39 +05:30
});
const ChangePassword = ({
isOpen,
onClose,
firstField,
actionId,
setActionId,
}) => {
2024-12-20 20:00:13 +05:30
const initialValue = {
oldPassword: "",
newPassword: "",
confirmNewPassword: "",
};
2024-12-05 20:26:39 +05:30
const [isLoading, setIsLoading] = useState(false);
const [alert, setAlert] = useState(false);
2024-12-06 16:11:18 +05:30
const [showCurrentPassword, setShowCurrentPassword] = useState(false);
const [showNewPassword, setShowNewPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
2024-12-09 20:10:47 +05:30
const toast = useToast();
2024-12-20 20:00:13 +05:30
const [input, setInput] = useState(initialValue);
2024-12-05 20:26:39 +05:30
2024-12-09 20:10:47 +05:30
const [updatePassword] = useUpdatePasswordMutation();
2024-12-05 20:26:39 +05:30
2024-12-09 20:10:47 +05:30
// Form handling
2024-12-05 20:26:39 +05:30
const {
2024-12-09 20:10:47 +05:30
register,
2024-12-05 20:26:39 +05:30
handleSubmit,
reset,
formState: { errors },
} = useForm({
2024-12-09 20:10:47 +05:30
resolver: yupResolver(passwordSchema),
mode: "all",
2024-12-05 20:26:39 +05:30
});
2024-12-09 20:10:47 +05:30
// Form submit handler
2024-12-20 20:00:13 +05:30
const onSubmit = async () => {
2024-12-09 20:10:47 +05:30
setIsLoading(true);
try {
2024-12-20 20:00:13 +05:30
const res = await updatePassword(input); // Assuming API request works as expected
2024-12-09 20:10:47 +05:30
if (res?.data?.statusCode === 200) {
toast({
render: () => <ToastBox message={res?.data?.message} />,
});
handleClose();
2024-12-12 13:52:55 +05:30
} else if (res?.error) {
2024-12-09 20:10:47 +05:30
toast({
render: () => (
<ToastBox message={res?.error?.data?.message} status={"error"} />
),
});
2024-12-20 17:17:18 +05:30
setAlert(false);
2024-12-06 20:04:20 +05:30
}
2024-12-09 20:10:47 +05:30
} catch (error) {
console.error(error);
} finally {
setIsLoading(false);
}
2024-12-05 20:26:39 +05:30
};
2024-12-20 20:00:13 +05:30
const handleSubmitFrom = (data) => {
setAlert(true);
setInput(data);
};
2024-12-09 20:10:47 +05:30
// Handle modal close
2024-12-05 20:26:39 +05:30
const handleClose = () => {
setAlert(false);
onClose();
2024-12-09 20:10:47 +05:30
reset();
2024-12-05 20:26:39 +05:30
};
return (
<>
2024-12-20 20:18:26 +05:30
<Modal isOpen={isOpen} onClose={handleClose} initialFocusRef={firstField}>
2024-12-05 20:26:39 +05:30
<ModalOverlay />
<ModalContent>
2024-12-09 20:10:47 +05:30
<ModalHeader fontSize="md">Change Password</ModalHeader>
2024-12-05 20:26:39 +05:30
<ModalCloseButton />
<ModalBody pb={6}>
<Stack spacing={4}>
2024-12-09 20:10:47 +05:30
{/* Current Password */}
2024-12-20 16:22:29 +05:30
<FormControl isInvalid={errors.oldPassword} isRequired>
2024-12-09 20:10:47 +05:30
<FormLabel fontSize="sm" mb={1} fontWeight={500}>
2024-12-06 16:11:18 +05:30
Current Password
</FormLabel>
<InputGroup size="sm">
<Input
2024-12-09 20:10:47 +05:30
{...register("oldPassword")}
fontSize="sm"
type={showCurrentPassword ? "text" : "password"}
2024-12-05 20:26:39 +05:30
focusBorderColor="forestGreen.300"
2024-12-06 16:11:18 +05:30
/>
<InputRightElement width="4.5rem">
<Button
h="1.5rem"
size="xs"
onClick={() => setShowCurrentPassword((prev) => !prev)}
>
{showCurrentPassword ? "Hide" : "Show"}
</Button>
</InputRightElement>
</InputGroup>
2024-12-09 20:10:47 +05:30
<FormErrorMessage>
{errors.oldPassword?.message}
2024-12-05 20:26:39 +05:30
</FormErrorMessage>
</FormControl>
2024-12-06 16:11:18 +05:30
2024-12-09 20:10:47 +05:30
{/* New Password */}
2024-12-20 16:22:29 +05:30
<FormControl isInvalid={errors.newPassword} isRequired>
2024-12-09 20:10:47 +05:30
<FormLabel fontSize="sm" mb={1}>
2024-12-06 16:11:18 +05:30
New Password
</FormLabel>
<InputGroup size="sm">
<Input
2024-12-09 20:10:47 +05:30
{...register("newPassword")}
fontSize="sm"
type={showNewPassword ? "text" : "password"}
2024-12-05 20:26:39 +05:30
focusBorderColor="forestGreen.300"
2024-12-06 16:11:18 +05:30
/>
<InputRightElement width="4.5rem">
<Button
h="1.5rem"
size="xs"
onClick={() => setShowNewPassword((prev) => !prev)}
>
{showNewPassword ? "Hide" : "Show"}
</Button>
</InputRightElement>
</InputGroup>
2024-12-09 20:10:47 +05:30
<FormErrorMessage>
2024-12-05 20:26:39 +05:30
{errors.newPassword?.message}
</FormErrorMessage>
</FormControl>
2024-12-06 16:11:18 +05:30
2024-12-09 20:10:47 +05:30
{/* Confirm Password */}
2024-12-20 16:22:29 +05:30
<FormControl isInvalid={errors.confirmNewPassword} isRequired>
2024-12-09 20:10:47 +05:30
<FormLabel fontSize="sm" mb={1}>
Confirm New Password
2024-12-06 16:11:18 +05:30
</FormLabel>
<InputGroup size="sm">
<Input
2024-12-09 20:10:47 +05:30
{...register("confirmNewPassword")}
fontSize="sm"
type={showConfirmPassword ? "text" : "password"}
2024-12-05 20:26:39 +05:30
focusBorderColor="forestGreen.300"
2024-12-06 16:11:18 +05:30
/>
<InputRightElement width="4.5rem">
<Button
h="1.5rem"
size="xs"
onClick={() => setShowConfirmPassword((prev) => !prev)}
>
{showConfirmPassword ? "Hide" : "Show"}
</Button>
</InputRightElement>
</InputGroup>
2024-12-09 20:10:47 +05:30
<FormErrorMessage>
{errors.confirmNewPassword?.message}
2024-12-05 20:26:39 +05:30
</FormErrorMessage>
</FormControl>
</Stack>
</ModalBody>
<DrawerFooter mb={5}>
<Button
onClick={handleClose}
2024-12-09 20:10:47 +05:30
bg="#e0ebeb"
size="sm"
mr={3}
rounded={"sm"}
2024-12-05 20:26:39 +05:30
>
Cancel
</Button>
<Button
rounded={"sm"}
2024-12-09 20:10:47 +05:30
colorScheme="forestGreen"
size="sm"
2024-12-20 20:00:13 +05:30
// onClick={() => setAlert(true)}
onClick={handleSubmit(handleSubmitFrom)}
2024-12-09 20:10:47 +05:30
isLoading={isLoading}
2024-12-05 20:26:39 +05:30
>
Save
</Button>
</DrawerFooter>
</ModalContent>
</Modal>
<CustomAlertDialog
isOpen={alert}
onClose={() => setAlert(false)}
2024-12-20 20:00:13 +05:30
alertHandler={onSubmit}
2024-12-09 20:10:47 +05:30
message={"Are you sure you want to change the password?"}
2024-12-05 20:26:39 +05:30
isLoading={isLoading}
/>
</>
);
};
export default ChangePassword;