diff --git a/app/UserPanel/(Admin)/AdminDashboard/Categories/[id]/page.tsx b/app/UserPanel/(Admin)/AdminDashboard/Categories/[id]/page.tsx new file mode 100644 index 0000000..cb2be10 --- /dev/null +++ b/app/UserPanel/(Admin)/AdminDashboard/Categories/[id]/page.tsx @@ -0,0 +1,7 @@ +import React from 'react' + +export default function page() { + return ( +
page
+ ) +} diff --git a/app/UserPanel/(Admin)/AdminDashboard/Categories/page.tsx b/app/UserPanel/(Admin)/AdminDashboard/Categories/page.tsx new file mode 100644 index 0000000..3f6ad8d --- /dev/null +++ b/app/UserPanel/(Admin)/AdminDashboard/Categories/page.tsx @@ -0,0 +1,8 @@ +import AdminCategories from '@/components/AdminCategories' +import React from 'react' + +export default function page() { + return ( + + ) +} diff --git a/app/UserPanel/(Admin)/AdminDashboard/Organizations/[id]/page.tsx b/app/UserPanel/(Admin)/AdminDashboard/Organizations/[id]/page.tsx new file mode 100644 index 0000000..cb2be10 --- /dev/null +++ b/app/UserPanel/(Admin)/AdminDashboard/Organizations/[id]/page.tsx @@ -0,0 +1,7 @@ +import React from 'react' + +export default function page() { + return ( +
page
+ ) +} diff --git a/app/UserPanel/(Admin)/AdminDashboard/Organizations/page.tsx b/app/UserPanel/(Admin)/AdminDashboard/Organizations/page.tsx new file mode 100644 index 0000000..eb1d5b9 --- /dev/null +++ b/app/UserPanel/(Admin)/AdminDashboard/Organizations/page.tsx @@ -0,0 +1,8 @@ +import AdminOrganizations from '@/components/AdminOrganizations' +import React from 'react' + +export default function page() { + return ( + + ) +} diff --git a/components/AdminCategories.tsx b/components/AdminCategories.tsx new file mode 100644 index 0000000..b3b59ea --- /dev/null +++ b/components/AdminCategories.tsx @@ -0,0 +1,7 @@ +import React from 'react' + +export default function AdminCategories() { + return ( +
AdminCategories
+ ) +} diff --git a/components/AdminKeysManagement.tsx b/components/AdminKeysManagement.tsx index 972cd7a..a3a6b0c 100644 --- a/components/AdminKeysManagement.tsx +++ b/components/AdminKeysManagement.tsx @@ -1,8 +1,11 @@ "use client"; -import React, { useState } from "react"; -import { useQuery } from "@tanstack/react-query"; -import { GenerateKeyPair, GetKeys } from "@/utils/adminApi"; +import React, { useState, useRef } from "react"; +import { useMutation, useQuery } 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, @@ -28,18 +31,185 @@ import { 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 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.", -// }), -// }); +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, @@ -64,177 +234,212 @@ export default function AdminKeysManagement() { queryFn: () => GenerateKeyPair(), enabled: false, // Disable automatic fetching }); - const handleClick = () => { + + const handleClickGenerate = () => { refetch2(); + + if (inputPubRef.current && inputPrvRef.current) { + inputPubRef.current.value = data2?.publicKey ?? ''; + inputPrvRef.current.value = data2?.privateKey ?? ''; + } console.log(data2); - // Manually trigger the query when the button is clicked }; - // } + const handleClickAddKey = () => { + // refetch2(); + console.log(data1); + }; + return ( -
+
{/* 1 */} - + - - - - اطلاعات کلید جدید - - {isNewKeyLoading ? ( -
- -
- ) : ( -
-

- کلید عمومی : -

-

- {data2?.publicKey} -

- -

- کلید خصوصی : -

-

- {data2?.privateKey} -

-
- )} -
-
- - بستن - {/* Continue */} - -
-
- - {/* 1 */} - - - - + - افزودن کلید جدید + +

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

+ + + +
+ - {isNewKeyLoading ? ( -
- +
+
+ ایجاد جفت کلید جدید
- ) : ( - <> - //
- // - //
- // ( - // - // - // کد پیگیری - // - // - // - // - // - // - // )} - // /> - // ( - // - // - // رمز عبور - // - // - // - // - // - // - // )} - // /> - //
+ {isNewKeyLoading ? ( +
+ +
+ ) : ( +
+ + {isNewKeyLoading ? ( +
+ +
+ ) : ( +
+
+

+ کلید عمومی : +

+ {/*

+ {data2?.publicKey} +

*/} + +
+
+

+ کلید خصوصی : +

+ {/*

+ {data2?.privateKey} +

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

@@ -247,6 +452,100 @@ export default function AdminKeysManagement() { {item.inspectorId}

+ + + + {/* */} + + + + تغییر کلید + +
+ +
+ ( + + + پیوست (کلید خصوصی) + + + + + + + )} + /> + + {/* ( + + + آی دی کلید عمومی + + + + + + + )} + /> */} +
+ +
+ + + {/* + تغییر کلید + */} + + + بستن + +
+
+ +
+
+ + {/* Continue */} + +
+
); })} diff --git a/components/AdminOrganizations.tsx b/components/AdminOrganizations.tsx new file mode 100644 index 0000000..bf351e8 --- /dev/null +++ b/components/AdminOrganizations.tsx @@ -0,0 +1,7 @@ +import React from 'react' + +export default function AdminOrganizations() { + return ( +
AdminOrganizations
+ ) +} diff --git a/components/AdminUserManagement.tsx b/components/AdminUserManagement.tsx index 827cd7d..c31f000 100644 --- a/components/AdminUserManagement.tsx +++ b/components/AdminUserManagement.tsx @@ -24,7 +24,7 @@ import TableAdmin from "./TableAdmin"; export default function AdminUserManagement() { return ( -
+
{/* 1 */} diff --git a/constants/index.tsx b/constants/index.tsx index aab85b3..8c9f7b9 100644 --- a/constants/index.tsx +++ b/constants/index.tsx @@ -143,8 +143,8 @@ export const AdminMenuItems = [ { id: 1, name: "داشبورد", href: "/UserPanel/AdminDashboard" }, { id: 2, name: "مدیریت کاربران", href: "/UserPanel/AdminDashboard/UserManagement" }, { id: 3, name: "مدیریت کلید ها", href: "/UserPanel/AdminDashboard/KeysManagement" }, - { id: 4, name: "گزارشات و آمار", href: "/UserPanel/AdminDashboard/#" }, - { id: 5, name: "تنظیمات", href: "/UserPanel/AdminDashboard/#" }, + { id: 4, name: "سازمان ها", href: "/UserPanel/AdminDashboard/Organizations" }, + { id: 5, name: "دسته بندی ها", href: "/UserPanel/AdminDashboard/Categories" }, ]; export const ControllerTableItems = [