import { zodResolver } from "@hookform/resolvers/zod" import { useMutation, useQueryClient } from "@tanstack/react-query" import { Plus } from "lucide-react" import { useState } from "react" import { useForm } from "react-hook-form" import { z } from "zod" import { type LocationCreate, LocationsService } from "@/client" import { Button } from "@/components/ui/button" import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { LoadingButton } from "@/components/ui/loading-button" import useCustomToast from "@/hooks/useCustomToast" import { handleError } from "@/utils" const formSchema = z.object({ title: z.string().min(1, { message: "Title is required" }), description: z.string().optional(), }) type FormData = z.infer const AddLocation = () => { const [isOpen, setIsOpen] = useState(false) const queryClient = useQueryClient() const { showSuccessToast, showErrorToast } = useCustomToast() const form = useForm({ resolver: zodResolver(formSchema), mode: "onBlur", criteriaMode: "all", defaultValues: { title: "", description: "", }, }) const mutation = useMutation({ mutationFn: (data: LocationCreate) => LocationsService.createLocation({ requestBody: data }), onSuccess: () => { showSuccessToast("Location created successfully") form.reset() setIsOpen(false) }, onError: handleError.bind(showErrorToast), onSettled: () => { queryClient.invalidateQueries({ queryKey: ["locations"] }) }, }) const onSubmit = (data: FormData) => { mutation.mutate(data) } return ( Add Location Fill in the details to add a new location.
( Title * )} /> ( Description )} />
Save
) } export default AddLocation