Sootzani-Inspector/components/TableController.tsx

207 lines
6.7 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.

import React, { useState } from "react";
import {
Table,
TableBody,
TableCaption,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { ControllerTableItems } from "@/constants";
import Link from "next/link";
import { Decrypt, convertDate } from "@/lib/utils";
import useData from "@/store/useData";
import { Label } from "./ui/label";
import { Input } from "./ui/input";
import { useRouter } from "next/navigation";
import { toast } from "sonner";
import { ScrollArea } from "./ui/scroll-area";
import secureLocalStorage from "react-secure-storage";
import { Checkbox } from "./ui/checkbox";
import { Skeleton } from "./ui/skeleton";
interface Complaint {
id: string;
trackingNumber: string;
title: string;
category: {
id: string;
title: string;
description: string;
};
complaintOrganization: {
id: string;
title: string;
description: string;
};
status: number;
registeredAt: string;
lastChanged: string;
lastActor: number;
cipherKeyInspector: string;
}
export default function TableController({
data,
checkKey,
}: {
data: Complaint[];
checkKey: (item: any) => any;
}) {
const saveData = useData((state) => state.saveData);
const [selectedFile, setSelectedFile] = useState<any>(null);
const [open, setOpen] = useState<boolean>(false);
const [rowData, setRowData] = useState<Complaint | null>(null);
const [check, setCheck] = useState<any>(false);
const router = useRouter();
const handleFileChange = (event: any) => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = (e) => {
const contents = e?.target?.result;
setSelectedFile(contents);
};
reader.readAsText(file);
};
const checkKey2 = (item: any) => {
const dcKey = Decrypt(item.cipherKeyInspector, selectedFile);
if (dcKey != false && dcKey != null) {
saveData({
trackingNumber: item.trackingNumber,
password: Decrypt(item.cipherKeyInspector, selectedFile),
});
secureLocalStorage.setItem("trackingNumber", item.trackingNumber);
secureLocalStorage.setItem("privateKey", selectedFile);
// secureLocalStorage.setItem(
// "privateKey",
// Decrypt(item.cipherKeyInspector, selectedFile)
// );
router.push(`/UserPanel/ControllerDashboard/Complaints/${item.id}`);
} else {
// toast("******کلید شما معتبر نمی باشد.", {
// className: "!bg-red-500 !text-white",
// });
}
};
console.log(check);
return (
<div>
<Table className="border-[1px] border-solid border-muted rounded-md w-full min-w-[40rem] overflow-y-auto scrollbar-thumb-gray-600 scrollbar-track-gray-100 scrollbar-thin">
{/* <TableCaption>A list of your recent invoices.</TableCaption> */}
<TableHeader>
<TableRow>
<TableHead>ردیف</TableHead>
<TableHead>کد پیگیری</TableHead>
<TableHead> تاریخ ثبت</TableHead>
<TableHead>تاریخ</TableHead>
<TableHead>موضوع</TableHead>
<TableHead>دستگاه مربوطه</TableHead>
</TableRow>
</TableHeader>
<TableBody className="bg-[#F5F5F5] min-h-[20rem] ">
{data?.map((item: Complaint, index: number) => {
return (
<TableRow
onClick={() => {
localStorage.setItem("CompliantData", JSON.stringify(item));
router.push(
`/UserPanel/ControllerDashboard/Complaints/${item.id}`
);
}}
className="cursor-pointer hover:bg-gray-200"
key={item.id}
>
{/* <Link
href={`/UserPanel/ControllerDashboard/Complaints/${item.id}`}
className="contents"
> */}
<TableCell className="w-1/12 font-medium ">
{index + 1}
</TableCell>
<TableCell className="w-1/12">{item.trackingNumber}</TableCell>
<TableCell className="w-2/12">
{convertDate(item.registeredAt)}
</TableCell>
<TableCell className="w-2/12">
{convertDate(item.lastChanged)}
</TableCell>
<TableCell className="w-3/12">
{item?.title ? item.title : "-"}
</TableCell>
<TableCell className="w-3/12">
{item?.complaintOrganization
? item?.complaintOrganization.title
: "-"}
</TableCell>
{/* </Link> */}
</TableRow>
);
})}
{data.length != 0 &&
[...Array(5 - data.length)].map(() => {
return (
<TableRow
key={Math.random()}
className="border border-t-0 border-b-0"
/>
);
})}
</TableBody>
</Table>
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent dir="rtl" className="sm:max-w-[425px]">
<DialogHeader dir="rtl">
<DialogTitle dir="rtl">بارگذاری کلید خصوصی</DialogTitle>
<DialogDescription>
لطفا کلید خصوصی خود را آپلود نمایید.
</DialogDescription>
</DialogHeader>
<div className="grid w-full max-w-sm items-center gap-1.5">
{/* <Label htmlFor="picture">Picture</Label> */}
<Input
dir="rtl"
id="picture"
onChange={handleFileChange}
type="file"
accept=".txt"
/>
</div>
<div className="flex items-center space-x-2">
<Checkbox
checked={check}
onCheckedChange={setCheck}
id="terms"
className="ml-2 "
/>
<label
htmlFor="terms"
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
ذخیره کلید برای دفعات بعد
</label>
</div>
<DialogFooter>
<DialogClose asChild></DialogClose>
<Button onClick={() => checkKey2(rowData)} type="submit">
ذخیره
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
);
}