2024-03-08 14:58:36 +01:00
|
|
|
import React from 'react'
|
|
|
|
import {
|
|
|
|
Box,
|
|
|
|
Button,
|
|
|
|
Container,
|
|
|
|
FormControl,
|
|
|
|
FormErrorMessage,
|
|
|
|
FormLabel,
|
|
|
|
Heading,
|
|
|
|
Input,
|
|
|
|
useColorModeValue,
|
|
|
|
} from '@chakra-ui/react'
|
|
|
|
import { SubmitHandler, useForm } from 'react-hook-form'
|
|
|
|
import { useMutation } from 'react-query'
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
import { ApiError, UpdatePassword, UsersService } from '../../client'
|
|
|
|
import useCustomToast from '../../hooks/useCustomToast'
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const ChangePassword: React.FC = () => {
|
2024-03-08 14:58:36 +01:00
|
|
|
const color = useColorModeValue('gray.700', 'white')
|
|
|
|
const showToast = useCustomToast()
|
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
reset,
|
|
|
|
getValues,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
} = useForm<UpdatePasswordForm>({
|
|
|
|
mode: 'onBlur',
|
|
|
|
criteriaMode: 'all',
|
|
|
|
})
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
const UpdatePassword = async (data: UpdatePassword) => {
|
|
|
|
await UsersService.updatePasswordMe({ requestBody: data })
|
|
|
|
}
|
2024-03-07 19:16:23 +01:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
const mutation = useMutation(UpdatePassword, {
|
|
|
|
onSuccess: () => {
|
|
|
|
showToast('Success!', 'Password updated.', 'success')
|
|
|
|
reset()
|
|
|
|
},
|
|
|
|
onError: (err: ApiError) => {
|
|
|
|
const errDetail = err.body.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 (
|
|
|
|
<>
|
|
|
|
<Container maxW="full" as="form" onSubmit={handleSubmit(onSubmit)}>
|
|
|
|
<Heading size="sm" py={4}>
|
|
|
|
Change Password
|
|
|
|
</Heading>
|
|
|
|
<Box w={{ sm: 'full', md: '50%' }}>
|
|
|
|
<FormControl isRequired isInvalid={!!errors.current_password}>
|
|
|
|
<FormLabel color={color} htmlFor="current_password">
|
|
|
|
Current password
|
|
|
|
</FormLabel>
|
|
|
|
<Input
|
|
|
|
id="current_password"
|
2024-03-10 00:53:03 +01:00
|
|
|
{...register('current_password')}
|
2024-03-08 14:58:36 +01:00
|
|
|
placeholder="Password"
|
|
|
|
type="password"
|
|
|
|
/>
|
|
|
|
{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"
|
|
|
|
{...register('new_password', {
|
|
|
|
required: 'Password is required',
|
|
|
|
minLength: {
|
|
|
|
value: 8,
|
|
|
|
message: 'Password must be at least 8 characters',
|
|
|
|
},
|
|
|
|
})}
|
|
|
|
placeholder="Password"
|
|
|
|
type="password"
|
|
|
|
/>
|
|
|
|
{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"
|
|
|
|
{...register('confirm_password', {
|
|
|
|
required: 'Please confirm your password',
|
|
|
|
validate: (value) =>
|
|
|
|
value === getValues().new_password ||
|
|
|
|
'The passwords do not match',
|
|
|
|
})}
|
|
|
|
placeholder="Password"
|
|
|
|
type="password"
|
|
|
|
/>
|
|
|
|
{errors.confirm_password && (
|
|
|
|
<FormErrorMessage>
|
|
|
|
{errors.confirm_password.message}
|
|
|
|
</FormErrorMessage>
|
|
|
|
)}
|
|
|
|
</FormControl>
|
|
|
|
<Button
|
|
|
|
bg="ui.main"
|
|
|
|
color="white"
|
|
|
|
_hover={{ opacity: 0.8 }}
|
|
|
|
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
|