2024-02-26 09:39:09 -05:00
import React from 'react' ;
2024-02-28 18:30:59 -05:00
import { Button , Checkbox , Flex , FormControl , FormErrorMessage , FormLabel , Input , Modal , ModalBody , ModalCloseButton , ModalContent , ModalFooter , ModalHeader , ModalOverlay } from '@chakra-ui/react' ;
2024-02-26 09:39:09 -05:00
import { SubmitHandler , useForm } from 'react-hook-form' ;
import { ApiError , UserUpdate } from '../../client' ;
import useCustomToast from '../../hooks/useCustomToast' ;
import { useUsersStore } from '../../store/users-store' ;
interface EditUserProps {
user_id : number ;
isOpen : boolean ;
onClose : ( ) = > void ;
}
interface UserUpdateForm extends UserUpdate {
confirm_password : string ;
}
const EditUser : React.FC < EditUserProps > = ( { user_id , isOpen , onClose } ) = > {
const showToast = useCustomToast ( ) ;
const { editUser , users } = useUsersStore ( ) ;
const currentUser = users . find ( ( user ) = > user . id === user_id ) ;
2024-02-28 18:30:59 -05:00
const { register , handleSubmit , reset , getValues , formState : { errors , isSubmitting } } = useForm < UserUpdateForm > ( {
mode : 'onBlur' ,
criteriaMode : 'all' ,
defaultValues : {
email : currentUser?.email ,
full_name : currentUser?.full_name ,
password : '' ,
confirm_password : '' ,
is_superuser : currentUser?.is_superuser ,
is_active : currentUser?.is_active
}
} ) ;
2024-02-26 09:39:09 -05:00
const onSubmit : SubmitHandler < UserUpdateForm > = async ( data ) = > {
2024-02-28 18:30:59 -05:00
try {
if ( data . password === '' ) {
delete data . password ;
2024-02-26 09:39:09 -05:00
}
2024-02-28 18:30:59 -05:00
await editUser ( user_id , data ) ;
showToast ( 'Success!' , 'User updated successfully.' , 'success' ) ;
reset ( ) ;
onClose ( ) ;
} catch ( err ) {
const errDetail = ( err as ApiError ) . body . detail ;
showToast ( 'Something went wrong.' , ` ${ errDetail } ` , 'error' ) ;
2024-02-26 09:39:09 -05:00
}
}
const onCancel = ( ) = > {
reset ( ) ;
onClose ( ) ;
}
return (
< >
< Modal
isOpen = { isOpen }
onClose = { onClose }
size = { { base : 'sm' , md : 'md' } }
isCentered
>
< ModalOverlay / >
< ModalContent as = 'form' onSubmit = { handleSubmit ( onSubmit ) } >
< ModalHeader > Edit User < / ModalHeader >
< ModalCloseButton / >
< ModalBody pb = { 6 } >
2024-02-28 18:30:59 -05:00
< FormControl isInvalid = { ! ! errors . email } >
2024-02-26 09:39:09 -05:00
< FormLabel htmlFor = 'email' > Email < / FormLabel >
2024-02-28 18:30:59 -05:00
< Input id = 'email' { ...register ( 'email' , { pattern : { value : / ^ [ A - Z0 - 9._ % + - ] + @ [ A - Z0 - 9. - ] + \ . [ A - Z ] { 2 , 4 } $ / i , message : 'Invalid email address' } } ) } placeholder = 'Email' type = 'email' / >
{ errors . email && < FormErrorMessage > { errors . email . message } < / FormErrorMessage > }
2024-02-26 09:39:09 -05:00
< / FormControl >
< FormControl mt = { 4 } >
< FormLabel htmlFor = 'name' > Full name < / FormLabel >
2024-02-28 18:30:59 -05:00
< Input id = "name" { ...register ( 'full_name' ) } type = 'text' / >
2024-02-26 09:39:09 -05:00
< / FormControl >
2024-02-28 18:30:59 -05:00
< FormControl mt = { 4 } isInvalid = { ! ! errors . password } >
< FormLabel htmlFor = 'password' > Set Password < / FormLabel >
< Input id = 'password' { ...register ( 'password' , { minLength : { value : 8 , message : 'Password must be at least 8 characters' } } ) } placeholder = '••••••••' type = 'password' / >
{ errors . password && < FormErrorMessage > { errors . password . message } < / FormErrorMessage > }
2024-02-26 09:39:09 -05:00
< / FormControl >
2024-02-28 18:30:59 -05:00
< FormControl mt = { 4 } isInvalid = { ! ! errors . confirm_password } >
< FormLabel htmlFor = 'confirm_password' > Confirm Password < / FormLabel >
< Input id = 'confirm_password' { ...register ( 'confirm_password' , {
validate : value = > value === getValues ( ) . password || 'The passwords do not match'
} ) } placeholder = '••••••••' type = 'password' / >
{ errors . confirm_password && < FormErrorMessage > { errors . confirm_password . message } < / FormErrorMessage > }
2024-02-26 09:39:09 -05:00
< / FormControl >
< Flex >
< FormControl mt = { 4 } >
2024-02-28 18:30:59 -05:00
< Checkbox { ...register ( 'is_superuser' ) } colorScheme = 'teal' > Is superuser ? < / Checkbox >
2024-02-26 09:39:09 -05:00
< / FormControl >
< FormControl mt = { 4 } >
2024-02-28 18:30:59 -05:00
< Checkbox { ...register ( 'is_active' ) } colorScheme = 'teal' > Is active ? < / Checkbox >
2024-02-26 09:39:09 -05:00
< / FormControl >
< / Flex >
< / ModalBody >
< ModalFooter gap = { 3 } >
< Button bg = 'ui.main' color = 'white' _hover = { { opacity : 0.8 } } type = 'submit' isLoading = { isSubmitting } >
Save
< / Button >
< Button onClick = { onCancel } > Cancel < / Button >
< / ModalFooter >
< / ModalContent >
< / Modal >
< / >
)
}
export default EditUser ;