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' ;
2024-03-07 19:16:23 +01:00
import { useMutation , useQueryClient } from 'react-query' ;
2024-02-26 09:39:09 -05:00
2024-03-07 19:16:23 +01:00
import { UserCreate , UsersService } from '../../client' ;
2024-02-26 09:39:09 -05:00
import { ApiError } from '../../client/core/ApiError' ;
2024-03-07 19:16:23 +01:00
import useCustomToast from '../../hooks/useCustomToast' ;
2024-02-26 09:39:09 -05:00
interface AddUserProps {
isOpen : boolean ;
onClose : ( ) = > void ;
}
interface UserCreateForm extends UserCreate {
2024-02-28 18:30:59 -05:00
confirm_password : string ;
2024-02-26 09:39:09 -05:00
}
const AddUser : React.FC < AddUserProps > = ( { isOpen , onClose } ) = > {
2024-03-07 19:16:23 +01:00
const queryClient = useQueryClient ( ) ;
2024-02-26 09:39:09 -05:00
const showToast = useCustomToast ( ) ;
2024-02-28 18:30:59 -05:00
const { register , handleSubmit , reset , getValues , formState : { errors , isSubmitting } } = useForm < UserCreateForm > ( {
mode : 'onBlur' ,
criteriaMode : 'all' ,
defaultValues : {
email : '' ,
full_name : '' ,
password : '' ,
confirm_password : '' ,
is_superuser : false ,
is_active : false
}
} ) ;
2024-02-26 09:39:09 -05:00
2024-03-07 19:16:23 +01:00
const addUser = async ( data : UserCreate ) = > {
await UsersService . createUser ( { requestBody : data } )
}
const mutation = useMutation ( addUser , {
onSuccess : ( ) = > {
2024-02-28 18:30:59 -05:00
showToast ( 'Success!' , 'User created successfully.' , 'success' ) ;
reset ( ) ;
onClose ( ) ;
2024-03-07 19:16:23 +01:00
} ,
onError : ( err : ApiError ) = > {
const errDetail = err . body . detail ;
2024-02-28 18:30:59 -05:00
showToast ( 'Something went wrong.' , ` ${ errDetail } ` , 'error' ) ;
2024-03-07 19:16:23 +01:00
} ,
onSettled : ( ) = > {
queryClient . invalidateQueries ( 'users' ) ;
2024-02-26 09:39:09 -05:00
}
2024-03-07 19:16:23 +01:00
} ) ;
const onSubmit : SubmitHandler < UserCreateForm > = ( data ) = > {
mutation . mutate ( data ) ;
2024-02-26 09:39:09 -05:00
}
return (
< >
< Modal
isOpen = { isOpen }
onClose = { onClose }
size = { { base : 'sm' , md : 'md' } }
isCentered
>
< ModalOverlay / >
< ModalContent as = 'form' onSubmit = { handleSubmit ( onSubmit ) } >
< ModalHeader > Add User < / ModalHeader >
< ModalCloseButton / >
< ModalBody pb = { 6 } >
2024-02-28 18:30:59 -05:00
< FormControl isRequired 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' , { required : 'Email is required' , 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 >
2024-02-28 18:30:59 -05:00
< FormControl mt = { 4 } isInvalid = { ! ! errors . full_name } >
2024-02-26 09:39:09 -05:00
< FormLabel htmlFor = 'name' > Full name < / FormLabel >
< Input id = 'name' { ...register ( 'full_name' ) } placeholder = 'Full name' type = 'text' / >
2024-02-28 18:30:59 -05:00
{ errors . full_name && < FormErrorMessage > { errors . full_name . message } < / FormErrorMessage > }
2024-02-26 09:39:09 -05:00
< / FormControl >
2024-02-28 18:30:59 -05:00
< FormControl mt = { 4 } isRequired isInvalid = { ! ! errors . password } >
2024-02-26 09:39:09 -05:00
< FormLabel htmlFor = 'password' > Set Password < / FormLabel >
2024-02-28 18:30:59 -05:00
< Input id = 'password' { ...register ( 'password' , { required : 'Password is required' , minLength : { value : 8 , message : 'Password must be at least 8 characters' } } ) } placeholder = 'Password' 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 } 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 ( ) . password || 'The passwords do not match'
} ) } placeholder = 'Password' type = 'password' / >
{ errors . confirm_password && < FormErrorMessage > { errors . confirm_password . message } < / FormErrorMessage > }
2024-02-26 09:39:09 -05:00
< / FormControl >
< Flex mt = { 4 } >
< FormControl >
< Checkbox { ...register ( 'is_superuser' ) } colorScheme = 'teal' > Is superuser ? < / Checkbox >
< / FormControl >
< FormControl >
< Checkbox { ...register ( 'is_active' ) } colorScheme = 'teal' > Is active ? < / Checkbox >
< / FormControl >
< / Flex >
< / ModalBody >
< ModalFooter gap = { 3 } >
2024-02-28 18:30:59 -05:00
< Button bg = 'ui.main' color = 'white' _hover = { { opacity : 0.8 } } type = 'submit' isLoading = { isSubmitting } >
2024-02-26 09:39:09 -05:00
Save
< / Button >
< Button onClick = { onClose } > Cancel < / Button >
< / ModalFooter >
< / ModalContent >
< / Modal >
< / >
)
}
export default AddUser ;