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

151 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-03-08 14:58:36 +01:00
import {
Box,
Button,
Container,
Flex,
Heading,
Input,
Text,
2024-03-17 17:28:45 +01:00
} from "@chakra-ui/react"
2024-04-08 15:49:22 -05:00
import { useMutation, useQueryClient } from "@tanstack/react-query"
2024-03-17 17:28:45 +01:00
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 {
type ApiError,
type UserPublic,
2024-03-17 17:28:45 +01:00
type UserUpdateMe,
2024-03-28 21:15:11 -05:00
UsersService,
} from "@/client"
import useAuth from "@/hooks/useAuth"
import useCustomToast from "@/hooks/useCustomToast"
import { emailPattern, handleError } from "@/utils"
import { Field } from "../ui/field"
const UserInformation = () => {
2024-03-08 14:58:36 +01:00
const queryClient = useQueryClient()
const { showSuccessToast } = useCustomToast()
2024-03-08 14:58:36 +01:00
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<UserPublic>({
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({
mutationFn: (data: UserUpdateMe) =>
UsersService.updateUserMe({ requestBody: data }),
onSuccess: () => {
showSuccessToast("User updated successfully.")
2024-03-08 14:58:36 +01:00
},
onError: (err: ApiError) => {
handleError(err)
},
onSettled: () => {
2024-08-01 13:01:03 -05:00
queryClient.invalidateQueries()
},
})
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">
2024-03-08 14:58:36 +01:00
<Heading size="sm" py={4}>
User Information
</Heading>
<Box
2025-02-21 20:10:51 +00:00
w={{ sm: "full", md: "sm" }}
as="form"
onSubmit={handleSubmit(onSubmit)}
>
<Field label="Full name">
2024-03-08 14:58:36 +01:00
{editMode ? (
<Input
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
fontSize="md"
2024-03-12 14:58:44 +01:00
py={2}
color={!currentUser?.full_name ? "gray" : "inherit"}
truncate
2025-02-21 20:10:51 +00:00
maxW="sm"
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>
)}
</Field>
<Field
mt={4}
label="Email"
invalid={!!errors.email}
errorText={errors.email?.message}
>
2024-03-08 14:58:36 +01:00
{editMode ? (
<Input
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"
/>
) : (
2025-02-21 20:10:51 +00:00
<Text fontSize="md" py={2} truncate maxW="sm">
{currentUser?.email}
2024-03-08 14:58:36 +01:00
</Text>
)}
</Field>
2024-03-08 14:58:36 +01:00
<Flex mt={4} gap={3}>
<Button
variant="solid"
2024-03-08 14:58:36 +01:00
onClick={toggleEditMode}
2024-03-17 17:28:45 +01:00
type={editMode ? "button" : "submit"}
loading={editMode ? isSubmitting : false}
disabled={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
variant="subtle"
colorPalette="gray"
onClick={onCancel}
disabled={isSubmitting}
>
2024-03-08 14:58:36 +01:00
Cancel
</Button>
)}
</Flex>
</Box>
</Container>
</>
)
}
2024-03-08 14:58:36 +01:00
export default UserInformation