ok
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import React, { useState } from "react";
|
||||
|
||||
import API_BASE_URL from "../config.js";
|
||||
import {createCoupon} from "../helpers/couponHelpers.js"
|
||||
|
||||
function getAuthToken() {
|
||||
return localStorage.getItem("auth");
|
||||
@@ -11,30 +11,17 @@ const CreateCouponPage = () => {
|
||||
const [discountPeriods, setDiscountPeriods] = useState("");
|
||||
const [message, setMessage] = useState("");
|
||||
|
||||
|
||||
const handleSubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/coupon/create`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${getAuthToken()}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
discountType,
|
||||
discountValue,
|
||||
discountPeriods,
|
||||
}),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
setMessage(`Coupon created successfully: ${data.coupon.code}`);
|
||||
} else {
|
||||
setMessage("Failed to create coupon.");
|
||||
}
|
||||
} catch (error) {
|
||||
setMessage("Error creating coupon.");
|
||||
|
||||
// Call the helper function to create the coupon
|
||||
const result = await createCoupon(discountType, discountValue, discountPeriods);
|
||||
|
||||
if (result.success) {
|
||||
setMessage(`Coupon created successfully: ${result.couponCode}`);
|
||||
} else {
|
||||
setMessage(result.message);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user