// 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); // Detect query params on component mount useEffect(() => { if (couponCode !== '') return; const modal = queryParams.get('modal'); const code = queryParams.get('c'); console.log(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) { // Your AES-256 key (ensure this is kept secret and secure!) const secretKey = 'xixixi666'; // 32 characters for AES-256 // Decrypt the couponCode const decryptedBytes = CryptoJS.AES.decrypt(code, secretKey); const decryptedCode = decryptedBytes.toString(CryptoJS.enc.Utf8); console.log(decryptedCode) setCouponCode(decryptedCode); setIsUsingCoupon(true); // Automatically switch to the coupon input state handleCheckCoupon(decryptedCode); // Automatically check 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 }; // 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 (