"use client"; import React, { useState, useRef, useEffect } from "react"; import { useMutation, useQuery, RefetchOptions } from "@tanstack/react-query"; import { GenerateKeyPair, GetKeys, AddKey, ChangeKey } from "@/utils/adminApi"; import { FaRegEdit } from "react-icons/fa"; import { IoMdClose } from "react-icons/io"; import { useRouter } from "next/navigation"; 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"; import { toast } from "sonner"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import * as z from "zod"; const MAX_FILE_SIZE = 5242880; // 5 MB // Edit Key Form const FormSchemaEditKey = z.object({ uploadPrivateKey: z.any(), // .instanceof(File) // .custom((val) => val instanceof FileList, "Required") // .refine((files) => files.length <= 1, "Maximum of 1 file is allowed.") // .refine( // (files) => // Array.from(files).every((file) => file.size <= MAX_IMAGE_SIZE), // "Each file size should be less than 5 MB." // ), // .refine((files) => files?.length === 0, "File is required.") // .refine((files) => files?.length >= 1, "Maximum of 1 file is allowed.") // .refine((files) => files?.[0]?.size <= MAX_FILE_SIZE, `Max file size is 5MB.`) // publicKeyId: z // .string() // .nullable(), }); // Add Key Form const FormSchemaAddKey = z.object({ title: z.string().min(2, { message: "عنوان کلید را وارد کنید", }), publicKey: z.string().min(4, { message: "کلید عمومی را وارد کنید", }), }); export default function AdminKeysManagement() { const router = useRouter(); const [keyId, setKeyId] = useState(""); const [openAdd, setOpenAdd] = React.useState(false); const [openEdit, setOpenEdit] = React.useState(false); const inputPubRef = useRef(null); const inputPrvRef = useRef(null); // ..............Read .txt const [selectedFile, setSelectedFile] = useState(null); const handleFileChange = (event: any) => { const file = event.target.files[0]; const reader = new FileReader(); reader.onload = (e) => { const contents = e?.target?.result; // Process the contents of the file here setSelectedFile(contents); console.log(contents); }; reader?.readAsText(file); console.log(reader); console.log(selectedFile); }; // Edit Form const formEdit = useForm>({ resolver: zodResolver(FormSchemaEditKey), defaultValues: { // publicKeyId: "", // uploadFile: null, }, }); // ............'Mutation' Change Key Submit const ChangeKeyMutation = useMutation({ mutationKey: ["ChangeKey"], mutationFn: ChangeKey, onSuccess: (res) => { console.log(res); // alert("Success"); console.log("Success ChangeKey"); setOpenEdit(false); toast("عملیات شما با موفقیت انجام شد. ", { className: "!bg-green-500 !text-white", }); // router.refresh() }, onError: (error) => { console.log(error); console.log(error?.message); setOpenEdit(false); toast(error?.message, { className: "!bg-red-500 !text-black", }); }, }); function onSubmitEdit(data: z.infer) { console.log("bcbcbbcbcbc" + data); console.log("bcbcbbcbcbccc" + selectedFile); console.log("bcbcbbcbcbccccc" + data1[0]?.id); // if (keyId) { ChangeKeyMutation.mutate({ privateKey: selectedFile, publicKeyId: keyId, // publicKeyId: data1?.id, }); // } // else{ // console.error("keyId is undefined"); // } } // Add Form const formAdd = useForm>({ resolver: zodResolver(FormSchemaAddKey), defaultValues: { title: "", publicKey: "", }, }); // ............'Mutation' Add Key Submit const AddKeyMutation = useMutation({ mutationKey: ["AddKey"], mutationFn: AddKey, onSuccess: (res) => { console.log(res); console.log("Success AddKey"); setOpenAdd(false); refetch1(); formAdd.reset(); if (inputPubRef.current && inputPrvRef.current) { inputPrvRef.current.value = ""; inputPubRef.current.value = ""; } toast("عملیات شما با موفقیت انجام شد. ", { className: "!bg-green-500 !text-white", }); // inputPubRef.current.clear(); // router.refresh() }, onError: (error) => { console.log(error); console.log(error?.message); setOpenAdd(false); toast(error?.message, { className: "!bg-red-500 !text-black", }); }, }); function onSubmitAdd(data: z.infer) { AddKeyMutation.mutate({ title: data.title, publicKey: data.publicKey, }); } // const { resetField } = useForm(); // 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 }); // Define the type for refetch2 // type RefetchFunction = (options?: RefetchOptions) => Promise; const handleClickGenerate = () => { refetch2(); console.log(data2); }; useEffect(() => { if (inputPubRef.current && inputPrvRef.current) { inputPubRef.current.value = data2?.publicKey ?? ""; inputPrvRef.current.value = data2?.privateKey ?? ""; } }, [handleClickGenerate]); const handleClickAddKey = () => { // refetch2(); console.log(data1); }; return (
{/* 1 */}

افزودن کلید جدید

ایجاد جفت کلید جدید
{isNewKeyLoading ? (
) : (
{isNewKeyLoading ? (
) : (

کلید عمومی :

{/*

{data2?.publicKey}

*/}

کلید خصوصی :

{/*

{data2?.privateKey}

*/}
)}
)}
افزودن کلید جدید
( عنوان کلید )} /> ( کلید عمومی )} />
{/* بستن */} {/* Continue */}
{/* 2 */}
لیست کلیدها
{data1?.map((item: any) => { return (

عنوان کلید : {item.title}

آی دی بازرس : {item.inspectorId}

{/* */} تغییر کلید
( پیوست (کلید خصوصی) )} /> {/* ( آی دی کلید عمومی )} /> */}
{/* تغییر کلید */} بستن
{/* Continue */}
); })}
); }