2024-02-26 09:39:09 -05:00
import React , { useState } from 'react' ;
2024-03-07 19:16:23 +01:00
import { Box , Button , Container , Flex , FormControl , FormErrorMessage , FormLabel , Heading , Input , Text , useColorModeValue } 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' ;
import { ApiError , UserOut , UserUpdateMe , UsersService } from '../../client' ;
import useAuth from '../../hooks/useAuth' ;
2024-02-26 09:39:09 -05:00
import useCustomToast from '../../hooks/useCustomToast' ;
const UserInformation : React.FC = ( ) = > {
2024-03-07 19:16:23 +01:00
const queryClient = useQueryClient ( ) ;
2024-02-26 09:39:09 -05:00
const color = useColorModeValue ( 'gray.700' , 'white' ) ;
const showToast = useCustomToast ( ) ;
const [ editMode , setEditMode ] = useState ( false ) ;
2024-03-07 19:16:23 +01:00
const { user : currentUser } = useAuth ( ) ;
const { register , handleSubmit , reset , formState : { isSubmitting , errors , isDirty } } = useForm < UserOut > ( {
mode : 'onBlur' , criteriaMode : 'all' , defaultValues : {
full_name : currentUser?.full_name ,
email : currentUser?.email
}
} )
2024-02-26 09:39:09 -05:00
const toggleEditMode = ( ) = > {
setEditMode ( ! editMode ) ;
} ;
2024-03-07 19:16:23 +01:00
const updateInfo = async ( data : UserUpdateMe ) = > {
await UsersService . updateUserMe ( { requestBody : data } )
}
const mutation = useMutation ( updateInfo , {
onSuccess : ( ) = > {
2024-02-26 09:39:09 -05:00
showToast ( 'Success!' , 'User updated successfully.' , 'success' ) ;
2024-03-07 19:16:23 +01:00
} ,
onError : ( err : ApiError ) = > {
const errDetail = err . body . detail ;
2024-02-26 09:39:09 -05:00
showToast ( 'Something went wrong.' , ` ${ errDetail } ` , 'error' ) ;
2024-03-07 19:16:23 +01:00
} ,
onSettled : ( ) = > {
queryClient . invalidateQueries ( 'users' ) ;
queryClient . invalidateQueries ( 'currentUser' ) ;
2024-02-26 09:39:09 -05:00
}
2024-03-07 19:16:23 +01:00
} ) ;
const onSubmit : SubmitHandler < UserUpdateMe > = async ( data ) = > {
mutation . mutate ( data )
2024-02-26 09:39:09 -05:00
}
const onCancel = ( ) = > {
reset ( ) ;
toggleEditMode ( ) ;
}
return (
< >
< Container maxW = 'full' as = 'form' onSubmit = { handleSubmit ( onSubmit ) } >
< Heading size = 'sm' py = { 4 } >
User Information
< / Heading >
< Box w = { { 'sm' : 'full' , 'md' : '50%' } } >
< FormControl >
< FormLabel color = { color } htmlFor = 'name' > Full name < / FormLabel >
{
editMode ?
2024-03-07 19:16:23 +01:00
< Input id = 'name' { ...register ( 'full_name' , { maxLength : 30 } ) } type = 'text' size = 'md' / > :
2024-02-26 09:39:09 -05:00
< Text size = 'md' py = { 2 } >
2024-03-07 19:16:23 +01:00
{ currentUser ? . full_name || 'N/A' }
2024-02-26 09:39:09 -05:00
< / Text >
}
< / FormControl >
2024-03-07 19:16:23 +01:00
< FormControl mt = { 4 } isInvalid = { ! ! errors . email } >
2024-02-26 09:39:09 -05:00
< FormLabel color = { color } htmlFor = 'email' > Email < / FormLabel >
{
editMode ?
2024-03-07 19:16:23 +01: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' } } ) } type = 'text' size = 'md' / > :
2024-02-26 09:39:09 -05:00
< Text size = 'md' py = { 2 } >
2024-03-07 19:16:23 +01:00
{ currentUser ! . email }
2024-02-26 09:39:09 -05:00
< / Text >
}
2024-03-07 19:16:23 +01:00
{ errors . email && < FormErrorMessage > { errors . email . message } < / FormErrorMessage > }
2024-02-26 09:39:09 -05:00
< / FormControl >
< Flex mt = { 4 } gap = { 3 } >
< Button
bg = 'ui.main'
color = 'white'
_hover = { { opacity : 0.8 } }
onClick = { toggleEditMode }
type = { editMode ? 'button' : 'submit' }
isLoading = { editMode ? isSubmitting : false }
2024-03-07 19:16:23 +01:00
isDisabled = { editMode ? ! isDirty : false }
2024-02-26 09:39:09 -05:00
>
{ editMode ? 'Save' : 'Edit' }
< / Button >
{ editMode &&
2024-02-28 18:30:59 -05:00
< Button onClick = { onCancel } isDisabled = { isSubmitting } >
2024-02-26 09:39:09 -05:00
Cancel
< / Button > }
< / Flex >
< / Box >
< / C o n t a i n e r >
< / >
) ;
}
export default UserInformation ;