Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
25 kB
2
Indexable
Never
"use client";
import { Input, Label } from "@/components/common/input";
import { useState, useEffect } from "react";
import axios from "axios";
import toast from "react-hot-toast";
import { useQuery } from "react-query";
import { useRouter } from "next/navigation";
import { usePermissions } from "../../../../components/PermissionsContext";
import LoadingGif from "@/components/common/loaders/LoadingGif";
import handleGetRequest from "helpers/updateStatus";
import { set } from "mongoose";

const getPercent = async () => {
  const responce = await axios.get("/api/promoter/getComission");
  return responce.data;
};

const getResellerCommission = async () => {
  const responce = await axios.get("/api/reseller/commission/getComission");
  return responce.data;
};

const getFreeTokenCommission = async () => {
  const responce = await axios.get("/api/commission/freeTokenCommission");
  return responce.data;
};

export default function PromoterPercentage() {
  const [promoterPercent, setPromoterPercent] = useState<number>();
  const [promoterPercent2, setPromoterPercent2] = useState<number>();
  const [promoterPercent3, setPromoterPercent3] = useState<number>();
  const [promoterFromOne, setPromoterFromOne] = useState<number>();
  const [promoterFromTwo, setPromoterFromTwo] = useState<number>();
  const [promoterFromThree, setPromoterFromThree] = useState<number>();
  const [promoterToOne, setPromoterToOne] = useState<number>();
  const [promoterToTwo, setPromoterToTwo] = useState<number>();
  const [promoterToThree, setPromoterToThree] = useState<number>();

  const [resellerCommission, setResellerCommission] = useState<number>();
  const [resellerCommission2, setResellerCommission2] = useState<number>();
  const [resellerCommission3, setResellerCommission3] = useState<number>();
  const [resellerFromTwo, setResellerFromTwo] = useState<number>();
  const [resellerFromOne, setResellerFromOne] = useState<number>();
  const [resellerFromThree, setResellerFromThree] = useState<number>();
  const [resellerToOne, setResellerToOne] = useState<number>();
  const [resellerToTwo, setResellerToTwo] = useState<number>();
  const [resellerToThree, setResellerToThree] = useState<number>();

  const [freeTokenCommission, setFreeTokenCommission] = useState<number>();
  const [freeTokenCommission2, setFreeTokenCommission2] = useState<number>();
  const [freeTokenCommission3, setFreeTokenCommission3] = useState<number>();
  const [freeTokenFromOne, setFreeTokenFromOne] = useState<number>();
  const [freeTokenFromTwo, setFreeTokenFromTwo] = useState<number>();
  const [freeTokenFromThree, setFreeTokenFromThree] = useState<number>();
  const [freeTokenToOne, setFreeTokenToOne] = useState<number>();
  const [freeTokenToTwo, setFreeTokenToTwo] = useState<number>();
  const [freeTokenToThree, setFreeTokenToThree] = useState<number>();

  const [metamillionsPrice, setMetamillionsPrice] = useState<number>();
  const [metamaskPrice, setMetamaskPrice] = useState<number>();

  const [isDisabled, setIsDisabled] = useState(false);

  const { data, isLoading, error } = useQuery({
    queryKey: ["promoterPercent"],
    queryFn: getPercent,
  });

  const {
    data: datax,
    isLoading: isLoadingx,
    error: errorx,
  } = useQuery({
    queryKey: ["resellerCommission"],
    queryFn: getResellerCommission,
  });

  const {
    data: freeToken,
    isLoading: isLoadingfreeToken,
    error: freeTokenError,
  } = useQuery({
    queryKey: ["freeTokenCommission"],
    queryFn: getFreeTokenCommission,
  });

  const router = useRouter();

  const permissions = usePermissions();
  const newPermission = permissions?.setconfugrationRights;
  const permissionId = permissions?.userId;

  useEffect(() => {
    if (permissions?.setconfugrationRights === "none") {
      alert("You dont have the access to this page");
      router.push("/dashboard");
    }
  }, [permissions]);

  useEffect(() => {
    setPromoterPercent(data?.promoterCommission);
    setPromoterPercent2(data?.promoterCommission2);
    setPromoterPercent3(data?.promoterCommission3);
    setPromoterFromOne(data?.fromOne);
    setPromoterFromTwo(data?.fromTwo);
    setPromoterFromThree(data?.fromThree);
    setPromoterToOne(data?.toOne);
    setPromoterToTwo(data?.toTwo);
    setPromoterToThree(data?.toThree);

    setResellerCommission(datax?.resellerCommission);
    setResellerCommission2(datax?.resellerCommission2);
    setResellerCommission3(datax?.resellerCommission3);
    setResellerFromOne(datax?.fromOne);
    setResellerFromTwo(datax?.fromTwo);
    setResellerFromThree(datax?.fromThree);
    setResellerToOne(datax?.toOne);
    setResellerToTwo(datax?.toTwo);
    setResellerToThree(datax?.toThree);

    setFreeTokenCommission(freeToken?.freeTokenCommission);
    setFreeTokenCommission2(freeToken?.freeTokenCommission2);
    setFreeTokenCommission3(freeToken?.freeTokenCommission3);
    setFreeTokenFromOne(freeToken?.fromOne);
    setFreeTokenFromTwo(freeToken?.fromTwo);
    setFreeTokenFromThree(freeToken?.fromThree);
    setFreeTokenToOne(freeToken?.toOne);
    setFreeTokenToTwo(freeToken?.toTwo);
    setFreeTokenToThree(freeToken?.toThree);

    setMetamillionsPrice(freeToken?.price);
    setMetamaskPrice(freeToken?.priceMetamask);
  }, [data, datax, freeToken]);

  if (isLoading) return <LoadingGif />;
  let toastPostID: string;

  const UpdatePromoterPercent = async (e: React.FormEvent) => {
    e.preventDefault();
    setIsDisabled(true);
    toast.loading("Adding promoter percentage..", { id: toastPostID });
    await axios
      .put("/api/promoter/commission", {
        promoterCommission: promoterPercent,
        promoterCommission2: promoterPercent2,
        promoterCommission3: promoterPercent3,
        fromOne: promoterFromOne,
        fromTwo: promoterFromTwo,
        fromThree: promoterFromThree,
        toOne: promoterToOne,
        toTwo: promoterToTwo,
        toThree: promoterToThree,
      })
      .then((res) => {
        setIsDisabled(false);
        if (res.status === 200) {
          toast.dismiss();
          toast.success("Promoter percentage added 🔥", { id: toastPostID });
          handleGetRequest(
            `Promoter percentage has been added updated`,
            "Success",
            permissionId
          );
        }
      })
      .catch((err) => {
        setIsDisabled(false);
        toast.dismiss();
        toast.error("something went wrong", { id: toastPostID });
        console.log(err, "checking error");
      });
  };

  const UpdateResellerCommission = async (e: React.FormEvent) => {
    e.preventDefault();
    setIsDisabled(true);
    toast.loading("Adding reseller percentage..", { id: toastPostID });
    await axios
      .put("/api/reseller/commission/update", {
        resellerCommission: resellerCommission,
        resellerCommission2: resellerCommission2,
        resellerCommission3: resellerCommission3,
        fromOne: resellerFromOne,
        fromTwo: resellerFromTwo,
        fromThree: resellerFromThree,
        toOne: resellerToOne,
        toTwo: resellerToTwo,
        toThree: resellerToThree,
      })
      .then((res) => {
        setIsDisabled(false);
        if (res.status === 200) {
          toast.dismiss();
          toast.success("Reseller commissin added 🔥", { id: toastPostID });
          handleGetRequest(
            `Reseller commission has been added updated`,
            "Success",
            permissionId
          );
        }
      })
      .catch((err) => {
        setIsDisabled(false);
        toast.dismiss();
        toast.error("something went wrong", { id: toastPostID });
        console.log(err, "checking error");
      });
  };

  const UpdateFreeTokenCommission = async (e: React.FormEvent) => {
    e.preventDefault();
    setIsDisabled(true);
    toast.loading("Adding FreeTokens percentage..", { id: toastPostID });
    await axios
      .put("/api/commission/update", {
        freeTokenCommission: freeTokenCommission,
        freeTokenCommission2: freeTokenCommission2,
        freeTokenCommission3: freeTokenCommission3,
        fromOne: freeTokenFromOne,
        fromTwo: freeTokenFromTwo,
        fromThree: freeTokenFromThree,
        toOne: freeTokenToOne,
        toTwo: freeTokenToTwo,
        toThree: freeTokenToThree,

        price: metamillionsPrice,
        priceMetamask: metamaskPrice,
      })
      .then((res) => {
        setIsDisabled(false);
        if (res.status === 200) {
          toast.dismiss();
          toast.success("Freetokens commissin added 🔥", { id: toastPostID });
          handleGetRequest(
            `Free Tokens commission has been added updated`,
            "Success",
            permissionId
          );
        }
      })
      .catch((err) => {
        setIsDisabled(false);
        toast.dismiss();
        toast.error("something went wrong", { id: toastPostID });
        console.log(err, "checking error");
      });
  };
  return (
    <div className="flex flex-col max-w-[1280px] mx-auto ">
      <div className=" w-full mx-auto py-10 ">
        <h1 className="font-medium text-2xl mb-6">Set Promoter Percentage</h1>
        <form onSubmit={(e) => UpdatePromoterPercent(e)}>
          <div className="my-10 bg-slate-200 p-4 rounded">
            <div className="grid grid-flow-row gap-5 mt-5 ">
              <div>
                <Label>Enter Promoter Percentage</Label>
                <div className=" flex felx-row items-center ">
                  <Input
                    placeholder="%"
                    type="number"
                    className="max-w-[300px]"
                    min="1"
                    max="99"
                    onChange={(e: any) => setPromoterPercent(e.target.value)}
                    required={true}
                    value={promoterPercent}
                  />
                  <Input
                    placeholder="from"
                    type="number"
                    className="max-w-[150px] ml-4"
                    min="0"
                    onChange={(e: any) => setPromoterFromOne(e.target.value)}
                    required={true}
                    value={promoterFromOne}
                  />
                  <p className=" ml-4"> - </p>
                  <Input
                    placeholder="to"
                    type="number"
                    className="max-w-[150px] ml-4"
                    min={Number(promoterFromOne) + 1}
                    max={Number(promoterFromTwo) - 1}
                    onChange={(e: any) => setPromoterToOne(e.target.value)}
                    required={true}
                    value={promoterToOne}
                  />
                  <p className=" ml-4"> MUSD </p>
                </div>
              </div>
              <div className=" flex felx-row items-center ">
                <Input
                  placeholder="%"
                  type="number"
                  className="max-w-[300px]"
                  min="1"
                  max="99"
                  onChange={(e: any) => setPromoterPercent2(e.target.value)}
                  required={true}
                  value={promoterPercent2}
                />
                <Input
                  placeholder="from"
                  type="number"
                  className="max-w-[150px] ml-4"
                  min={Number(promoterToOne) + 1}
                  max={Number(promoterToTwo) - 1}
                  onChange={(e: any) => setPromoterFromTwo(e.target.value)}
                  required={true}
                  value={promoterFromTwo}
                />
                <p className=" ml-4"> - </p>
                <Input
                  placeholder="to"
                  type="number"
                  className="max-w-[150px] ml-4"
                  min={Number(promoterFromTwo) + 1}
                  max={Number(promoterFromThree) - 1}
                  onChange={(e: any) => setPromoterToTwo(e.target.value)}
                  required={true}
                  value={promoterToTwo}
                />
                <p className=" ml-4"> MUSD </p>
              </div>
              <div className=" flex felx-row items-center ">
                <Input
                  placeholder="%"
                  type="number"
                  className="max-w-[300px]"
                  min="1"
                  max="99"
                  onChange={(e: any) => setPromoterPercent3(e.target.value)}
                  required={true}
                  value={promoterPercent3}
                />
                <Input
                  placeholder="from"
                  type="number"
                  className="max-w-[150px] ml-4"
                  min={Number(promoterToTwo) + 1}
                  max={Number(promoterToThree) - 1}
                  onChange={(e: any) => setPromoterFromThree(e.target.value)}
                  required={true}
                  value={promoterFromThree}
                />
                <p className=" ml-4"> - </p>
                <Input
                  placeholder="to"
                  type="number"
                  className="max-w-[150px] ml-4"
                  min={Number(promoterFromThree) + 1}
                  onChange={(e: any) => setPromoterToThree(e.target.value)}
                  required={true}
                  value={promoterToThree}
                />
                <p className=" ml-4"> MUSD </p>
              </div>
            </div>
            {newPermission === "readandwrite" && (
              <button
                className="h-10 text-white block  tracking-widest w-[200px] bg-[#10277c] rounded disabled:opacity-50 mt-8"
                type="submit"
                disabled={isDisabled}
              >
                Update
              </button>
            )}
          </div>
        </form>
      </div>

      <div className=" w-full mx-auto py-10">
        <h1 className="font-medium text-2xl mb-6">Set Reseller Commission</h1>
        <form onSubmit={(e) => UpdateResellerCommission(e)}>
          <div className="my-10 bg-slate-200 p-4 rounded">
            <div className="grid grid-flow-row gap-5 mt-5">
              <div>
                <Label>Enter Reseller Commission</Label>
                <div className=" flex felx-row items-center ">
                  <Input
                    placeholder="%"
                    type="number"
                    className="max-w-[300px]"
                    min="1"
                    max="99"
                    onChange={(e: any) => setResellerCommission(e.target.value)}
                    required={true}
                    value={resellerCommission}
                  />
                  <Input
                    placeholder="from"
                    type="number"
                    className="max-w-[150px] ml-4"
                    min="0"
                    onChange={(e: any) => setResellerFromOne(e.target.value)}
                    required={true}
                    value={resellerFromOne}
                  />
                  <p className=" ml-4"> - </p>
                  <Input
                    placeholder="to"
                    type="number"
                    className="max-w-[150px] ml-4"
                    min={Number(resellerFromOne) + 1}
                    max={Number(resellerFromTwo) - 1}
                    onChange={(e: any) => setResellerToOne(e.target.value)}
                    required={true}
                    value={resellerToOne}
                  />
                  <p className=" ml-4"> MUSD </p>
                </div>
              </div>
              <div className=" flex felx-row items-center ">
                <Input
                  placeholder="%"
                  type="number"
                  className="max-w-[300px]"
                  min="1"
                  max="99"
                  onChange={(e: any) => setResellerCommission2(e.target.value)}
                  required={true}
                  value={resellerCommission2}
                />
                <Input
                  placeholder="from"
                  type="number"
                  className="max-w-[150px] ml-4"
                  min={Number(resellerToOne) + 1}
                  max={Number(resellerToTwo) - 1}
                  onChange={(e: any) => setResellerFromTwo(e.target.value)}
                  required={true}
                  value={resellerFromTwo}
                />
                <p className=" ml-4"> - </p>
                <Input
                  placeholder="to"
                  type="number"
                  className="max-w-[150px] ml-4"
                  min={Number(resellerFromTwo) + 1}
                  max={Number(resellerFromThree) - 1}
                  onChange={(e: any) => setResellerToTwo(e.target.value)}
                  required={true}
                  value={resellerToTwo}
                />
                <p className=" ml-4"> MUSD </p>
              </div>
              <div className=" flex felx-row items-center ">
                <Input
                  placeholder="%"
                  type="number"
                  className="max-w-[300px]"
                  min="1"
                  max="99"
                  onChange={(e: any) => setResellerCommission3(e.target.value)}
                  required={true}
                  value={resellerCommission3}
                />
                <Input
                  placeholder="from"
                  type="number"
                  className="max-w-[150px] ml-4"
                  min={Number(resellerToTwo) + 1}
                  max={Number(resellerToThree) - 1}
                  onChange={(e: any) => setResellerFromThree(e.target.value)}
                  required={true}
                  value={resellerFromThree}
                />
                <p className=" ml-4"> - </p>
                <Input
                  placeholder="to"
                  type="number"
                  className="max-w-[150px] ml-4"
                  min={Number(resellerFromThree) + 1}
                  onChange={(e: any) => setResellerToThree(e.target.value)}
                  required={true}
                  value={resellerToThree}
                />
                <p className=" ml-4"> MUSD </p>
              </div>
            </div>
            {newPermission === "readandwrite" && (
              <button
                className="h-10 text-white block  tracking-widest w-[200px] bg-[#10277c] rounded disabled:opacity-50 mt-8"
                type="submit"
                disabled={isDisabled}
              >
                Update
              </button>
            )}
          </div>
        </form>
      </div>

      <div className=" w-full mx-auto py-10">
        <h1 className="font-medium text-2xl mb-6">
          Set Free Tokens Commission
        </h1>
        <form onSubmit={(e) => UpdateFreeTokenCommission(e)}>
          <div className="my-10 bg-slate-200 p-4 rounded">
            <div className="flex flex-col ">
              <div className="grid w-full grid-flow-row gap-5 mt-5">
                <div>
                  <Label>Enter free Tokens Commission</Label>
                  <div className=" flex felx-row items-center ">
                    <Input
                      placeholder="%"
                      type="number"
                      className="max-w-[300px]"
                      min="1"
                      max="99"
                      onChange={(e: any) =>
                        setFreeTokenCommission(e.target.value)
                      }
                      required={true}
                      value={freeTokenCommission}
                    />
                    <Input
                      placeholder="from"
                      type="number"
                      className="max-w-[150px] ml-4"
                      min="0"
                      onChange={(e: any) => setFreeTokenFromOne(e.target.value)}
                      required={true}
                      value={freeTokenFromOne}
                    />
                    <p className=" ml-4"> - </p>
                    <Input
                      placeholder="to"
                      type="number"
                      className="max-w-[150px] ml-4"
                      min={Number(freeTokenFromOne) + 1}
                      max={Number(freeTokenFromTwo) - 1}
                      onChange={(e: any) => setFreeTokenToOne(e.target.value)}
                      required={true}
                      value={freeTokenToOne}
                    />
                    <p className=" ml-4"> MUSD </p>
                  </div>
                </div>
                <div className=" flex felx-row items-center ">
                  <Input
                    placeholder="%"
                    type="number"
                    className="max-w-[300px]"
                    min="1"
                    max="99"
                    onChange={(e: any) =>
                      setFreeTokenCommission2(e.target.value)
                    }
                    required={true}
                    value={freeTokenCommission2}
                  />
                  <Input
                    placeholder="from"
                    type="number"
                    className="max-w-[150px] ml-4"
                    min={Number(freeTokenToOne) + 1}
                    max={Number(freeTokenToTwo) - 1}
                    onChange={(e: any) => setFreeTokenFromTwo(e.target.value)}
                    required={true}
                    value={freeTokenFromTwo}
                  />
                  <p className=" ml-4"> - </p>
                  <Input
                    placeholder="to"
                    type="number"
                    className="max-w-[150px] ml-4"
                    min={Number(freeTokenFromTwo) + 1}
                    max={Number(freeTokenFromThree) - 1}
                    onChange={(e: any) => setFreeTokenToTwo(e.target.value)}
                    required={true}
                    value={freeTokenToTwo}
                  />
                  <p className=" ml-4"> MUSD </p>
                </div>
                <div className=" flex felx-row items-center ">
                  <Input
                    placeholder="%"
                    type="number"
                    min="1"
                    max="99"
                    className="max-w-[300px]"
                    onChange={(e: any) =>
                      setFreeTokenCommission3(e.target.value)
                    }
                    required={true}
                    value={freeTokenCommission3}
                  />
                  <Input
                    placeholder="from"
                    type="number"
                    className="max-w-[150px] ml-4"
                    min={Number(freeTokenToTwo) + 1}
                    max={Number(freeTokenToThree) - 1}
                    onChange={(e: any) => setFreeTokenFromThree(e.target.value)}
                    required={true}
                    value={freeTokenFromThree}
                  />
                  <p className=" ml-4"> - </p>
                  <Input
                    placeholder="to"
                    type="number"
                    className="max-w-[150px] ml-4"
                    min={Number(freeTokenFromThree) + 1}
                    onChange={(e: any) => setFreeTokenToThree(e.target.value)}
                    required={true}
                    value={freeTokenToThree}
                  />
                  <p className=" ml-4"> MUSD </p>
                </div>
              </div>
              <div className="grid w-full grid-flow-row gap-5 mt-5">
                <div>
                  <Label>Enter free Tokens Price Equavlent to 1 MUSD</Label>
                  <div className=" flex felx-row mb-5 items-center ">
                    <Input
                      placeholder="%"
                      type="number"
                      className="max-w-[300px]"
                      min="1"
                      max="99"
                      onChange={(e: any) =>
                        setMetamillionsPrice(e.target.value)
                      }
                      required={true}
                      value={metamillionsPrice}
                    />
                    <p className=" ml-4">Metamillions Wallet</p>
                  </div>

                  <div className=" flex felx-row items-center ">
                    <Input
                      placeholder="%"
                      type="number"
                      className="max-w-[300px]"
                      min="1"
                      max="99"
                      onChange={(e: any) => setMetamaskPrice(e.target.value)}
                      required={true}
                      value={metamaskPrice}
                    />
                    <p className=" ml-4">MetaMask Wallet</p>
                  </div>
                </div>
              </div>
            </div>
            {newPermission === "readandwrite" && (
              <button
                className="h-10 text-white block  tracking-widest w-[200px] bg-[#10277c] rounded disabled:opacity-50 mt-8"
                type="submit"
                disabled={isDisabled}
              >
                Update
              </button>
            )}
          </div>
        </form>
      </div>
    </div>
  );
}
Leave a Comment