Sootzani-Inspector/components/AdminKeysManagement.tsx

258 lines
9.0 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, { useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { GenerateKeyPair, GetKeys } from "@/utils/adminApi";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { ThreeDots } from "react-loader-spinner";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
// const FormSchema = z.object({
// title: z.string().min(2, {
// message: "trackingNumber must be at least 2 characters.",
// }),
// password: z.string().min(4, {
// message: "Password must be at least 4 characters.",
// }),
// });
export default function AdminKeysManagement() {
// List Keys
const {
data: data1,
error: Error1,
isLoading: isListLoading,
refetch: refetch1,
} = useQuery({
queryKey: ["KeysList"],
queryFn: () => GetKeys(),
// refetchOnWindowFocus: false,
});
console.log(data1);
// GenerateKeyPair
const {
data: data2,
error: Error2,
isLoading: isNewKeyLoading,
refetch: refetch2,
} = useQuery({
queryKey: ["GenerateKey"],
queryFn: () => GenerateKeyPair(),
enabled: false, // Disable automatic fetching
});
const handleClick = () => {
refetch2();
console.log(data2);
// Manually trigger the query when the button is clicked
};
// }
return (
<div className="w-full bg-[#F5F5F5] min-h-[82vh] flex justify-center">
<div className="w-[95vw] lg:w-[85vw] 3xl:w-[70vw] mt-8 max-sm:mt-16 pt-9 ">
{/* 1 */}
<AlertDialog>
<AlertDialogTrigger asChild>
<Button
variant="outline"
onClick={handleClick}
disabled={isNewKeyLoading}
>
{" "}
ایجاد جفت کلید
</Button>
</AlertDialogTrigger>
<AlertDialogContent className=" w-full max-w-6xl h-full max-h-[80vh]">
<AlertDialogHeader>
<AlertDialogTitle>اطلاعات کلید جدید</AlertDialogTitle>
<AlertDialogDescription>
{isNewKeyLoading ? (
<div className="felx justify-center items-center self-center h-full">
<ThreeDots
visible={true}
height="80"
width="100"
color="#003778"
radius="9"
ariaLabel="three-dots-loading"
wrapperStyle={{}}
wrapperClass=""
/>
</div>
) : (
<div className=" " id="KeyFetch">
<p className="text-black text-sm font-semibold">
کلید عمومی :
</p>
<p className="text-slate-700 text-sm text-left mb-4">
{data2?.publicKey}
</p>
<p className="text-black text-sm font-semibold">
کلید خصوصی :
</p>
<p className="text-slate-700 text-sm text-left mb-4">
{data2?.privateKey}
</p>
</div>
)}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>بستن</AlertDialogCancel>
{/* <AlertDialogAction>Continue</AlertDialogAction> */}
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
{/* 1 */}
<AlertDialog>
<AlertDialogTrigger asChild>
<Button
variant="outline"
onClick={handleClick}
disabled={isNewKeyLoading}
>
افزودن کلید جدید
</Button>
</AlertDialogTrigger>
<AlertDialogContent className=" w-full max-w-6xl h-full max-h-[80vh]">
<AlertDialogHeader>
<AlertDialogTitle>افزودن کلید جدید</AlertDialogTitle>
<AlertDialogDescription>
{isNewKeyLoading ? (
<div className="felx justify-center items-center self-center h-full">
<ThreeDots
visible={true}
height="80"
width="100"
color="#003778"
radius="9"
ariaLabel="three-dots-loading"
wrapperStyle={{}}
wrapperClass=""
/>
</div>
) : (
<></>
// <Form {...form}>
// <form
// onSubmit={form.handleSubmit(onSubmit)}
// className="w-full flex flex-col mt-6 "
// >
// <div className="flex flex-row flex-wrap justify-between">
// <FormField
// control={form.control}
// name="trackingNumber"
// render={({ field }) => (
// <FormItem className=" w-full lg:w-[48%]">
// <FormLabel className="text-[#7C838A] text-sm font-medium">
// کد پیگیری
// </FormLabel>
// <FormControl>
// <Input
// placeholder="کد پیگیری خود را وارد نمایید"
// {...field}
// />
// </FormControl>
// <FormMessage />
// </FormItem>
// )}
// />
// <FormField
// control={form.control}
// name="password"
// render={({ field }) => (
// <FormItem className=" w-full lg:w-[48%]">
// <FormLabel className="text-[#7C838A] text-sm font-medium ">
// رمز عبور
// </FormLabel>
// <FormControl>
// <Input
// placeholder="رمز عبور خود را وارد کنید"
// {...field}
// />
// </FormControl>
// <FormMessage />
// </FormItem>
// )}
// />
// </div>
// <div className="flex justify-center sm:justify-end gap-2 w-full mt-8">
// <Button
// // href="/FollowResult"
// type="submit"
// className="bg-blue text-white min-w-max w-1/2 sm:w-1/3 lg:w-1/4 p-2 rounded-xl text-center"
// >
// پیگیری درخواست
// </Button>
// </div>
// </form>
// </Form>
)}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>بستن</AlertDialogCancel>
{/* <AlertDialogAction>Continue</AlertDialogAction> */}
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<div className="w-full text-right text-neutral-600 text-lg font-bold my-4">
لیست کلیدها
</div>
{/* 2 */}
<div className="w-full flex flex-row max-md:flex-wrap justify-between gap-3">
{data1?.map((item: any) => {
return (
<div
key={item.id}
className={`flex flex-col justify-center gap-2 w-full lg:w-1/2 h-28 px-5 bg-white rounded-md shadow-md items-start
`}
>
<p className=" text-slate-500 text-sm font-normal leading-normal">
عنوان کلید :
<span className=" text-slate-800">{item.title}</span>
</p>
<p className=" text-slate-500 text-sm font-normal leading-normal">
آی دی بازرس :
<span className=" text-slate-800 self-end">
{item.inspectorId}
</span>
</p>
</div>
);
})}
</div>
</div>
</div>
);
}