Added Violation component and modifeid Comment Componnent and Delete Component
thats all related to RequestComponentmaster
parent
d82592c189
commit
f8d9513d6a
|
|
@ -46,7 +46,7 @@ function RecentRequests() {
|
|||
<div className="w-full rounded-md bg-rose-100">
|
||||
<div id="map" aria-label="نقشه">
|
||||
{/* <OnMapRequests /> */}
|
||||
<ComponentMap />
|
||||
{/* <ComponentMap /> */}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@ import { convertFullTime } from "@/lib/utils";
|
|||
import { useRouter } from "next/navigation";
|
||||
import RequestViolations from "./RequestViolations";
|
||||
|
||||
import { postReportViolation } from "@/utils/CitizenReport";
|
||||
|
||||
// interface RequestCardProps {
|
||||
// isSelfRequest?: boolean;
|
||||
// }
|
||||
|
|
@ -34,14 +36,24 @@ interface RequestItem {
|
|||
medias: Media[];
|
||||
isLiked: boolean;
|
||||
likes: number;
|
||||
commentsCount:number;
|
||||
}
|
||||
commentsCount: number;
|
||||
}
|
||||
|
||||
interface RequestCardProps {
|
||||
item: RequestItem;
|
||||
isSelfRequest?: boolean;
|
||||
}
|
||||
|
||||
// Define the post violation function type
|
||||
type PostViolationArgs = {
|
||||
id: string;
|
||||
payload: {
|
||||
instanceId: number;
|
||||
violationTypeId: number;
|
||||
description: string;
|
||||
};
|
||||
};
|
||||
|
||||
const RequestCard: React.FC<RequestCardProps> = ({ item, isSelfRequest }) => {
|
||||
const router = useRouter();
|
||||
|
||||
|
|
@ -50,6 +62,15 @@ const RequestCard: React.FC<RequestCardProps> = ({ item, isSelfRequest }) => {
|
|||
router.push(`/User/MyRequest/${item.id}`);
|
||||
}
|
||||
};
|
||||
|
||||
// Correctly wrapped function
|
||||
const wrappedPostReportViolation = async ({
|
||||
id,
|
||||
payload,
|
||||
}: PostViolationArgs) => {
|
||||
return postReportViolation(id, payload);
|
||||
};
|
||||
|
||||
return (
|
||||
// <div className="overflow-x-hidden overflow-y-scroll pl-5 scrollbar-thin scrollbar-thumb-yellow scrollbar-track-white">
|
||||
<>
|
||||
|
|
@ -105,13 +126,22 @@ const RequestCard: React.FC<RequestCardProps> = ({ item, isSelfRequest }) => {
|
|||
<div className="w-full flex items-center justify-between mb-[10px]">
|
||||
<RequestProcessIcon />
|
||||
<div className="flex items-center justify-center">
|
||||
<RequestViolations />
|
||||
{/* <RequestViolations reportId={item.id} /> */}
|
||||
|
||||
<RequestViolations
|
||||
reportId={item.id}
|
||||
mutationFn={wrappedPostReportViolation}
|
||||
/>
|
||||
|
||||
<RequestLike
|
||||
reportId={item.id}
|
||||
initialIsLiked={item.isLiked}
|
||||
initialLikes={item.likes}
|
||||
/>
|
||||
<RequestComments reportId={item.id} commentsCount={item.commentsCount}/>
|
||||
<RequestComments
|
||||
reportId={item.id}
|
||||
commentsCount={item.commentsCount}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -5,9 +5,10 @@ import RequestViolation from "./RequestViolation";
|
|||
import { BsTrash3 } from "react-icons/bs";
|
||||
import ph from "@/assets/images/ph.png";
|
||||
import Image from "next/image";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { deleteComment } from "@/utils/CitizenReport";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { deleteComment, postCommentViolation } from "@/utils/CitizenReport";
|
||||
import { toast } from "sonner";
|
||||
import RequestViolations from "./RequestViolations";
|
||||
|
||||
interface RequestCommentProps {
|
||||
id: string;
|
||||
|
|
@ -15,9 +16,21 @@ interface RequestCommentProps {
|
|||
dateTime: string;
|
||||
user: any;
|
||||
canDelete: boolean;
|
||||
onDeleteCommentSuccess:any
|
||||
onDeleteCommentSuccess: any;
|
||||
commentsCount: () => void;
|
||||
}
|
||||
|
||||
// Define the post violation function type
|
||||
type PostViolationArgs = {
|
||||
id: string;
|
||||
payload: {
|
||||
instanceId: number;
|
||||
violationTypeId: number;
|
||||
description: string;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
const getUserTitle = (user: any) => {
|
||||
if (user.firstName || user.lastName)
|
||||
return `${user.firstName || ""} ${user.lastName || ""}`;
|
||||
|
|
@ -32,14 +45,18 @@ const RequestComment: React.FC<RequestCommentProps> = ({
|
|||
user,
|
||||
canDelete,
|
||||
onDeleteCommentSuccess,
|
||||
commentsCount,
|
||||
}) => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const DeleteComment = useMutation({
|
||||
mutationKey: ["deleteComment"],
|
||||
mutationFn: deleteComment,
|
||||
onSuccess: (res) => {
|
||||
queryClient.invalidateQueries({ queryKey: ["Comments"] });
|
||||
commentsCount();
|
||||
toast(<>{res.message}</>, {
|
||||
className: "!bg-green !text-black",
|
||||
className: "!bg-green !text-black flex justify-end",
|
||||
});
|
||||
},
|
||||
onError: (error: any | string) => {
|
||||
|
|
@ -53,6 +70,13 @@ const RequestComment: React.FC<RequestCommentProps> = ({
|
|||
const handelDelete = () => {
|
||||
DeleteComment.mutate(id);
|
||||
};
|
||||
// Correctly wrapped function
|
||||
const wrappedPostCommentViolation = async ({
|
||||
id,
|
||||
payload,
|
||||
}: PostViolationArgs) => {
|
||||
return postCommentViolation(id, payload);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex w-full items-stretch mb-3 p-2 border-b-[1px] border-b-gray-200 last:border-b-0">
|
||||
|
|
@ -88,9 +112,15 @@ const RequestComment: React.FC<RequestCommentProps> = ({
|
|||
{comment}
|
||||
</p>
|
||||
{canDelete && (
|
||||
<BsTrash3 className="text-red cursor-pointer" onClick={handelDelete} />
|
||||
<BsTrash3
|
||||
className="text-red cursor-pointer"
|
||||
onClick={handelDelete}
|
||||
/>
|
||||
)}
|
||||
<RequestViolation />
|
||||
<RequestViolations
|
||||
reportId={id}
|
||||
mutationFn={wrappedPostCommentViolation}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -31,9 +31,9 @@ interface Comment {
|
|||
id: string;
|
||||
text: string;
|
||||
dateTime: string;
|
||||
user:any;
|
||||
canDelete:boolean;
|
||||
onDeleteCommentSuccess:any
|
||||
user: any;
|
||||
canDelete: boolean;
|
||||
onDeleteCommentSuccess: any;
|
||||
}
|
||||
|
||||
// Define the post comment function type
|
||||
|
|
@ -75,8 +75,8 @@ const RequestComments: React.FC<RequestCommentsProps> = ({
|
|||
setCommentText("");
|
||||
// Increment the comments count
|
||||
setCommentsCounts((prevCount) => prevCount + 1);
|
||||
toast("successfully", {
|
||||
className: "!bg-green !text-black",
|
||||
toast("نظر شما با موفقیت ثبت شد", {
|
||||
className: "!bg-green !text-black flex justify-end",
|
||||
});
|
||||
},
|
||||
onError: (error) => {
|
||||
|
|
@ -126,9 +126,12 @@ const RequestComments: React.FC<RequestCommentsProps> = ({
|
|||
id={comment.id}
|
||||
comment={comment.text}
|
||||
dateTime={comment.dateTime}
|
||||
user={comment.user}
|
||||
user={comment.user}
|
||||
canDelete={comment.canDelete}
|
||||
onDeleteCommentSuccess={refetch()}
|
||||
commentsCount={() =>
|
||||
setCommentsCounts((prev) => prev - 1)
|
||||
}
|
||||
/>
|
||||
</>
|
||||
))}
|
||||
|
|
@ -151,7 +154,7 @@ const RequestComments: React.FC<RequestCommentsProps> = ({
|
|||
wrapperClass=""
|
||||
/>
|
||||
) : (
|
||||
<MdSend className="w-5 h-5 text-green"/>
|
||||
<MdSend className="w-5 h-5 text-green" />
|
||||
)}
|
||||
</button>
|
||||
<Textarea
|
||||
|
|
|
|||
|
|
@ -29,27 +29,54 @@ import {
|
|||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectGroup,
|
||||
SelectItem,
|
||||
SelectLabel,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "../ui/select";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
import { postReportViolation } from "@/utils/CitizenReport";
|
||||
import { toast } from "sonner";
|
||||
import { FaCheckCircle } from "react-icons/fa";
|
||||
|
||||
const formSchema = z.object({
|
||||
violationTypeId: z.string({
|
||||
required_error: "لطفا دسته بندی را انتخاب نمایید.",
|
||||
}),
|
||||
description: z.string(),
|
||||
});
|
||||
interface RequestViolationsProps {
|
||||
reportId: string;
|
||||
mutationFn: ({ id, payload }: PostViolationArgs) => Promise<any>;
|
||||
}
|
||||
|
||||
const RequestViolations = ({ request = {}, comment = {}, type = "" }) => {
|
||||
// Define the post violation function type
|
||||
type PostViolationArgs = {
|
||||
id: string;
|
||||
payload: {
|
||||
instanceId: number;
|
||||
violationTypeId: number;
|
||||
description: string;
|
||||
};
|
||||
};
|
||||
|
||||
const RequestViolations: React.FC<RequestViolationsProps> = ({
|
||||
reportId,
|
||||
mutationFn,
|
||||
}) => {
|
||||
// states
|
||||
const [currentStep, setCurrentStep] = useState<number>(1);
|
||||
const [violationType, setViolationType] = useState("");
|
||||
const [instanceId, setInstanceId] = useState<number>(1);
|
||||
const [violationDescription, setViolationDescription] = useState("");
|
||||
const [charCount, setCharCount] = useState(0);
|
||||
const [currentStep, setCurrentStep] = useState<number>(1);
|
||||
const [open, setOpen] = useState<boolean>();
|
||||
const [open2, setOpen2] = useState<boolean>(false);
|
||||
|
||||
useEffect(() => {
|
||||
const CtizenInstances = localStorage.getItem("SH_CT_INSTANCE");
|
||||
if (CtizenInstances) {
|
||||
const parsedItem = JSON.parse(CtizenInstances);
|
||||
setInstanceId(parsedItem.id);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const {
|
||||
data: violationTypes,
|
||||
|
|
@ -64,11 +91,16 @@ const RequestViolations = ({ request = {}, comment = {}, type = "" }) => {
|
|||
});
|
||||
|
||||
// functions
|
||||
const onSelectType = (vT: string) => (e: any) => {
|
||||
e.stopPropagation();
|
||||
setViolationType(vT);
|
||||
setCurrentStep((prev) => prev + 1);
|
||||
};
|
||||
// const onSelectType = (vT: string) => (e: any) => {
|
||||
// e.stopPropagation();
|
||||
// setViolationType(vT);
|
||||
// setCurrentStep((prev) => prev + 1);
|
||||
// };
|
||||
|
||||
const formSchema = z.object({
|
||||
violationTypeId: z.string().nonempty("لطفا دسته بندی را انتخاب نمایید."),
|
||||
description: z.string(),
|
||||
});
|
||||
|
||||
// 1. Define your form.
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
|
|
@ -83,83 +115,165 @@ const RequestViolations = ({ request = {}, comment = {}, type = "" }) => {
|
|||
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 violationMutation = useMutation({
|
||||
mutationKey: ["violationMutation"],
|
||||
// mutationFn: ({ id, payload }: PostViolationArgs) =>
|
||||
// postReportViolation(id, payload),
|
||||
mutationFn: mutationFn,
|
||||
onSuccess: (res) => {
|
||||
// toast(<>{"گزارش شما با موفقیت ثبت شد"}</>, {
|
||||
// className: "!bg-green !text-white flex justify-end",
|
||||
// });
|
||||
setOpen(false);
|
||||
setOpen2(true);
|
||||
},
|
||||
onError: (err: any | string) => {
|
||||
toast(<>{err.response.data.detail}</>, {
|
||||
className: "!bg-rose-800 !text-white",
|
||||
});
|
||||
refetch();
|
||||
},
|
||||
});
|
||||
|
||||
// Function to search for an ID based on the title
|
||||
function findViolationTypeIdByTitle(title: string) {
|
||||
if (violationTypes?.data) {
|
||||
const foundItem = violationTypes.data.find(
|
||||
(item: any) => item.title === title
|
||||
);
|
||||
return foundItem ? foundItem.id : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const handleSubmit = () => {};
|
||||
return (
|
||||
<Dialog>
|
||||
<DialogTrigger asChild>
|
||||
{/* <Button variant="outline">Share</Button> */}
|
||||
<FaRegFlag className="text-lg mx-1 cursor-pointer" />
|
||||
</DialogTrigger>
|
||||
// 2. Define a submit handler.
|
||||
function onSubmit(values: z.infer<typeof formSchema>) {
|
||||
console.log(values);
|
||||
setViolationType(values.violationTypeId);
|
||||
setViolationDescription(values.description);
|
||||
|
||||
<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">
|
||||
const violationId = findViolationTypeIdByTitle(values.violationTypeId);
|
||||
|
||||
violationMutation.mutate({
|
||||
id: reportId,
|
||||
payload: {
|
||||
instanceId: instanceId,
|
||||
violationTypeId: violationId,
|
||||
description: values.description,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
{/* <Button variant="outline">Share</Button> */}
|
||||
<FaRegFlag className="text-lg mx-1 cursor-pointer" />
|
||||
</DialogTrigger>
|
||||
|
||||
<DialogContent className="sm:max-w-xl">
|
||||
<div className="">
|
||||
<Form {...form}>
|
||||
<form
|
||||
onSubmit={form.handleSubmit(onSubmit)}
|
||||
className="flex flex-col gap-8 w-full mt-6"
|
||||
>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="violationTypeId"
|
||||
render={({ field }) => (
|
||||
<FormItem className="w-full mb-4">
|
||||
{/* <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>
|
||||
)}
|
||||
/>
|
||||
<Select
|
||||
onValueChange={field.onChange}
|
||||
defaultValue={field.value}
|
||||
dir="rtl"
|
||||
>
|
||||
<FormControl>
|
||||
<SelectTrigger className="bg-white">
|
||||
<SelectValue placeholder=" عنوان تخلف" />
|
||||
</SelectTrigger>
|
||||
</FormControl>
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
{/* <SelectLabel>Fruits</SelectLabel> */}
|
||||
{violationTypes?.data.map(
|
||||
(Items: any, index: any) => (
|
||||
<SelectItem key={index} value={Items.title}>
|
||||
{Items.title}
|
||||
</SelectItem>
|
||||
)
|
||||
)}
|
||||
</SelectGroup>
|
||||
</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>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="description"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel className="text-[#7C838A] text-sm font-medium">
|
||||
توضیحات ({charCount}/1024)
|
||||
</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
placeholder=" توضیحات خود را بنویسید"
|
||||
className=" resize-none bg-white text-black text-sm "
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
{/* <DialogClose asChild> */}
|
||||
<Button
|
||||
type="submit"
|
||||
className="w-1/2 p-2 text-center text-black sm:w-1/3 lg:w-1/4 bg-yellow hover:bg-yellow rounded-xl self-end"
|
||||
>
|
||||
{/* {complaintSubmitMutation.isPending ? (
|
||||
<div className="flex items-center justify-center gap-2">
|
||||
<Oval
|
||||
visible={true}
|
||||
height="20"
|
||||
width="20"
|
||||
color="#fff"
|
||||
ariaLabel="oval-loading"
|
||||
wrapperStyle={{}}
|
||||
wrapperClass=""
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
"ارسال گزارش"
|
||||
)} */}
|
||||
ارسال
|
||||
</Button>
|
||||
{/* </DialogClose> */}
|
||||
</form>
|
||||
</Form>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
||||
<Dialog open={open2} onOpenChange={setOpen2}>
|
||||
<DialogContent>
|
||||
<div className="flex flex-col justify-center items-center gap-8">
|
||||
<FaCheckCircle className="text-green w-12 h-12 " />
|
||||
<p className="text-green text-sm">
|
||||
شهروند گرامی، گزارش تخلف شما ثبت شد و در اسرع وقت بررسی میگردد.
|
||||
</p>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -133,10 +133,9 @@ export async function postCommentViolation(
|
|||
id: string,
|
||||
payload: { instanceId: number; violationTypeId: number; description: string }
|
||||
) {
|
||||
const data = await axios.post(`/api/CitizenReport/CommentViolation/${id}`, {
|
||||
payload,
|
||||
});
|
||||
const data = await axios.post(`/api/CitizenReport/CommentViolation/${id}`,payload);
|
||||
return data.data;
|
||||
|
||||
}
|
||||
|
||||
// Post CitizenReport ReportViolation {id}
|
||||
|
|
@ -144,8 +143,6 @@ export async function postReportViolation(
|
|||
id: string,
|
||||
payload: { instanceId: number; violationTypeId: number; description: string }
|
||||
) {
|
||||
const data = await axios.post(`/api/CitizenReport/ReportViolation/${id}`, {
|
||||
payload,
|
||||
});
|
||||
const data = await axios.post(`/api/CitizenReport/ReportViolation/${id}`,payload);
|
||||
return data.data;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue