import React, { useState } from 'react'; import { Box, Button, Container, Flex, FormControl, FormLabel, Heading, Input, Text, useColorModeValue } from '@chakra-ui/react'; import { SubmitHandler, useForm } from 'react-hook-form'; import { ApiError, UserOut, UserUpdateMe } from '../../client'; import useCustomToast from '../../hooks/useCustomToast'; import { useUserStore } from '../../store/user-store'; import { useUsersStore } from '../../store/users-store'; const UserInformation: React.FC = () => { const color = useColorModeValue('gray.700', 'white'); const showToast = useCustomToast(); const [editMode, setEditMode] = useState(false); const { register, handleSubmit, reset, formState: { isSubmitting } } = useForm(); const { user, editUser } = useUserStore(); const { getUsers } = useUsersStore(); const toggleEditMode = () => { setEditMode(!editMode); }; const onSubmit: SubmitHandler = async (data) => { try { await editUser(data); await getUsers() showToast('Success!', 'User updated successfully.', 'success'); } catch (err) { const errDetail = (err as ApiError).body.detail; showToast('Something went wrong.', `${errDetail}`, 'error'); } } const onCancel = () => { reset(); toggleEditMode(); } return ( <> User Information Full name { editMode ? : {user?.full_name || 'N/A'} } Email { editMode ? : {user?.email || 'N/A'} } {editMode && } ); } export default UserInformation;