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-02-26 09:39:09 -05:00
|
|
|
|
2024-03-17 17:28:45 +01:00
|
|
|
import {
|
|
|
|
type ApiError,
|
2024-04-06 18:26:12 -05:00
|
|
|
type UserPublic,
|
2024-03-17 17:28:45 +01:00
|
|
|
type UserUpdateMe,
|
2024-03-28 21:15:11 -05:00
|
|
|
UsersService,
|
2025-02-17 19:55:20 +00:00
|
|
|
} from "@/client"
|
|
|
|
import useAuth from "@/hooks/useAuth"
|
|
|
|
import useCustomToast from "@/hooks/useCustomToast"
|
|
|
|
import { emailPattern, handleError } from "@/utils"
|
2025-02-17 19:33:00 +00:00
|
|
|
import { Field } from "../ui/field"
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-03-28 20:22:28 -05:00
|
|
|
const UserInformation = () => {
|
2024-03-08 14:58:36 +01:00
|
|
|
const queryClient = useQueryClient()
|
2025-02-17 19:33:00 +00:00
|
|
|
const { showSuccessToast } = useCustomToast()
|
2024-03-08 14:58:36 +01:00
|
|
|
const [editMode, setEditMode] = useState(false)
|
|
|
|
const { user: currentUser } = useAuth()
|
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
reset,
|
2024-03-08 17:43:21 +01:00
|
|
|
getValues,
|
2024-03-08 14:58:36 +01:00
|
|
|
formState: { isSubmitting, errors, isDirty },
|
2024-04-06 18:26:12 -05:00
|
|
|
} = 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-02-26 09:39:09 -05:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
const toggleEditMode = () => {
|
|
|
|
setEditMode(!editMode)
|
|
|
|
}
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-04-04 16:30:42 +02:00
|
|
|
const mutation = useMutation({
|
|
|
|
mutationFn: (data: UserUpdateMe) =>
|
|
|
|
UsersService.updateUserMe({ requestBody: data }),
|
|
|
|
onSuccess: () => {
|
2025-02-17 19:33:00 +00:00
|
|
|
showSuccessToast("User updated successfully.")
|
2024-03-08 14:58:36 +01:00
|
|
|
},
|
2024-04-04 16:30:42 +02:00
|
|
|
onError: (err: ApiError) => {
|
2025-02-17 19:33:00 +00:00
|
|
|
handleError(err)
|
2024-04-04 16:30:42 +02:00
|
|
|
},
|
|
|
|
onSettled: () => {
|
2024-08-01 13:01:03 -05:00
|
|
|
queryClient.invalidateQueries()
|
2024-04-04 16:30:42 +02:00
|
|
|
},
|
|
|
|
})
|
2024-03-07 19:16:23 +01:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
const onSubmit: SubmitHandler<UserUpdateMe> = async (data) => {
|
|
|
|
mutation.mutate(data)
|
|
|
|
}
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
const onCancel = () => {
|
|
|
|
reset()
|
|
|
|
toggleEditMode()
|
|
|
|
}
|
2024-02-26 09:39:09 -05:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
return (
|
|
|
|
<>
|
2024-05-20 13:30:18 -05:00
|
|
|
<Container maxW="full">
|
2024-03-08 14:58:36 +01:00
|
|
|
<Heading size="sm" py={4}>
|
|
|
|
User Information
|
|
|
|
</Heading>
|
2024-05-20 13:30:18 -05:00
|
|
|
<Box
|
2025-02-21 20:10:51 +00:00
|
|
|
w={{ sm: "full", md: "sm" }}
|
2024-05-20 13:30:18 -05:00
|
|
|
as="form"
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
|
|
>
|
2025-02-17 19:33:00 +00:00
|
|
|
<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
|
2025-02-17 19:33:00 +00:00
|
|
|
fontSize="md"
|
2024-03-12 14:58:44 +01:00
|
|
|
py={2}
|
2025-02-17 19:33:00 +00:00
|
|
|
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>
|
|
|
|
)}
|
2025-02-17 19:33:00 +00:00
|
|
|
</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",
|
2024-03-28 19:49:02 -05:00
|
|
|
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">
|
2024-03-13 22:59:28 +01:00
|
|
|
{currentUser?.email}
|
2024-03-08 14:58:36 +01:00
|
|
|
</Text>
|
|
|
|
)}
|
2025-02-17 19:33:00 +00:00
|
|
|
</Field>
|
2024-03-08 14:58:36 +01:00
|
|
|
<Flex mt={4} gap={3}>
|
|
|
|
<Button
|
2025-02-17 19:33:00 +00:00
|
|
|
variant="solid"
|
2024-03-08 14:58:36 +01:00
|
|
|
onClick={toggleEditMode}
|
2024-03-17 17:28:45 +01:00
|
|
|
type={editMode ? "button" : "submit"}
|
2025-02-17 19:33:00 +00:00
|
|
|
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 && (
|
2025-02-17 19:33:00 +00:00
|
|
|
<Button
|
|
|
|
variant="subtle"
|
|
|
|
colorPalette="gray"
|
|
|
|
onClick={onCancel}
|
|
|
|
disabled={isSubmitting}
|
|
|
|
>
|
2024-03-08 14:58:36 +01:00
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</Flex>
|
|
|
|
</Box>
|
|
|
|
</Container>
|
|
|
|
</>
|
|
|
|
)
|
2024-02-26 09:39:09 -05:00
|
|
|
}
|
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
export default UserInformation
|