From 6a9ea9e90b8f764b5ae1ffcd5d093ba7ae6c5be4 Mon Sep 17 00:00:00 2001 From: yasaman Date: Thu, 4 Jan 2024 21:31:40 +0330 Subject: [PATCH] Category , Organizations in Admin Panel don --- components/AdminCategories.tsx | 473 +++++++++++++++++++++++++++- components/AdminDashboard.tsx | 8 +- components/AdminKeysManagement.tsx | 2 +- components/AdminOrganizations.tsx | 474 ++++++++++++++++++++++++++++- components/ui/switch.tsx | 29 ++ components/ui/toggle.tsx | 45 +++ package-lock.json | 56 ++++ package.json | 2 + utils/adminApi.ts | 28 +- 9 files changed, 1102 insertions(+), 15 deletions(-) create mode 100644 components/ui/switch.tsx create mode 100644 components/ui/toggle.tsx diff --git a/components/AdminCategories.tsx b/components/AdminCategories.tsx index b3b59ea..367e2fc 100644 --- a/components/AdminCategories.tsx +++ b/components/AdminCategories.tsx @@ -1,7 +1,474 @@ -import React from 'react' +"use client"; + +import React, { useState, useRef, useEffect } from "react"; +import { useMutation, useQuery, RefetchOptions } from "@tanstack/react-query"; +import { + GetAllCategories, + GetSingleCategory, + SubmitCategory, + UpdateCategory, + DeleteCategory, +} from "@/utils/adminApi"; +import { FaRegEdit } from "react-icons/fa"; +import { IoMdClose } from "react-icons/io"; +import { RiDeleteBin6Line } from "react-icons/ri"; +import { useRouter } from "next/navigation"; +import { ThreeDots } from "react-loader-spinner"; +import { Button } from "@/components/ui/button"; +import { toast } from "sonner"; + +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogTrigger, +} from "@/components/ui/alert-dialog"; + +import { + Form, + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; + +import { zodResolver } from "@hookform/resolvers/zod"; +import { useForm } from "react-hook-form"; +import * as z from "zod"; +import { Textarea } from "./ui/textarea"; +import { Switch } from "@/components/ui/switch"; + +// Form Validation +const EditCatFormSchema = z.object({ + titleCat: z + .string() + .min(2, { + message: "عنوان باید حداقل 2 کاراکتر باشد.", + }) + .max(30, { + message: "عنوان نباید بیشتر از 30 کاراکتر باشد.", + }), + description: z.string().max(160), +}); + +// Add Key Form +const AddCatFormSchema = z.object({ + titleCategory: z.string().min(2, { + message: "عنوان را وارد کنید", + }), + description: z.string(), +}); export default function AdminCategories() { + const router = useRouter(); + + const [keyId, setKeyId] = useState(""); + const [keyIdDel, setKeyIdDel] = useState(""); + const [titleCat, setTitleCat] = useState(""); + const [descCat, setDescCat] = useState(""); + + const [openAdd, setOpenAdd] = React.useState(false); + const [openEdit, setOpenEdit] = React.useState(false); + + const [delCat, setDelCat] = React.useState(false); + + // List Categories + const { + data: data1, + error: Error1, + isLoading: isListLoading, + refetch: refetch1, + } = useQuery({ + queryKey: ["CategoriesList"], + queryFn: () => GetAllCategories(), + // refetchOnWindowFocus: false, + }); + console.log(data1); + + // Edit + const handleClickEdit = (id: string, title: string, description: string) => { + // Set state variables + setKeyId(id); + setTitleCat(title); + setDescCat(description); + + // Reset the form with new default values + formEdit.reset({ + titleCat: title, + description: description, + }); + }; + + // Form Edit + type EditCatFormValues = z.infer; + // This can come from your database or API. + const defaultValuesEdit: Partial = { + titleCat: "", + description: "", + }; + + const formEdit = useForm({ + resolver: zodResolver(EditCatFormSchema), + defaultValues: defaultValuesEdit, + mode: "onChange", + }); + + // Form Add + type AddCatFormValues = z.infer; + // This can come from your database or API. + const defaultValuesAdd: Partial = { + titleCategory: "", + description: "", + }; + + const formAdd = useForm({ + resolver: zodResolver(AddCatFormSchema), + defaultValues: defaultValuesAdd, + mode: "onChange", + }); + // Edit Submit + + // ............'Mutation' Change Key Submit + const ChangeCatMutation = useMutation({ + mutationKey: ["ChangeCat"], + mutationFn: UpdateCategory, + onSuccess: (res) => { + console.log(res); + console.log("Success ChangeCat"); + setOpenEdit(false); + + refetch1(); + + toast("عملیات شما با موفقیت انجام شد. ", { + className: "!bg-green-500 !text-white", + }); + }, + onError: (error) => { + console.log(error); + console.log(error?.message); + + setOpenEdit(false); + + toast(error?.message, { + className: "!bg-red-500 !text-black", + }); + }, + }); + + //Delete + const DeleteCatMutation = useMutation({ + mutationKey: ["DeleteCat"], + mutationFn: DeleteCategory, + onSuccess: (res) => { + console.log(res); + console.log("Success ChangeCat"); + setOpenEdit(false); + refetch1(); + toast("عملیات شما با موفقیت انجام شد. ", { + className: "!bg-green-500 !text-white", + }); + }, + onError: (error) => { + console.log(error); + console.log(error?.message); + + toast(error?.message, { + className: "!bg-red-500 !text-black", + }); + }, + }); + + // Add + // ............'Mutation' Change Key Submit + const AddCatMutation = useMutation({ + mutationKey: ["AddCat"], + mutationFn: SubmitCategory, + onSuccess: (res) => { + console.log(res); + console.log("Success ChangeCat"); + setOpenAdd(false); + + refetch1(); + + toast("عملیات شما با موفقیت انجام شد. ", { + className: "!bg-green-500 !text-white", + }); + }, + onError: (error) => { + console.log(error); + console.log(error?.message); + + setOpenAdd(false); + + toast(error?.message, { + className: "!bg-red-500 !text-black", + }); + }, + }); + + function onSubmitEdit(data: EditCatFormValues) { + ChangeCatMutation.mutate({ + id: keyId, + payload: { + title: data.titleCat, + description: data.description, + }, + }); + console.log(data); + } + function onSubmitAdd(data: AddCatFormValues) { + AddCatMutation.mutate({ + title: data.titleCategory, + description: data.description, + }); + console.log(data); + } return ( -
AdminCategories
- ) +
+
+
+ {/* 1 */} +
+ لیست دسته بندی ها +
+ {/* 2 */} + + {/* 1 */} + + + + + + + +

+ افزودن دسته بندی +

+ + + +
+ + +
+
+ افزودن دسته بندی +
+ +
+ +
+ ( + + + عنوان دسته بندی + + + + + + + )} + /> + + ( + + + توضیحات + + + + + + + )} + /> +
+ +
+ + + بستن + +
+
+ +
+
+
+ + {/* بستن */} + +
+
+
+
+ + + + +

+ تغییر کلید +

+ + + +
+ + +
+ +
+ ( + + عنوان + + + + + + )} + /> + + ( + + توضیحات + +