Files
full-stack-fastapi-template/frontend/src/components/UserSettings/UserInformation.tsx

153 lines
4.0 KiB
TypeScript
Raw Normal View History

2024-03-08 14:58:36 +01:00
import {
Box,
Button,
Container,
Flex,
FormControl,
FormErrorMessage,
FormLabel,
Heading,
Input,
Text,
useColorModeValue,
2024-03-17 17:28:45 +01:00
} from "@chakra-ui/react"
import { useState } from "react"
2024-03-28 21:15:11 -05:00
import { type SubmitHandler, useForm } from "react-hook-form"
2024-03-17 17:28:45 +01:00
import { useMutation, useQueryClient } from "react-query"
2024-03-17 17:28:45 +01:00
import {
type ApiError,
type UserOut,
type UserUpdateMe,
2024-03-28 21:15:11 -05:00
UsersService,
2024-03-17 17:28:45 +01:00
} from "../../client"
import useAuth from "../../hooks/useAuth"
import useCustomToast from "../../hooks/useCustomToast"
import { emailPattern } from "../../utils"
const UserInformation = () => {
2024-03-08 14:58:36 +01:00
const queryClient = useQueryClient()
const color = useColorModeValue("inherit", "ui.light")
2024-03-08 14:58:36 +01:00
const showToast = useCustomToast()
const [editMode, setEditMode] = useState(false)
const { user: currentUser } = useAuth()
const {
register,
handleSubmit,
reset,
getValues,
2024-03-08 14:58:36 +01:00
formState: { isSubmitting, errors, isDirty },
} = useForm<UserOut>({
2024-03-17 17:28:45 +01:00
mode: "onBlur",
criteriaMode: "all",
2024-03-08 14:58:36 +01:00
defaultValues: {
full_name: currentUser?.full_name,
email: currentUser?.email,
},
})
2024-03-08 14:58:36 +01:00
const toggleEditMode = () => {
setEditMode(!editMode)
}
const mutation = useMutation(
(data: UserUpdateMe) => UsersService.updateUserMe({ requestBody: data }),
{
onSuccess: () => {
showToast("Success!", "User updated successfully.", "success")
},
onError: (err: ApiError) => {
const errDetail = err.body?.detail
showToast("Something went wrong.", `${errDetail}`, "error")
},
onSettled: () => {
queryClient.invalidateQueries("users")
queryClient.invalidateQueries("currentUser")
},
2024-03-08 14:58:36 +01:00
},
)
2024-03-08 14:58:36 +01:00
const onSubmit: SubmitHandler<UserUpdateMe> = async (data) => {
mutation.mutate(data)
}
2024-03-08 14:58:36 +01:00
const onCancel = () => {
reset()
toggleEditMode()
}
2024-03-08 14:58:36 +01:00
return (
<>
<Container maxW="full" as="form" onSubmit={handleSubmit(onSubmit)}>
<Heading size="sm" py={4}>
User Information
</Heading>
2024-03-17 17:28:45 +01:00
<Box w={{ sm: "full", md: "50%" }}>
2024-03-08 14:58:36 +01:00
<FormControl>
<FormLabel color={color} htmlFor="name">
Full name
</FormLabel>
{editMode ? (
<Input
id="name"
2024-03-17 17:28:45 +01:00
{...register("full_name", { maxLength: 30 })}
2024-03-08 14:58:36 +01:00
type="text"
size="md"
/>
) : (
2024-03-12 14:58:44 +01:00
<Text
size="md"
py={2}
color={!currentUser?.full_name ? "ui.dim" : "inherit"}
2024-03-12 14:58:44 +01:00
>
2024-03-17 17:28:45 +01:00
{currentUser?.full_name || "N/A"}
2024-03-08 14:58:36 +01:00
</Text>
)}
</FormControl>
<FormControl mt={4} isInvalid={!!errors.email}>
<FormLabel color={color} htmlFor="email">
Email
</FormLabel>
{editMode ? (
<Input
id="email"
2024-03-17 17:28:45 +01:00
{...register("email", {
required: "Email is required",
pattern: emailPattern,
2024-03-08 14:58:36 +01:00
})}
2024-03-11 16:50:46 +01:00
type="email"
2024-03-08 14:58:36 +01:00
size="md"
/>
) : (
<Text size="md" py={2}>
{currentUser?.email}
2024-03-08 14:58:36 +01:00
</Text>
)}
{errors.email && (
<FormErrorMessage>{errors.email.message}</FormErrorMessage>
)}
</FormControl>
<Flex mt={4} gap={3}>
<Button
2024-03-11 16:50:46 +01:00
variant="primary"
2024-03-08 14:58:36 +01:00
onClick={toggleEditMode}
2024-03-17 17:28:45 +01:00
type={editMode ? "button" : "submit"}
2024-03-08 14:58:36 +01:00
isLoading={editMode ? isSubmitting : false}
2024-03-17 17:28:45 +01:00
isDisabled={editMode ? !isDirty || !getValues("email") : false}
2024-03-08 14:58:36 +01:00
>
2024-03-17 17:28:45 +01:00
{editMode ? "Save" : "Edit"}
2024-03-08 14:58:36 +01:00
</Button>
{editMode && (
<Button onClick={onCancel} isDisabled={isSubmitting}>
Cancel
</Button>
)}
</Flex>
</Box>
</Container>
</>
)
}
2024-03-08 14:58:36 +01:00
export default UserInformation