import { useMutation, useQueryClient } from "@tanstack/react-query" import { Controller, type SubmitHandler, useForm } from "react-hook-form" import { Button, DialogActionTrigger, DialogTitle, Flex, Input, Text, VStack, } from "@chakra-ui/react" import { useState } from "react" import { FaPlus } from "react-icons/fa" import { type UserCreate, UsersService } from "../../client" import type { ApiError } from "../../client/core/ApiError" import useCustomToast from "../../hooks/useCustomToast" import { emailPattern, handleError } from "../../utils" import { Checkbox } from "../ui/checkbox" import { DialogBody, DialogCloseTrigger, DialogContent, DialogFooter, DialogHeader, DialogRoot, DialogTrigger, } from "../ui/dialog" import { Field } from "../ui/field" interface UserCreateForm extends UserCreate { confirm_password: string } const AddUser = () => { const [isOpen, setIsOpen] = useState(false) const queryClient = useQueryClient() const { showSuccessToast } = useCustomToast() const { control, register, handleSubmit, reset, getValues, formState: { errors, isValid, isSubmitting }, } = useForm({ mode: "onBlur", criteriaMode: "all", defaultValues: { email: "", full_name: "", password: "", confirm_password: "", is_superuser: false, is_active: false, }, }) const mutation = useMutation({ mutationFn: (data: UserCreate) => UsersService.createUser({ requestBody: data }), onSuccess: () => { showSuccessToast("User created successfully.") reset() setIsOpen(false) }, onError: (err: ApiError) => { handleError(err) }, onSettled: () => { queryClient.invalidateQueries({ queryKey: ["users"] }) }, }) const onSubmit: SubmitHandler = (data) => { mutation.mutate(data) } return ( setIsOpen(open)} >
Add User Fill in the form below to add a new user to the system. value === getValues().password || "The passwords do not match", })} placeholder="Password" type="password" /> ( field.onChange(checked)} > Is superuser? )} /> ( field.onChange(checked)} > Is active? )} />
) } export default AddUser