import React, { useState } from "react"; import styles from './Join.module.css'; import Coupon from '../components/Coupon'; import CryptoJS from 'crypto-js'; import { createCoupon } from "../helpers/couponHelpers.js" function getAuthToken() { return localStorage.getItem("auth"); } const CreateCouponPage = () => { const [discountType, setDiscountType] = useState("percentage"); const [discountValue, setDiscountValue] = useState(""); const [discountPeriods, setDiscountPeriods] = useState(""); const [couponDetails, setCouponDetails] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(false); const [wasInputtingPassword, setWasInputtingPassword] = useState(false); const [inputtingPassword, setInputtingPassword] = useState(false); const [username, setUsername] = useState(''); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [retypePassword, setRetypePassword] = useState(''); const [codeExpectation, setCodeExpectation] = useState(''); const [couponUrl, setCouponUrl] = useState(''); const handleSubmit = async (e) => { e.preventDefault(); setLoading(true); // Call the helper function to create the coupon const result = await createCoupon(discountType, discountValue, discountPeriods, codeExpectation); if (result.success) { setCouponDetails(result.coupon); const secretKey = 'xixixi666'; // AES-256 key (32 characters) // Encrypt the coupon code inline using CryptoJS let encryptedCouponCode = CryptoJS.AES.encrypt(result.coupon.code, secretKey).toString().replace('+','LDNWAlASJDNdaw').replace('/','XCLZBKlaWDJ').replace('=','LDSsadANJlas'); // Encode the encrypted coupon code for URL usage: // 1. First, encodeURIComponent to handle URL-specific characters. // 2. Then, replace special characters like '+' and '/' that might cause issues. let encodedCouponCode = encodeURIComponent(encryptedCouponCode); // Construct the URL with the encoded coupon code as a query parameter const urlWithCoupon = `https://dev.coupon.kedaimaster.com/coupon?c=${encodedCouponCode}`; // Optionally, set the URL to use with the coupon setCouponUrl(urlWithCoupon); console.log("Generated URL with Encrypted Coupon Code:", urlWithCoupon); setLoading(false); } else { setLoading(false); } }; return (
Buat Voucher {couponDetails != null && 'Berhasil'}
Daftarkan kedaimu sekarang dan mulai gunakan semua fitur unggulan kami.
{couponDetails != null ? <> :
setDiscountValue(e.target.value)} className={!error ? styles.usernameInput : styles.usernameInputError} />
setDiscountPeriods(e.target.value)} maxLength="30" className={!error ? styles.usernameInput : styles.usernameInputError} /> setCodeExpectation(e.target.value)} maxLength="30" className={!error ? styles.usernameInput : styles.usernameInputError} />
}
); }; export default CreateCouponPage;