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"
|
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,
|
2024-03-17 17:28:45 +01:00
|
|
|
} from "../../client"
|
|
|
|
import useAuth from "../../hooks/useAuth"
|
|
|
|
import useCustomToast from "../../hooks/useCustomToast"
|
2024-03-28 19:49:02 -05:00
|
|
|
import { emailPattern } from "../../utils"
|
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()
|
2024-04-01 22:53:33 -05:00
|
|
|
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,
|
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: () => {
|
|
|
|
showToast("Success!", "User updated successfully.", "success")
|
2024-03-08 14:58:36 +01:00
|
|
|
},
|
2024-04-04 16:30:42 +02:00
|
|
|
onError: (err: ApiError) => {
|
|
|
|
const errDetail = (err.body as any)?.detail
|
|
|
|
showToast("Something went wrong.", `${errDetail}`, "error")
|
|
|
|
},
|
|
|
|
onSettled: () => {
|
|
|
|
// TODO: can we do just one call now?
|
|
|
|
queryClient.invalidateQueries({ queryKey: ["users"] })
|
|
|
|
queryClient.invalidateQueries({ queryKey: ["currentUser"] })
|
|
|
|
},
|
|
|
|
})
|
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
|
|
|
|
w={{ sm: "full", md: "50%" }}
|
|
|
|
as="form"
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
|
|
>
|
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}
|
2024-04-01 22:53:33 -05:00
|
|
|
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",
|
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"
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Text size="md" py={2}>
|
2024-03-13 22:59:28 +01:00
|
|
|
{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-02-26 09:39:09 -05:00
|
|
|
}
|
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
export default UserInformation
|