shahrbin-citizen/components/Requests/RequestViolations.tsx

167 lines
4.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"use client";
import React, { MouseEventHandler, useEffect, useState } from "react";
import { useMutation, useQuery } from "@tanstack/react-query";
import { getViolationTypes } from "@/utils/CitizenCommon";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "../ui/textarea";
import { Oval } from "react-loader-spinner";
import { FaRegFlag } from "react-icons/fa";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "../ui/form";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "../ui/select";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
const formSchema = z.object({
violationTypeId: z.string({
required_error: "لطفا دسته بندی را انتخاب نمایید.",
}),
description: z.string(),
});
const RequestViolations = ({ request = {}, comment = {}, type = "" }) => {
// states
const [currentStep, setCurrentStep] = useState<number>(1);
const [violationType, setViolationType] = useState("");
const [violationDescription, setViolationDescription] = useState("");
const [charCount, setCharCount] = useState(0);
const {
data: violationTypes,
isLoading,
isError,
isSuccess,
refetch,
} = useQuery({
queryKey: ["Violation"],
queryFn: () => getViolationTypes(),
// refetchOnWindowFocus: false,
});
// functions
const onSelectType = (vT: string) => (e: any) => {
e.stopPropagation();
setViolationType(vT);
setCurrentStep((prev) => prev + 1);
};
// 1. Define your form.
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
violationTypeId: "",
description: "",
},
});
useEffect(() => {
setCharCount(form.watch("description").length);
}, [form.watch("description")]);
// 2. Define a submit handler.
function onSubmit(values: z.infer<typeof formSchema>) {
// Do something with the form values.
// ✅ This will be type-safe and validated.
console.log(values);
}
const handleSubmit = () => {};
return (
<Dialog>
<DialogTrigger asChild>
{/* <Button variant="outline">Share</Button> */}
<FaRegFlag className="text-lg mx-1 cursor-pointer" />
</DialogTrigger>
<DialogContent className="sm:max-w-md">
<div className="">
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="flex flex-col w-full mt-6"
>
<FormField
control={form.control}
name="violationTypeId"
render={({ field }) => (
<FormItem className="w-full">
{/* <FormLabel className="text-[#7C838A] text-sm font-medium mt-7">
عنوان تخلف
</FormLabel> */}
<Select
onValueChange={field.onChange}
defaultValue={field.value}
dir="rtl"
>
<FormControl>
<SelectTrigger className="bg-slate-200">
<SelectValue placeholder=" عنوان تخلف" />
</SelectTrigger>
</FormControl>
<SelectContent>
{violationTypes?.data.map((Items: any, index: any) => (
<SelectItem key={index} value={Items.id}>
{Items.title}
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage className="mt-2 " />
</FormItem>
)}
/>
<FormField
control={form.control}
name="description"
render={({ field }) => (
<FormItem>
<FormLabel className="text-[#7C838A] text-sm font-medium ">
توضیحات ({charCount}/1024)
</FormLabel>
<FormControl>
<Textarea
placeholder=" توضیحات خود را بنویسید"
className="mb-3 resize-none"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
</div>
</DialogContent>
</Dialog>
);
};
export default RequestViolations;