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

184 lines
5.4 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"
import { useMutation, useQueryClient } from "@tanstack/react-query"
2024-03-28 21:15:11 -05:00
import { type UserCreate, UsersService } from "../../client"
2024-03-17 17:28:45 +01:00
import type { ApiError } from "../../client/core/ApiError"
import useCustomToast from "../../hooks/useCustomToast"
import { emailPattern } from "../../utils"
interface AddUserProps {
2024-03-08 14:58:36 +01:00
isOpen: boolean
onClose: () => void
}
interface UserCreateForm extends UserCreate {
2024-03-08 14:58:36 +01:00
confirm_password: string
}
const AddUser = ({ isOpen, onClose }: AddUserProps) => {
2024-03-08 14:58:36 +01:00
const queryClient = useQueryClient()
const showToast = useCustomToast()
const {
register,
handleSubmit,
reset,
getValues,
formState: { errors, isSubmitting },
} = useForm<UserCreateForm>({
2024-03-17 17:28:45 +01:00
mode: "onBlur",
criteriaMode: "all",
2024-03-08 14:58:36 +01:00
defaultValues: {
2024-03-17 17:28:45 +01:00
email: "",
full_name: "",
password: "",
confirm_password: "",
2024-03-08 14:58:36 +01:00
is_superuser: false,
is_active: false,
},
})
const mutation = useMutation({
mutationFn: (data: UserCreate) =>
UsersService.createUser({ requestBody: data }),
onSuccess: () => {
showToast("Success!", "User created successfully.", "success")
reset()
onClose()
2024-03-08 14:58:36 +01:00
},
onError: (err: ApiError) => {
const errDetail = (err.body as any)?.detail
showToast("Something went wrong.", `${errDetail}`, "error")
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: ["users"] })
},
})
2024-03-08 14:58:36 +01:00
const onSubmit: SubmitHandler<UserCreateForm> = (data) => {
mutation.mutate(data)
}
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>Add User</ModalHeader>
<ModalCloseButton />
<ModalBody pb={6}>
<FormControl isRequired 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} isInvalid={!!errors.full_name}>
<FormLabel htmlFor="name">Full name</FormLabel>
<Input
id="name"
2024-03-17 17:28:45 +01:00
{...register("full_name")}
2024-03-08 14:58:36 +01:00
placeholder="Full name"
type="text"
/>
{errors.full_name && (
<FormErrorMessage>{errors.full_name.message}</FormErrorMessage>
)}
</FormControl>
<FormControl mt={4} isRequired isInvalid={!!errors.password}>
<FormLabel htmlFor="password">Set Password</FormLabel>
<Input
id="password"
2024-03-17 17:28:45 +01:00
{...register("password", {
required: "Password is required",
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}
isRequired
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", {
required: "Please confirm your 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 mt={4}>
<FormControl>
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>
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>
<ModalFooter gap={3}>
2024-03-11 16:50:46 +01:00
<Button variant="primary" type="submit" isLoading={isSubmitting}>
2024-03-08 14:58:36 +01:00
Save
</Button>
<Button onClick={onClose}>Cancel</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
)
}
2024-03-08 14:58:36 +01:00
export default AddUser