Files
full-stack-fastapi-template/frontend/src/components/Admin/EditUser.tsx

184 lines
5.0 KiB
TypeScript
Raw Normal View History

2024-03-08 14:58:36 +01:00
import {
Button,
Checkbox,
Flex,
FormControl,
FormErrorMessage,
FormLabel,
Input,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
2024-03-17 17:28:45 +01:00
} from "@chakra-ui/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 { useMutation, useQueryClient } from "react-query"
2024-03-17 17:28:45 +01:00
import {
type ApiError,
type UserOut,
type UserUpdate,
2024-03-28 21:15:11 -05:00
UsersService,
2024-03-17 17:28:45 +01:00
} from "../../client"
import useCustomToast from "../../hooks/useCustomToast"
import { emailPattern } from "../../utils"
interface EditUserProps {
2024-03-08 14:58:36 +01:00
user: UserOut
isOpen: boolean
onClose: () => void
}
interface UserUpdateForm extends UserUpdate {
2024-03-08 14:58:36 +01:00
confirm_password: string
}
const EditUser = ({ user, isOpen, onClose }: EditUserProps) => {
2024-03-08 14:58:36 +01:00
const queryClient = useQueryClient()
const showToast = useCustomToast()
2024-03-08 14:58:36 +01:00
const {
register,
handleSubmit,
reset,
getValues,
formState: { errors, isSubmitting, isDirty },
} = useForm<UserUpdateForm>({
2024-03-17 17:28:45 +01:00
mode: "onBlur",
criteriaMode: "all",
2024-03-08 14:58:36 +01:00
defaultValues: user,
})
const mutation = useMutation(
(data: UserUpdateForm) =>
UsersService.updateUser({ userId: user.id, requestBody: data }),
{
onSuccess: () => {
showToast("Success!", "User updated successfully.", "success")
onClose()
},
onError: (err: ApiError) => {
const errDetail = err.body?.detail
showToast("Something went wrong.", `${errDetail}`, "error")
},
onSettled: () => {
queryClient.invalidateQueries("users")
},
2024-03-08 14:58:36 +01:00
},
)
2024-03-08 14:58:36 +01:00
const onSubmit: SubmitHandler<UserUpdateForm> = async (data) => {
2024-03-17 17:28:45 +01:00
if (data.password === "") {
data.password = undefined
}
2024-03-08 14:58:36 +01:00
mutation.mutate(data)
}
2024-03-08 14:58:36 +01:00
const onCancel = () => {
reset()
onClose()
}
2024-03-08 14:58:36 +01:00
return (
<>
<Modal
isOpen={isOpen}
onClose={onClose}
2024-03-17 17:28:45 +01:00
size={{ base: "sm", md: "md" }}
2024-03-08 14:58:36 +01:00
isCentered
>
<ModalOverlay />
<ModalContent as="form" onSubmit={handleSubmit(onSubmit)}>
<ModalHeader>Edit User</ModalHeader>
<ModalCloseButton />
<ModalBody pb={6}>
<FormControl isInvalid={!!errors.email}>
<FormLabel htmlFor="email">Email</FormLabel>
<Input
id="email"
2024-03-17 17:28:45 +01:00
{...register("email", {
required: "Email is required",
pattern: emailPattern,
2024-03-08 14:58:36 +01:00
})}
placeholder="Email"
type="email"
/>
{errors.email && (
<FormErrorMessage>{errors.email.message}</FormErrorMessage>
)}
</FormControl>
<FormControl mt={4}>
<FormLabel htmlFor="name">Full name</FormLabel>
2024-03-17 17:28:45 +01:00
<Input id="name" {...register("full_name")} type="text" />
2024-03-08 14:58:36 +01:00
</FormControl>
<FormControl mt={4} isInvalid={!!errors.password}>
<FormLabel htmlFor="password">Set Password</FormLabel>
<Input
id="password"
2024-03-17 17:28:45 +01:00
{...register("password", {
2024-03-08 14:58:36 +01:00
minLength: {
value: 8,
2024-03-17 17:28:45 +01:00
message: "Password must be at least 8 characters",
2024-03-08 14:58:36 +01:00
},
})}
placeholder="Password"
type="password"
/>
{errors.password && (
<FormErrorMessage>{errors.password.message}</FormErrorMessage>
)}
</FormControl>
<FormControl mt={4} isInvalid={!!errors.confirm_password}>
<FormLabel htmlFor="confirm_password">Confirm Password</FormLabel>
<Input
id="confirm_password"
2024-03-17 17:28:45 +01:00
{...register("confirm_password", {
2024-03-08 14:58:36 +01:00
validate: (value) =>
value === getValues().password ||
2024-03-17 17:28:45 +01:00
"The passwords do not match",
2024-03-08 14:58:36 +01:00
})}
placeholder="Password"
type="password"
/>
{errors.confirm_password && (
<FormErrorMessage>
{errors.confirm_password.message}
</FormErrorMessage>
)}
</FormControl>
<Flex>
<FormControl mt={4}>
2024-03-17 17:28:45 +01:00
<Checkbox {...register("is_superuser")} colorScheme="teal">
2024-03-08 14:58:36 +01:00
Is superuser?
</Checkbox>
</FormControl>
<FormControl mt={4}>
2024-03-17 17:28:45 +01:00
<Checkbox {...register("is_active")} colorScheme="teal">
2024-03-08 14:58:36 +01:00
Is active?
</Checkbox>
</FormControl>
</Flex>
</ModalBody>
2024-03-08 14:58:36 +01:00
<ModalFooter gap={3}>
<Button
2024-03-11 16:50:46 +01:00
variant="primary"
2024-03-08 14:58:36 +01:00
type="submit"
isLoading={isSubmitting}
isDisabled={!isDirty}
>
Save
</Button>
<Button onClick={onCancel}>Cancel</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
)
}
2024-03-08 14:58:36 +01:00
export default EditUser