import { useState, useEffect } from 'react' import { useNavigate } from 'react-router-dom' import Navbar from '../components/Navbar' import Footer from '../components/Footer' import EmptyState from '../components/EmptyState' import DynamicPageRenderer from '../components/DynamicPageRenderer' import { API_BASE, type Category, type PageSection } from '../types' import { Ticket, Copy, CheckCircle2, ArrowRight } from 'lucide-react' interface Coupon { id: number code: string type: string value: number minPurchase: number usageLimit: number | null usedCount: number expiresAt: string | null } export default function CouponsPage() { const navigate = useNavigate() const [categories, setCategories] = useState([]) const [coupons, setCoupons] = useState([]) const [loading, setLoading] = useState(true) const [copiedCode, setCopiedCode] = useState(null) const [cmsSections, setCmsSections] = useState([]) useEffect(() => { fetch(`${API_BASE}/categories`) .then(res => res.json()) .then(setCategories) .catch(console.error) fetch(`${API_BASE}/pages/coupons-layout`) .then((r) => { if (!r.ok) throw new Error('Page not found'); return r.json(); }) .then((data) => { if (data && data.sections) { setCmsSections(data.sections); } }) .catch((err) => { console.log('Coupons dynamic layout sections not loaded:', err.message); }); }, []) useEffect(() => { fetch(`${API_BASE}/categories`) .then(res => res.json()) .then(setCategories) .catch(console.error) fetch(`${API_BASE}/coupons/active`) .then(res => res.json()) .then(data => { // Filter out coupons that have reached their limit const availableCoupons = data.filter((c: Coupon) => c.usageLimit === null || c.usedCount < c.usageLimit ) setCoupons(availableCoupons) }) .catch(console.error) .finally(() => setLoading(false)) }, []) const formatDiscount = (type: string, value: number) => { if (type === 'PERCENTAGE') return `${value}% OFF` if (type === 'FIXED_AMOUNT') return `ลด ${value} ฿` if (type === 'FREE_SHIPPING') return `ส่งฟรี` return `${value}` } const handleCopy = (code: string) => { navigator.clipboard.writeText(code) setCopiedCode(code) setTimeout(() => setCopiedCode(null), 2000) } const handleUseNow = (code: string) => { localStorage.setItem('homaroma_active_coupon', code) navigate('/shop') } const announcementSections = cmsSections.filter(s => s.isActive && s.type.toUpperCase() === 'ANNOUNCEMENT_BAR'); const heroSections = cmsSections.filter(s => s.isActive && (s.type.toUpperCase() === 'HERO' || s.type.toUpperCase() === 'BANNER')); const bottomSections = cmsSections.filter(s => s.isActive && s.type.toUpperCase() !== 'ANNOUNCEMENT_BAR' && s.type.toUpperCase() !== 'HERO' && s.type.toUpperCase() !== 'BANNER'); return (
{announcementSections.length > 0 && ( )}
{/* Header */} {heroSections.length > 0 ? ( ) : (

Exclusive Offers

คูปองส่วนลดพิเศษสำหรับคุณ เลือกใช้คูปองที่คุณต้องการแล้วไปช้อปกันเลย!

)} {loading ? (
{[1, 2, 3].map(i => (
))}
) : coupons.length === 0 ? ( ) : (
{coupons.map(coupon => (
{/* Decoration */}
{coupon.expiresAt && ( หมดเขต {new Date(coupon.expiresAt).toLocaleDateString('th-TH')} )}

{formatDiscount(coupon.type, coupon.value)}

เมื่อซื้อครบ {coupon.minPurchase} บาท

{coupon.code}
{/* Scalloped edge effect */}
))}
)}
{bottomSections.length > 0 && ( )}
) }