101 lines
2.6 KiB
TypeScript
101 lines
2.6 KiB
TypeScript
"use client";
|
|
import React from "react";
|
|
import { convertFullTime } from "@/lib/utils";
|
|
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 { toast } from "sonner";
|
|
|
|
interface RequestCommentProps {
|
|
id: string;
|
|
comment: string;
|
|
dateTime: string;
|
|
user: any;
|
|
canDelete: boolean;
|
|
onDeleteCommentSuccess:any
|
|
}
|
|
|
|
const getUserTitle = (user: any) => {
|
|
if (user.firstName || user.lastName)
|
|
return `${user.firstName || ""} ${user.lastName || ""}`;
|
|
else if (user.title) return user.title;
|
|
else return "شهروند";
|
|
};
|
|
|
|
const RequestComment: React.FC<RequestCommentProps> = ({
|
|
id,
|
|
comment,
|
|
dateTime,
|
|
user,
|
|
canDelete,
|
|
onDeleteCommentSuccess,
|
|
}) => {
|
|
|
|
const DeleteComment = useMutation({
|
|
mutationKey: ["deleteComment"],
|
|
mutationFn: deleteComment,
|
|
onSuccess: (res) => {
|
|
toast(<>{res.message}</>, {
|
|
className: "!bg-green !text-black",
|
|
});
|
|
},
|
|
onError: (error: any | string) => {
|
|
toast(<>{error.message}</>, {
|
|
className: "!bg-red-500 !text-white",
|
|
});
|
|
onDeleteCommentSuccess();
|
|
},
|
|
});
|
|
|
|
const handelDelete = () => {
|
|
DeleteComment.mutate(id);
|
|
};
|
|
|
|
return (
|
|
<div className="flex w-full items-stretch mb-3 p-2 border-b-[1px] border-b-gray-200 last:border-b-0">
|
|
<div className="w-12 h-12 ml-2">
|
|
<Image
|
|
className="object-cover rounded-full w-full h-full"
|
|
alt="picture"
|
|
quality={100}
|
|
height={50}
|
|
width={50}
|
|
src={
|
|
user?.avatar?.url3
|
|
? `${process.env.NEXT_PUBLIC_BASE_URL_APP}${user?.avatar?.url3}`
|
|
: ph
|
|
}
|
|
/>
|
|
</div>
|
|
<div className="flex flex-1 flex-col">
|
|
<div className="flex w-full items-center justify-between">
|
|
<p className="text-xs font-bold text-gray-500">
|
|
{getUserTitle(user)}
|
|
</p>
|
|
<span className="text-xs font-bold text-gray-500">
|
|
{convertFullTime(dateTime)}
|
|
</span>
|
|
</div>
|
|
|
|
<div className="flex flex-1 items-center justify-between">
|
|
<p
|
|
className="flex flex-1 text-justify text-sm text-black"
|
|
style={{ lineBreak: "anywhere" }}
|
|
>
|
|
{comment}
|
|
</p>
|
|
{canDelete && (
|
|
<BsTrash3 className="text-red cursor-pointer" onClick={handelDelete} />
|
|
)}
|
|
<RequestViolation />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default RequestComment;
|