Files
co-lite/solvway-customer-portal/app/draw/page.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

251 lines
13 KiB
TypeScript

"use client";
import { useState, useEffect } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { Sparkles, Ticket, User, History, Loader2, ExternalLink } from "lucide-react";
import { useTheme } from "@/app/lib/ThemeContext";
import { useMember } from "@/app/lib/MemberContext";
import { apiGet } from "@/app/lib/clientApi";
import EventGallerySection from "@/app/components/ui/EventGallerySection";
import type { GalleryImage } from "@/app/components/ui/ImageGallery";
import type { LuckyDrawInfo, LuckyDrawReceipt } from "@/app/lib/types";
const POSTER_GRADIENT = "linear-gradient(145deg,#FF6B00 0%,#290a81 100%)";
const STATUS_STYLE: Record<string, { bg: string; color: string; border: string }> = {
P: { bg: "rgba(245,158,11,0.12)", color: "#f59e0b", border: "rgba(245,158,11,0.3)" },
A: { bg: "rgba(34,197,94,0.12)", color: "#22c55e", border: "rgba(34,197,94,0.3)" },
R: { bg: "rgba(239,68,68,0.12)", color: "#ef4444", border: "rgba(239,68,68,0.3)" },
};
const stagger = { hidden: {}, visible: { transition: { staggerChildren: 0.07 } } };
const fadeUp = {
hidden: { opacity: 0, y: 16 },
visible: { opacity: 1, y: 0, transition: { type: "spring" as const, stiffness: 340, damping: 28 } },
};
export default function DrawPage() {
const { isDark } = useTheme();
const { member } = useMember();
const [info, setInfo] = useState<LuckyDrawInfo | null>(null);
const [loading, setLoading] = useState(true);
const [noEvent, setNoEvent] = useState(false);
const [showHistory, setShowHistory] = useState(false);
const [history, setHistory] = useState<LuckyDrawReceipt[] | null>(null);
const [historyLoading, setHistoryLoading] = useState(false);
const [historyError, setHistoryError] = useState(false);
const loadHistory = () => {
setHistoryLoading(true);
setHistoryError(false);
apiGet<LuckyDrawReceipt[]>("/api/event/lucky-draw-history")
.then((d) => setHistory(d))
.catch(() => setHistoryError(true))
.finally(() => setHistoryLoading(false));
};
const openHistory = () => {
setShowHistory(true);
// Load on first open; also retry automatically if a previous attempt errored.
if (!historyLoading && (history === null || historyError)) loadHistory();
};
useEffect(() => {
apiGet<LuckyDrawInfo>("/api/event/lucky-draw")
.then((d) => setInfo(d))
.catch(() => setNoEvent(true))
.finally(() => setLoading(false));
}, []);
const t = {
text: isDark ? "#FFFFFF" : "#1a1a1a",
textMuted: isDark ? "rgba(255,255,255,0.6)" : "#6B6B6B",
card: isDark ? "rgba(255,255,255,0.06)" : "#FFFFFF",
border: isDark ? "rgba(255,255,255,0.08)" : "rgba(0,0,0,0.07)",
shadow: isDark
? "0 4px 24px rgba(0,0,0,0.35), inset 0 1px 0 rgba(255,255,255,0.06)"
: "0 2px 16px rgba(0,0,0,0.07), 0 1px 3px rgba(0,0,0,0.04)",
};
const posterImages: GalleryImage[] = info?.posterUrl
? [{ id: "ld-poster", title: "Lucky Draw", subtitle: "Tap to view or download", gradient: POSTER_GRADIENT, src: info.posterUrl }]
: [];
return (
<div className="min-h-full" style={{ background: "var(--bg-primary)" }}>
<motion.div variants={stagger} initial="hidden" animate="visible" className="pt-5 pb-28 space-y-5">
{/* Header */}
<motion.div variants={fadeUp} className="px-4">
<div className="flex flex-wrap items-center justify-between gap-x-3 gap-y-2 mb-1">
<div className="flex items-center gap-2">
<Sparkles strokeWidth={2} className="w-4 h-4 stroke-tastefullyOrange" />
<p className="text-xs font-black tracking-[0.2em] uppercase" style={{ color: "var(--orange-text)" }}>Lucky Draw</p>
</div>
{!noEvent && (
<motion.button
onClick={openHistory}
whileTap={{ scale: 0.88 }}
transition={{ type: "spring", stiffness: 420, damping: 24 }}
className="ml-auto flex-shrink-0 flex items-center gap-1.5 px-3 py-2 rounded-xl"
style={{ background: isDark ? "rgba(255,255,255,0.06)" : "rgba(0,0,0,0.05)", border: `1px solid ${t.border}` }}
>
<History strokeWidth={2} className="w-4 h-4" style={{ stroke: t.textMuted }} />
<span className="text-sm font-semibold" style={{ color: t.textMuted }}>History</span>
</motion.button>
)}
</div>
<h1 className="text-4xl font-black tracking-tight leading-none" style={{ color: "var(--orange-text)" }}>Lucky Draw</h1>
<p className="text-sm mt-1.5" style={{ color: t.textMuted }}>Your chance to win big — every ticket counts</p>
</motion.div>
{noEvent ? (
<motion.div
variants={fadeUp}
className="mx-4 flex flex-col items-center justify-center py-12 rounded-2xl text-center"
style={{ background: t.card, border: `1px solid ${t.border}`, boxShadow: t.shadow }}
>
<Ticket strokeWidth={1.5} className="w-8 h-8 mb-2" style={{ stroke: t.textMuted }} />
<p className="text-sm font-semibold" style={{ color: t.text }}>No active event</p>
<p className="text-xs mt-0.5 px-8" style={{ color: t.textMuted }}>Lucky draw details appear when an event is live.</p>
</motion.div>
) : (
<>
{/* Member + stats card */}
<motion.div
variants={fadeUp}
className="mx-4 p-4 rounded-[1.25rem] overflow-hidden relative"
style={{ background: "linear-gradient(135deg, #C94D00 0%, #A83F00 100%)", boxShadow: "0 8px 32px rgba(255,107,0,0.35), 0 2px 8px rgba(0,0,0,0.15)" }}
>
<div className="absolute inset-0 pointer-events-none rounded-[1.25rem]" style={{ background: "radial-gradient(ellipse at 80% 20%, rgba(255,255,255,0.12) 0%, transparent 60%)" }} />
<div className="relative flex items-center gap-2 mb-4">
<div className="w-8 h-8 rounded-xl flex items-center justify-center" style={{ background: "rgba(255,255,255,0.2)", border: "1px solid rgba(255,255,255,0.6)" }}>
<User strokeWidth={2} className="w-4 h-4 stroke-white" />
</div>
<div className="min-w-0">
<p className="text-sm font-bold text-white leading-tight truncate">{member?.name ?? "—"}</p>
<p className="text-xs font-mono truncate" style={{ color: "rgba(255,255,255,0.6)" }}>{member?.memberCode ?? ""}</p>
</div>
</div>
<div className="relative flex items-stretch gap-3">
<div className="flex-1 rounded-xl px-3 py-2.5" style={{ background: "rgba(255,255,255,0.15)", border: "1px solid rgba(255,255,255,0.25)" }}>
<p className="text-xs font-semibold uppercase tracking-widest mb-1" style={{ color: "rgba(255,255,255,0.65)" }}>Amount Spent</p>
<p className="text-2xl font-extrabold tracking-tight text-white leading-none">RM {(info?.totalSpent ?? 0).toLocaleString()}</p>
</div>
<div className="flex-1 rounded-xl px-3 py-2.5" style={{ background: "rgba(255,255,255,0.15)", border: "1px solid rgba(255,255,255,0.25)" }}>
<p className="text-xs font-semibold uppercase tracking-widest mb-1" style={{ color: "rgba(255,255,255,0.65)" }}>Tickets</p>
<p className="text-2xl font-extrabold tracking-tight text-white leading-none flex items-center gap-1.5">
<Ticket strokeWidth={2} className="w-5 h-5 stroke-white" />{info?.ticketCount ?? 0}
</p>
</div>
</div>
</motion.div>
{/* Poster */}
<motion.div variants={fadeUp}>
<EventGallerySection loading={loading} images={posterImages} emptyMessage="The lucky draw poster isn't available yet." />
</motion.div>
</>
)}
</motion.div>
{/* History bottom sheet */}
<AnimatePresence>
{showHistory && (
<>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
onClick={() => setShowHistory(false)}
className="fixed inset-0 z-50"
style={{ background: "rgba(0,0,0,0.5)", backdropFilter: "blur(4px)" }}
/>
<motion.div
initial={{ y: "100%" }}
animate={{ y: 0 }}
exit={{ y: "100%" }}
transition={{ type: "spring", stiffness: 340, damping: 30 }}
className="fixed bottom-0 left-0 right-0 z-50 rounded-t-3xl px-5 pt-5 pb-28"
style={{ background: isDark ? "#1a1a1a" : "#FFFFFF", boxShadow: "0 -8px 40px rgba(0,0,0,0.3)", maxHeight: "70vh", overflowY: "auto" }}
>
<div className="w-10 h-1 rounded-full mx-auto mb-4" style={{ background: t.textMuted }} />
<div className="flex items-center justify-between mb-4">
<h2 className="text-lg font-bold" style={{ color: t.text }}>Receipt History</h2>
<motion.button
onClick={() => setShowHistory(false)}
whileTap={{ scale: 0.9 }}
className="text-xs font-semibold px-3 py-1.5 rounded-xl"
style={{ background: isDark ? "rgba(255,255,255,0.06)" : "rgba(0,0,0,0.05)", color: t.textMuted, border: `1px solid ${t.border}` }}
>
Close
</motion.button>
</div>
{historyLoading ? (
<div className="flex justify-center py-10">
<motion.div animate={{ rotate: 360 }} transition={{ repeat: Infinity, duration: 0.8, ease: "linear" }}>
<Loader2 strokeWidth={2} className="w-6 h-6 stroke-tastefullyOrange" />
</motion.div>
</div>
) : historyError ? (
<div className="flex flex-col items-center py-10 text-center">
<p className="text-sm font-semibold" style={{ color: t.text }}>Couldn&apos;t load history</p>
<button onClick={loadHistory} className="mt-2 text-xs font-bold px-3 py-1.5 rounded-xl" style={{ background: "rgba(255,107,0,0.12)", border: "1px solid rgba(255,107,0,0.3)", color: "var(--orange-text)" }}>
Try again
</button>
</div>
) : !history || history.length === 0 ? (
<p className="text-xs text-center py-10" style={{ color: t.textMuted }}>No receipts uploaded today.</p>
) : (
<div className="space-y-2.5">
{history.map((r, i) => {
const s = STATUS_STYLE[r.reviewStatus] ?? STATUS_STYLE.P;
return (
<div
key={i}
className="px-4 py-3 rounded-2xl"
style={{ background: t.card, border: `1px solid ${t.border}`, boxShadow: t.shadow }}
>
<div className="flex items-center justify-between gap-2">
<p className="text-xs font-bold" style={{ color: t.text }}>{r.uploadDateTime}</p>
<div className="flex items-center gap-2 flex-shrink-0">
{r.reviewStatus === "A" && r.ticketCount > 0 && (
<span className="text-xs font-bold" style={{ color: "var(--orange-text)" }}>+{r.ticketCount} 🎟</span>
)}
<span
className="text-xs font-bold uppercase tracking-widest px-2 py-0.5 rounded-full"
style={{ background: s.bg, color: s.color, border: `1px solid ${s.border}` }}
>
{r.reviewStatusLabel}
</span>
</div>
</div>
{r.receiptUrls.length > 0 && (
<div className="flex flex-wrap gap-2 mt-2">
{r.receiptUrls.map((url, j) => (
<a
key={j}
href={url}
target="_blank"
rel="noopener noreferrer"
className="flex items-center gap-1 text-xs font-semibold px-2 py-1 rounded-lg"
style={{ background: "rgba(255,107,0,0.1)", border: "1px solid rgba(255,107,0,0.25)", color: "var(--orange-text)" }}
>
<ExternalLink strokeWidth={2} className="w-3 h-3" /> View {j + 1}
</a>
))}
</div>
)}
</div>
);
})}
</div>
)}
</motion.div>
</>
)}
</AnimatePresence>
</div>
);
}