shahrbin-citizen/components/Requests/RequestProcess.tsx

222 lines
7.9 KiB
TypeScript
Raw Permalink 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, { useEffect, useState } from "react";
import { CgMenuRightAlt } from "react-icons/cg";
import {
Drawer,
DrawerClose,
DrawerContent,
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer";
import { Button } from "../ui/button";
import { IoClose } from "react-icons/io5";
import { useQuery } from "@tanstack/react-query";
import { getReportHistory } from "@/utils/CitizenReport";
import { convertDate, convertFullTime, convertTime } from "@/lib/utils";
import { log } from "console";
import Image from "next/image";
import imgdocument from "@/assets/images/icons8-document-50.png";
import imgimage from "@/assets/images/icons8-image-50.png";
import imgvideo from "@/assets/images/icons8-video-camera-50.png";
import imgvoice from "@/assets/images/icons8-voice-id-50.png";
import axios from "axios";
// Define types for props
interface RequestProcessProps {
reportId: string;
}
interface Media {
url: string;
url3: string;
mediaType: number;
}
interface Reason {
description: string;
id: number;
reasonMeaning: number;
title: string;
transitions: [];
}
// Define types for History
interface History {
id: string;
message: string;
attachments: Media[];
dateTime: string;
isPublic: boolean;
reason: Reason;
}
const RequestProcess: React.FC<RequestProcessProps> = ({ reportId }) => {
// Component state
const [history, setHistory] = useState<History[]>([]);
const [step, setStep] = useState<number>();
const { data, isLoading, isError, isSuccess, refetch } = useQuery({
queryKey: ["History", reportId],
queryFn: () => getReportHistory(reportId),
// refetchOnWindowFocus: false,
});
// let newArr = [];
useEffect(() => {
console.log("Fetched data:", data); // Log the fetched data
if (data) {
setHistory(data?.data);
setStep(data?.data?.length);
}
}, [data]);
const handleDownload = async (url: string) => {
try {
const response = await axios.get(url, {
responseType: "blob", // دریافت فایل به صورت blob
});
const filename = getFilenameFromUrl(url);
const blob = new Blob([response.data], { type: response.data.type });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(link.href);
} catch (error) {
console.error("Download failed:", error);
}
};
const getFilenameFromUrl = (url: string) => {
return url.split("/").pop() || "downloaded-file";
};
return (
<div className="">
<Drawer direction="right">
<DrawerTrigger onClick={(e) => e.stopPropagation()}>
<div className="text-xs text-gray-500 flex items-center cursor-pointer rounded-md">
<CgMenuRightAlt className="ml-1 text-lg" />
<span>مشاهده روند بررسی</span>
</div>
</DrawerTrigger>
<DrawerContent className=" px-20 w-1/2 h-screen z-[1000] overflow-y-auto scrollbar-thin scrollbar-thumb-blue2 scrollbar-track-white">
<DrawerHeader className="mb-4"></DrawerHeader>
{history.map((history, index, array) => (
<>
<ol
key={history.id}
className="relative border-s border-gray-300 dark:border-gray-700 mr-36"
>
<li className="mb-10 ms-6 w-full">
<span className="absolute flex items-center justify-center w-8 h-8 bg-blue-100 rounded-full -start-4 ring-2 ring-blue bg-blue2 text-white">
{array.length - index}
</span>
<div className=" bg-white border-2 border-gray-300 rounded-lg shadow-sm dark:bg-gray-700 dark:border-gray-600 w-full">
<div className="flex items-center justify-center bg-blue text-white rounded-t-lg p-2 border-2 text-sm">
{history.message}
</div>
<div
className={`p-5 m-1 italic font-normal text-black rounded-lg text-sm text-center ${
history?.attachments?.length > 0 &&
`border-b-[1px] border-blue`
}`}
>
{history?.reason?.title}
</div>
{history?.attachments?.length > 0 && (
<div className="flex flex-col justify-center items-center">
<div className="">
<span>پیوست ها</span>
</div>
<div className="flex flex-row justify-center">
{history?.attachments?.map((attachment, index) => (
<div
className="cursor-pointer"
// onClick={URI.download(a.url)}
onClick={() => handleDownload(attachment.url)}
key={index}
>
{attachment.mediaType == 0 && (
<Image
src={imgimage}
alt="picture"
quality={100}
height={1000}
width={1000}
className="w-20 h-20 object-cover"
/>
)}
{attachment.mediaType == 1 && (
<Image
src={imgvideo}
alt="picture"
quality={100}
height={100}
width={100}
className="w-20 h-20 object-cover"
/>
)}
{attachment.mediaType == 2 && (
<Image
src={imgvoice}
alt="picture"
quality={100}
height={100}
width={100}
className="w-20 h-20 object-cover"
/>
)}
{attachment.mediaType == 3 && (
<Image
src={imgdocument}
alt="picture"
quality={100}
height={100}
width={100}
className="w-20 h-20 object-cover"
/>
)}
</div>
))}
</div>
</div>
)}
</div>
</li>
<div className="flex flex-col justify-center items-center gap-2 absolute -right-36 top-0 ">
<span className="">
{convertTime(history.dateTime)}
</span>
<span className=" ">
{convertDate(history.dateTime)}
</span>
</div>
</ol>
</>
))}
<DrawerFooter className=" absolute top-0 left-0 w-14 h-14">
<DrawerClose asChild>
<Button variant="outline" className="p-0 border-0">
<IoClose className=" w-full h-full text-gray-600" />
</Button>
</DrawerClose>
</DrawerFooter>
</DrawerContent>
</Drawer>
</div>
);
};
export default RequestProcess;