2024-03-08 14:58:36 +01:00
|
|
|
import {
|
|
|
|
Box,
|
|
|
|
Button,
|
|
|
|
Container,
|
|
|
|
FormControl,
|
|
|
|
FormErrorMessage,
|
|
|
|
FormLabel,
|
|
|
|
Heading,
|
|
|
|
Input,
|
|
|
|
useColorModeValue,
|
2024-03-17 17:28:45 +01:00
|
|
|
} from "@chakra-ui/react"
|
2024-04-04 16:30:42 +02:00
|
|
|
import { useMutation } from "@tanstack/react-query"
|
2024-04-08 15:49:22 -05:00
|
|
|
import { type SubmitHandler, useForm } from "react-hook-form"
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-03-28 21:15:11 -05:00
|
|
|
import { type ApiError, type UpdatePassword, UsersService } from "../../client"
|
2024-03-17 17:28:45 +01:00
|
|
|
import useCustomToast from "../../hooks/useCustomToast"
|
2024-04-01 22:53:33 -05:00
|
|
|
import { confirmPasswordRules, passwordRules } from "../../utils"
|
2024-02-26 09:39:09 -05:00
|
|
|
|
|
|
|
interface UpdatePasswordForm extends UpdatePassword {
|
2024-03-08 14:58:36 +01:00
|
|
|
confirm_password: string
|
2024-02-26 09:39:09 -05:00
|
|
|
}
|
|
|
|
|
2024-03-28 20:22:28 -05:00
|
|
|
const ChangePassword = () => {
|
2024-04-01 22:53:33 -05:00
|
|
|
const color = useColorModeValue("inherit", "ui.light")
|
2024-03-08 14:58:36 +01:00
|
|
|
const showToast = useCustomToast()
|
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
reset,
|
|
|
|
getValues,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
} = useForm<UpdatePasswordForm>({
|
2024-03-17 17:28:45 +01:00
|
|
|
mode: "onBlur",
|
|
|
|
criteriaMode: "all",
|
2024-03-08 14:58:36 +01:00
|
|
|
})
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-04-04 16:30:42 +02:00
|
|
|
const mutation = useMutation({
|
|
|
|
mutationFn: (data: UpdatePassword) =>
|
2024-04-01 22:53:33 -05:00
|
|
|
UsersService.updatePasswordMe({ requestBody: data }),
|
2024-04-04 16:30:42 +02:00
|
|
|
onSuccess: () => {
|
2024-07-30 12:48:36 -05:00
|
|
|
showToast("Success!", "Password updated successfully.", "success")
|
2024-04-04 16:30:42 +02:00
|
|
|
reset()
|
2024-03-08 14:58:36 +01:00
|
|
|
},
|
2024-04-04 16:30:42 +02:00
|
|
|
onError: (err: ApiError) => {
|
|
|
|
const errDetail = (err.body as any)?.detail
|
|
|
|
showToast("Something went wrong.", `${errDetail}`, "error")
|
|
|
|
},
|
|
|
|
})
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
const onSubmit: SubmitHandler<UpdatePasswordForm> = async (data) => {
|
|
|
|
mutation.mutate(data)
|
|
|
|
}
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
return (
|
|
|
|
<>
|
2024-05-20 13:30:18 -05:00
|
|
|
<Container maxW="full">
|
2024-03-08 14:58:36 +01:00
|
|
|
<Heading size="sm" py={4}>
|
|
|
|
Change Password
|
|
|
|
</Heading>
|
2024-05-20 13:30:18 -05:00
|
|
|
<Box
|
|
|
|
w={{ sm: "full", md: "50%" }}
|
|
|
|
as="form"
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
|
|
>
|
2024-03-08 14:58:36 +01:00
|
|
|
<FormControl isRequired isInvalid={!!errors.current_password}>
|
|
|
|
<FormLabel color={color} htmlFor="current_password">
|
2024-05-20 13:30:18 -05:00
|
|
|
Current Password
|
2024-03-08 14:58:36 +01:00
|
|
|
</FormLabel>
|
|
|
|
<Input
|
|
|
|
id="current_password"
|
2024-03-17 17:28:45 +01:00
|
|
|
{...register("current_password")}
|
2024-03-08 14:58:36 +01:00
|
|
|
placeholder="Password"
|
|
|
|
type="password"
|
2024-07-21 20:24:32 -05:00
|
|
|
w="auto"
|
2024-03-08 14:58:36 +01:00
|
|
|
/>
|
|
|
|
{errors.current_password && (
|
|
|
|
<FormErrorMessage>
|
|
|
|
{errors.current_password.message}
|
|
|
|
</FormErrorMessage>
|
|
|
|
)}
|
|
|
|
</FormControl>
|
|
|
|
<FormControl mt={4} isRequired isInvalid={!!errors.new_password}>
|
|
|
|
<FormLabel htmlFor="password">Set Password</FormLabel>
|
|
|
|
<Input
|
|
|
|
id="password"
|
2024-04-01 22:53:33 -05:00
|
|
|
{...register("new_password", passwordRules())}
|
2024-03-08 14:58:36 +01:00
|
|
|
placeholder="Password"
|
|
|
|
type="password"
|
2024-07-21 20:24:32 -05:00
|
|
|
w="auto"
|
2024-03-08 14:58:36 +01:00
|
|
|
/>
|
|
|
|
{errors.new_password && (
|
|
|
|
<FormErrorMessage>{errors.new_password.message}</FormErrorMessage>
|
|
|
|
)}
|
|
|
|
</FormControl>
|
|
|
|
<FormControl mt={4} isRequired isInvalid={!!errors.confirm_password}>
|
|
|
|
<FormLabel htmlFor="confirm_password">Confirm Password</FormLabel>
|
|
|
|
<Input
|
|
|
|
id="confirm_password"
|
2024-04-01 22:53:33 -05:00
|
|
|
{...register("confirm_password", confirmPasswordRules(getValues))}
|
2024-03-08 14:58:36 +01:00
|
|
|
placeholder="Password"
|
|
|
|
type="password"
|
2024-07-21 20:24:32 -05:00
|
|
|
w="auto"
|
2024-03-08 14:58:36 +01:00
|
|
|
/>
|
|
|
|
{errors.confirm_password && (
|
|
|
|
<FormErrorMessage>
|
|
|
|
{errors.confirm_password.message}
|
|
|
|
</FormErrorMessage>
|
|
|
|
)}
|
|
|
|
</FormControl>
|
|
|
|
<Button
|
2024-03-11 16:50:46 +01:00
|
|
|
variant="primary"
|
2024-03-08 14:58:36 +01:00
|
|
|
mt={4}
|
|
|
|
type="submit"
|
|
|
|
isLoading={isSubmitting}
|
|
|
|
>
|
|
|
|
Save
|
|
|
|
</Button>
|
|
|
|
</Box>
|
|
|
|
</Container>
|
|
|
|
</>
|
|
|
|
)
|
2024-02-26 09:39:09 -05:00
|
|
|
}
|
2024-03-08 14:58:36 +01:00
|
|
|
export default ChangePassword
|