// LinktreePage.js import React, { useState, useEffect } from 'react'; import styles from './Join.module.css'; // Import the module.css file import { checkCoupon, logCouponForUser } from '../helpers/couponHelpers'; // Import the new helper import Coupon from '../components/Coupon'; import CryptoJS from 'crypto-js'; import { getLocalStorage, } from "../helpers/localStorageHelpers"; const LinktreePage = ({ data, setModal }) => { const queryParams = new URLSearchParams(window.location.search); const [isOnlyClaimCoupon, setIsOnlyClaimCoupon] = useState(false); const [isUsingCoupon, setIsUsingCoupon] = useState(false); const [couponCode, setCouponCode] = useState(''); const [couponStatus, setCouponStatus] = useState(0); const [couponDetails, setCouponDetails] = useState(null); useEffect(() => { if (couponCode !== '') return; const modal = queryParams.get('modal'); const code = queryParams.get('c'); console.log("Received coupon code:", code); if (modal === 'claim-coupon') { if (!getLocalStorage('auth')) { // Preserve the couponCode query param while navigating to the claim-coupon modal if (code) setModal('join', { c: code }); else setModal('join'); } setIsOnlyClaimCoupon(true); setIsUsingCoupon(true); // Automatically switch to the coupon input state } else { if (getLocalStorage('auth')) { // Preserve the couponCode query param while navigating to the claim-coupon modal if (code) setModal('claim-coupon', { c: code }); else setModal('claim-coupon'); } } if (code) { // URL decode the coupon code (it might be URL-encoded) let decodedCode = code.replace('LDNWAlASJDNdaw','+').replace('XCLZBKlaWDJ','/').replace('LDSsadANJlas','='); // Your AES-256 key (ensure this is kept secret and secure!) const secretKey = 'xixixi666'; // 32 characters for AES-256 // Decrypt the coupon code const decryptedBytes = CryptoJS.AES.decrypt(decodedCode, secretKey); const decryptedCode = decryptedBytes.toString(CryptoJS.enc.Utf8); console.log("Decrypted Code:", decryptedCode); if (decryptedCode) { setCouponCode(decryptedCode); setIsUsingCoupon(true); // Automatically switch to the coupon input state handleCheckCoupon(decryptedCode); // Automatically check the coupon code } else { console.log("Failed to decrypt the coupon code"); } } }, [queryParams]); // Handle manual coupon code check const handleCheckCoupon = async (code = couponCode) => { const result = await checkCoupon(code); // Call the helper setCouponStatus(result.coupon ? 200 : 404); setCouponDetails(result.coupon); }; // Handle manual coupon code check const handleLogCouponForUser = async (code = couponCode) => { const result = await logCouponForUser(code); // Call the helper return result.success; }; // Listen for query parameter changes (using the `location` object) useEffect(() => { const handlePopState = () => { const newQueryParams = new URLSearchParams(window.location.search); const newCouponCode = newQueryParams.get('couponCode'); if (!newCouponCode) { setIsUsingCoupon(false); setCouponCode(''); setCouponDetails(null); setCouponStatus(0); } }; window.addEventListener('popstate', handlePopState); return () => { window.removeEventListener('popstate', handlePopState); }; }, []); return (
{!isUsingCoupon ? (
Nikmati Kemudahan Mengelola Kafe
Daftarkan kedaimu sekarang dan mulai gunakan semua fitur unggulan kami.
Pelajari lebih lanjut { e.preventDefault(); // Prevent the default anchor behavior setIsUsingCoupon(true); }} className={styles.footerLink} > Gunakan Voucher
) : (
{isOnlyClaimCoupon ? 'Aktifkan Voucher' : 'Daftar Dengan Voucher'}
Voucher dapat digunakan untuk pembuatan akun bisnis maupun untuk memperpanjang masa berlangganan.
{couponStatus === 0 ? (
e.preventDefault()}> setCouponCode(e.target.value)} />
) : ( <> {couponStatus === 200 &&
} )}
{!getLocalStorage('auth') && Sudah punya akun bisnis? } Pelajari lebih lanjut {(!isOnlyClaimCoupon || couponStatus != 0) && { const url = new URL(window.location.href); url.searchParams.delete('couponCode'); url.searchParams.delete('codeStatus'); window.history.pushState({}, '', url.toString()); setIsUsingCoupon(couponStatus === 0 ? false : true); setCouponCode(''); setCouponDetails(null); setCouponStatus(0); }} className={styles.footerLink} > Kembali }
)}
); }; export default LinktreePage;