import { Box, Button, Container, Flex, Heading, Input, Text, } from "@chakra-ui/react" import { useMutation, useQueryClient } from "@tanstack/react-query" import { useState } from "react" import { type SubmitHandler, useForm } from "react-hook-form" import { type ApiError, type UserPublic, type UserUpdateMe, 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 = () => { const queryClient = useQueryClient() const { showSuccessToast } = useCustomToast() const [editMode, setEditMode] = useState(false) const { user: currentUser } = useAuth() const { register, handleSubmit, reset, getValues, formState: { isSubmitting, errors, isDirty }, } = useForm({ mode: "onBlur", criteriaMode: "all", defaultValues: { full_name: currentUser?.full_name, email: currentUser?.email, }, }) const toggleEditMode = () => { setEditMode(!editMode) } const mutation = useMutation({ mutationFn: (data: UserUpdateMe) => UsersService.updateUserMe({ requestBody: data }), onSuccess: () => { showSuccessToast("User updated successfully.") }, onError: (err: ApiError) => { handleError(err) }, onSettled: () => { queryClient.invalidateQueries() }, }) const onSubmit: SubmitHandler = async (data) => { mutation.mutate(data) } const onCancel = () => { reset() toggleEditMode() } return ( <> User Information {editMode ? ( ) : ( {currentUser?.full_name || "N/A"} )} {editMode ? ( ) : ( {currentUser?.email} )} {editMode && ( )} ) } export default UserInformation