import React, { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import axios from "axios"; import toast, { Toaster } from "react-hot-toast"; import { Button, Box, TextField, Typography } from "@mui/material"; import { isAutheticated } from "src/auth"; const CreateCoupon = () => { const token = isAutheticated(); const navigate = useNavigate(); const [loading, setLoading] = useState(false); //only for testing const [itemPerPage, setItemPerPage] = useState(10); //pagination const [page, setPage] = useState(1); //pagination //Form States const [open, setOpen] = useState(false); const [coupon, setCoupon] = useState(""); const [discount, setDiscount] = useState(""); const [affiliateDiscountAmt, setAffiliateDiscountAmt] = useState(""); const [valid, setValid] = useState(""); const [affiliate, setAffiliate] = useState(""); //Form error states const [couponError, setCouponError] = useState(false); const [discountError, setDiscountError] = useState(false); const [validError, setValidError] = useState(false); const [affiliateError, setAffiliateError] = useState(false); const [affiliateDiscountAmtError, setAffiliateDiscountAmtError] = useState(false); const [apiData, setApiData] = useState([]); //Discount limit const handelDiscount = (event) => { let value = event.target.value; if (parseInt(value) >= 9999) { setDiscount(9999); setDiscountError(true); } else if (parseInt(value) < 0) { setDiscount(0); } else { setDiscountError(false); setDiscount(value); } }; const handelAffilateDiscount = (event) => { let value = event.target.value; if (parseInt(value) >= 9999) { setAffiliateDiscountAmt(9999); setAffiliateDiscountAmtError(true); } else if (parseInt(value) < 0) { setDiscount(0); } else { setAffiliateDiscountAmtError(false); setAffiliateDiscountAmt(value); } }; //Handel Form Submition const handelCreate = (e) => { e.preventDefault(); if (!(coupon.length === 8)) { setCouponError(true); toast.error("Code should be of 8 charecter"); return; } if (valid === "") { setValidError(true); toast.error("Valid till is required"); return; } if (affiliate === "") { setAffiliateError(true); toast.error("Affiliate is required"); return; } if (discount == "") { setDiscountError(true); toast.error("Discount amount is required"); return; } if (affiliateDiscountAmt == "") { setAffiliateDiscountAmtError(true); toast.error("Affiliate Discount Amount is required"); return; } //Sending api Obj-------------------- let formDataObject = { coupon_code: coupon, discount_amount: discount, affiliate_discount_amount: affiliateDiscountAmt, valid_till: valid, is_coupon_active: true, id: affiliate, }; axios .patch("/api/v1/coupon/create", formDataObject, { headers: { Authorization: `Bearer ${token}`, }, }) .then((data) => { //reset Inputs setDiscount(""); setCoupon(""); setValid(""); setAffiliate(""); fetchAffiliate(); setCouponError(false); setDiscountError(false); setValidError(false); setAffiliateError(false); setAffiliateDiscountAmtError(false); swal({ title: "Congratulations!!", text: "The Coupon was Created successfully!", icon: "success", button: "OK", }); }) .catch((error) => { // Handle errors const message = error.response?.data?.message ? error.response?.data?.message : "Something went wrong!"; toast.error(message); console.error("There was a problem with your fetch operation:", error); }) .finally(() => { // Reset the affiliate and affiliateError state setAffiliateError(false); // Show success message // console.log(coupon, discount, valid, affiliate); }); }; const InputSpace = { marginBottom: "1rem", width: "20rem ", height: "45px", }; //Back to Coupons const handelBack = () => { navigate("/affiliate/coupons"); }; //Calling api to get Affiliates const fetchAffiliate = () => { axios .get("/api/v1/coupon/getaffiliate", { headers: { Authorization: `Bearer ${token}`, }, }) .then((response) => { setApiData(response.data.message); }) .catch((error) => { console.log(error.message); }); }; //Calling api useEffect(() => { fetchAffiliate(); }, []); return (

Coupons

Coupon Code (Must be 8 digit) { setCoupon(e.target.value); }} error={couponError} style={{ width: "100%" }} /> {couponError ? ( 8 characters required ) : ( "" )} Discount Amount
Affiliate Discount Amount
{affiliateDiscountAmtError ? ( Max Amount Rs.9999 ) : ( "" )} Coupon Valid Till: { setValid(e.target.value); }} type="date" /> {validError ? ( Validity Date is Required ) : ( "" )} Select Affiliate {affiliateError ? ( Affiliation is Required ) : ( "" )}
); }; export default CreateCoupon;