part of Admin Panel done

Kahdooei
yasaman 2024-01-03 16:12:39 +03:30
parent b5c5d2963d
commit a11f743013
9 changed files with 494 additions and 151 deletions

View File

@ -0,0 +1,7 @@
import React from 'react'
export default function page() {
return (
<div>page</div>
)
}

View File

@ -0,0 +1,8 @@
import AdminCategories from '@/components/AdminCategories'
import React from 'react'
export default function page() {
return (
<AdminCategories/>
)
}

View File

@ -0,0 +1,7 @@
import React from 'react'
export default function page() {
return (
<div>page</div>
)
}

View File

@ -0,0 +1,8 @@
import AdminOrganizations from '@/components/AdminOrganizations'
import React from 'react'
export default function page() {
return (
<AdminOrganizations />
)
}

View File

@ -0,0 +1,7 @@
import React from 'react'
export default function AdminCategories() {
return (
<div>AdminCategories</div>
)
}

View File

@ -1,8 +1,11 @@
"use client"; "use client";
import React, { useState } from "react"; import React, { useState, useRef } from "react";
import { useQuery } from "@tanstack/react-query"; import { useMutation, useQuery } from "@tanstack/react-query";
import { GenerateKeyPair, GetKeys } from "@/utils/adminApi"; 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 { import {
AlertDialog, AlertDialog,
@ -28,18 +31,185 @@ import {
FormMessage, FormMessage,
} from "@/components/ui/form"; } from "@/components/ui/form";
import { Input } from "@/components/ui/input"; 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({ const MAX_FILE_SIZE = 5242880; // 5 MB
// title: z.string().min(2, { // Edit Key Form
// message: "trackingNumber must be at least 2 characters.", const FormSchemaEditKey = z.object({
// }), uploadPrivateKey: z.any(),
// password: z.string().min(4, { // .instanceof(File)
// message: "Password must be at least 4 characters.",
// }), // .custom<FileList>((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() { 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<HTMLInputElement | null>(null);;
const inputPrvRef = useRef<HTMLInputElement | null>(null);;
// ..............Read .txt
const [selectedFile, setSelectedFile] = useState<any>(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<z.infer<typeof FormSchemaEditKey>>({
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<typeof FormSchemaEditKey>) {
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<z.infer<typeof FormSchemaAddKey>>({
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<typeof FormSchemaAddKey>) {
AddKeyMutation.mutate({
title: data.title,
publicKey: data.publicKey,
});
}
const { resetField } = useForm();
// List Keys // List Keys
const { const {
data: data1, data: data1,
@ -64,91 +234,59 @@ export default function AdminKeysManagement() {
queryFn: () => GenerateKeyPair(), queryFn: () => GenerateKeyPair(),
enabled: false, // Disable automatic fetching enabled: false, // Disable automatic fetching
}); });
const handleClick = () => {
const handleClickGenerate = () => {
refetch2(); refetch2();
if (inputPubRef.current && inputPrvRef.current) {
inputPubRef.current.value = data2?.publicKey ?? '';
inputPrvRef.current.value = data2?.privateKey ?? '';
}
console.log(data2); console.log(data2);
// Manually trigger the query when the button is clicked
}; };
// } const handleClickAddKey = () => {
// refetch2();
console.log(data1);
};
return ( return (
<div className="w-full bg-[#F5F5F5] min-h-[82vh] flex justify-center"> <div className="w-full bg-[#F5F5F5] min-h-[85vh] flex justify-center">
<div className="w-[95vw] lg:w-[85vw] 3xl:w-[70vw] mt-8 max-sm:mt-16 pt-9 "> <div className="w-[95vw] lg:w-[85vw] 3xl:w-[70vw] mt-8 max-sm:mt-16 pt-9 ">
{/* 1 */} {/* 1 */}
<AlertDialog> <AlertDialog open={openAdd} onOpenChange={setOpenAdd}>
<AlertDialogTrigger asChild> <AlertDialogTrigger asChild>
<Button <Button
variant="outline" variant="outline"
onClick={handleClick} onClick={handleClickAddKey}
disabled={isNewKeyLoading} className="bg-orange-500 text-white"
> // 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> </Button>
</AlertDialogTrigger> </AlertDialogTrigger>
<AlertDialogContent className=" w-full max-w-6xl h-full max-h-[80vh]"> <AlertDialogContent className=" w-full max-w-6xl h-full max-h-[90vh] lg:max-h-[70vh]">
<AlertDialogHeader> <AlertDialogHeader>
<AlertDialogTitle>افزودن کلید جدید</AlertDialogTitle> <AlertDialogTitle>
<p className="text-[#464646] text-base font-bold">
افزودن کلید جدید
</p>
<AlertDialogCancel className="p-0 border-0 h-auto">
<IoMdClose />
</AlertDialogCancel>
</AlertDialogTitle>
<AlertDialogDescription> <AlertDialogDescription>
<div className="w-full ">
<div className="w-full text-right text-neutral-600 text-lg font-bold mb-4">
ایجاد جفت کلید جدید
</div>
{isNewKeyLoading ? ( {isNewKeyLoading ? (
<div className="felx justify-center items-center self-center h-full"> <div className="felx justify-center items-center self-center">
<ThreeDots <ThreeDots
visible={true} visible={true}
height="80" height="50"
width="100" width="100"
color="#003778" color="#003778"
radius="9" radius="9"
@ -158,83 +296,150 @@ export default function AdminKeysManagement() {
/> />
</div> </div>
) : ( ) : (
<></> <div>
// <Form {...form}> <Button
// <form variant="outline"
// onSubmit={form.handleSubmit(onSubmit)} onClick={handleClickGenerate}
// className="w-full flex flex-col mt-6 " disabled={isNewKeyLoading}
// > className="my-2"
// <div className="flex flex-row flex-wrap justify-between"> >
// <FormField {" "}
// control={form.control} ایجاد جفت کلید
// name="trackingNumber" </Button>
// render={({ field }) => ( {isNewKeyLoading ? (
// <FormItem className=" w-full lg:w-[48%]"> <div className="felx justify-center items-center self-center h-full">
// <FormLabel className="text-[#7C838A] text-sm font-medium"> <ThreeDots
// کد پیگیری visible={true}
// </FormLabel> height="20"
// <FormControl> width="50"
// <Input color="#003778"
// placeholder="کد پیگیری خود را وارد نمایید" radius="9"
// {...field} ariaLabel="three-dots-loading"
// /> wrapperStyle={{}}
// </FormControl> wrapperClass=""
// <FormMessage /> />
// </FormItem> </div>
// )} ) : (
// /> <div
className="flex flex-row flex-wrap justify-between"
// <FormField id="KeyFetch"
// control={form.control} >
// name="password" <div className=" w-full lg:w-[48%]">
// render={({ field }) => ( <p className="text-[#7C838A] text-sm font-semibold">
// <FormItem className=" w-full lg:w-[48%]"> کلید عمومی :
// <FormLabel className="text-[#7C838A] text-sm font-medium "> </p>
// رمز عبور {/* <p className="text-slate-700 text-sm text-left mb-4">
// </FormLabel> {data2?.publicKey}
// <FormControl> </p> */}
// <Input <Input
// placeholder="رمز عبور خود را وارد کنید" name="pubKey"
// {...field} ref={inputPubRef}
// /> // value={data2?.publicKey}
// </FormControl> readOnly={true}
// <FormMessage /> className="text-slate-700 text-sm text-left mb-4 w-full bg-white"
// </FormItem> />
// )} </div>
// /> <div className=" w-full lg:w-[48%]">
// </div> <p className="text-[#7C838A] text-sm font-semibold">
کلید خصوصی :
// <div className="flex justify-center sm:justify-end gap-2 w-full mt-8"> </p>
// <Button {/* <p className="text-slate-700 text-sm text-left mb-4">
// // href="/FollowResult" {data2?.privateKey}
// type="submit" </p> */}
// 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" <Input
// > ref={inputPrvRef}
// پیگیری درخواست name="privKey"
// </Button> // value={data2?.privateKey}
// </div> readOnly={true}
// </form> className="text-slate-700 text-sm text-left mb-4 w-full bg-white"
// </Form> />
</div>
</div>
)} )}
</div>
)}
<div className="w-full text-right text-neutral-600 text-lg font-bold mt-4 mb-2">
افزودن کلید جدید
</div>
<Form {...formAdd}>
<form
onSubmit={formAdd.handleSubmit(onSubmitAdd)}
className="w-full flex flex-col mt-6 "
>
<div className="flex flex-row flex-wrap justify-between">
<FormField
control={formAdd.control}
name="title"
render={({ field }) => (
<FormItem className=" w-full lg:w-[48%]">
<FormLabel className="text-[#7C838A] text-sm font-medium">
عنوان کلید
</FormLabel>
<FormControl>
<Input
placeholder="عنوان کلید را وارد نمایید"
{...field}
className="bg-white"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={formAdd.control}
name="publicKey"
render={({ field }) => (
<FormItem className=" w-full lg:w-[48%]">
<FormLabel className="text-[#7C838A] text-sm font-medium ">
کلید عمومی
</FormLabel>
<FormControl>
<Input
placeholder="کلید عمومی را وارد کنید"
{...field}
className="bg-white"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="flex justify-center sm:justify-end gap-2 w-full mt-8">
<Button
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>
</div>
</AlertDialogDescription> </AlertDialogDescription>
</AlertDialogHeader> </AlertDialogHeader>
<AlertDialogFooter> <AlertDialogFooter>
<AlertDialogCancel>بستن</AlertDialogCancel> {/* <AlertDialogCancel>بستن</AlertDialogCancel> */}
{/* <AlertDialogAction>Continue</AlertDialogAction> */} {/* <AlertDialogAction>Continue</AlertDialogAction> */}
</AlertDialogFooter> </AlertDialogFooter>
</AlertDialogContent> </AlertDialogContent>
</AlertDialog> </AlertDialog>
{/* 2 */}
<div className="w-full text-right text-neutral-600 text-lg font-bold my-4"> <div className="w-full text-right text-neutral-600 text-lg font-bold my-4">
لیست کلیدها لیست کلیدها
</div> </div>
{/* 2 */} <div className="w-full flex flex-row flex-wrap justify-between gap-8 md:gap-4 ">
<div className="w-full flex flex-row max-md:flex-wrap justify-between gap-3">
{data1?.map((item: any) => { {data1?.map((item: any) => {
return ( return (
<div <div
key={item.id} 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 className={`flex flex-col justify-center gap-2 w-full lg:w-[49%] h-28 px-5 rounded-md shadow-md items-start
`} `}
> >
<p className=" text-slate-500 text-sm font-normal leading-normal"> <p className=" text-slate-500 text-sm font-normal leading-normal">
@ -247,6 +452,100 @@ export default function AdminKeysManagement() {
{item.inspectorId} {item.inspectorId}
</span> </span>
</p> </p>
<AlertDialog open={openEdit} onOpenChange={setOpenEdit}>
<AlertDialogTrigger asChild>
{/* <Button variant="outline">Show Dialog</Button> */}
<FaRegEdit
aria-hidden="true"
className="w-6 h-6 text-slate-600 hover:cursor-pointer self-end"
onClick={() => setKeyId(item?.id)}
/>
</AlertDialogTrigger>
<AlertDialogContent className=" w-full max-w-3xl h-full lg:max-h-[20em] max-h-[23em]">
<AlertDialogHeader>
<AlertDialogTitle>تغییر کلید</AlertDialogTitle>
<AlertDialogDescription>
<Form {...formEdit}>
<form
onSubmit={formEdit.handleSubmit(onSubmitEdit)}
className="w-full flex flex-col mt-6 "
>
<div className="flex flex-row flex-wrap justify-between">
<FormField
control={formEdit.control}
name="uploadPrivateKey"
render={({ field }) => (
<FormItem className=" w-full">
<FormLabel className="text-[#7C838A] text-sm font-medium mb-2">
پیوست (کلید خصوصی)
</FormLabel>
<FormControl>
<Input
required={true}
type="file"
accept=".txt"
className="bg-slate-300 mt-2"
// {...field}
onChange={handleFileChange}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{/* <FormField
control={formEdit.control}
name="publicKeyId"
render={({ field }) => (
<FormItem className=" w-full lg:w-[48%]">
<FormLabel className="text-[#7C838A] text-sm font-medium ">
آی دی کلید عمومی
</FormLabel>
<FormControl>
<Input
className="bg-slate-300 mt-2"
value={item?.id}
readOnly={true}
// {...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 p-2 rounded-xl text-center hover:bg-white hover:text-slate-800"
>
تغییر کلید
</Button>
{/* <AlertDialogAction
type="submit"
className="bg-blue text-white min-w-max p-2 rounded-xl text-center hover:bg-white hover:text-slate-800"
>
تغییر کلید
</AlertDialogAction> */}
<AlertDialogCancel className="bg-blue text-white rounded-xl mt-0">
بستن
</AlertDialogCancel>
</div>
</form>
</Form>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
{/* <AlertDialogAction>Continue</AlertDialogAction> */}
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div> </div>
); );
})} })}

View File

@ -0,0 +1,7 @@
import React from 'react'
export default function AdminOrganizations() {
return (
<div>AdminOrganizations</div>
)
}

View File

@ -24,7 +24,7 @@ import TableAdmin from "./TableAdmin";
export default function AdminUserManagement() { export default function AdminUserManagement() {
return ( return (
<div className="w-full bg-[#F5F5F5] min-h-[82vh] flex justify-center"> <div className="w-full bg-[#F5F5F5] min-h-[85vh] flex justify-center">
<div className="w-[95vw] lg:w-[85vw] 3xl:w-[70vw] mt-8 max-sm:mt-16 pt-9 "> <div className="w-[95vw] lg:w-[85vw] 3xl:w-[70vw] mt-8 max-sm:mt-16 pt-9 ">
<div className="w-full flex flex-col gap-5 "> <div className="w-full flex flex-col gap-5 ">
{/* 1 */} {/* 1 */}

View File

@ -143,8 +143,8 @@ export const AdminMenuItems = [
{ id: 1, name: "داشبورد", href: "/UserPanel/AdminDashboard" }, { id: 1, name: "داشبورد", href: "/UserPanel/AdminDashboard" },
{ id: 2, name: "مدیریت کاربران", href: "/UserPanel/AdminDashboard/UserManagement" }, { id: 2, name: "مدیریت کاربران", href: "/UserPanel/AdminDashboard/UserManagement" },
{ id: 3, name: "مدیریت کلید ها", href: "/UserPanel/AdminDashboard/KeysManagement" }, { id: 3, name: "مدیریت کلید ها", href: "/UserPanel/AdminDashboard/KeysManagement" },
{ id: 4, name: "گزارشات و آمار", href: "/UserPanel/AdminDashboard/#" }, { id: 4, name: "سازمان ها", href: "/UserPanel/AdminDashboard/Organizations" },
{ id: 5, name: "تنظیمات", href: "/UserPanel/AdminDashboard/#" }, { id: 5, name: "دسته بندی ها", href: "/UserPanel/AdminDashboard/Categories" },
]; ];
export const ControllerTableItems = [ export const ControllerTableItems = [