Files
co-lite/solvway-customer-portal/app/settings/transactions/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

230 lines
9.8 KiB
TypeScript

"use client";
import { useState, useEffect, useCallback } from "react";
import { motion } from "framer-motion";
import { Coins, Gift, ArrowDownRight, ArrowUpRight, Filter, Loader2 } from "lucide-react";
import { useTheme } from "@/app/lib/ThemeContext";
import { apiPost } from "@/app/lib/clientApi";
import { classifyTrxn, signedPoints, type TxKind } from "@/app/lib/transactions";
import type { PointTransaction, TransactionsPage } from "@/app/lib/types";
const FILTERS = ["All", "Earned", "Redeemed"] as const;
const filterMap: Record<string, TxKind | null> = {
All: null,
Earned: "earn",
Redeemed: "redeem",
};
const KIND_ICON = { earn: Coins, redeem: Gift } as const;
const KIND_COLOR = { earn: "#22c55e", redeem: "#ef4444" } as const;
const stagger = { hidden: {}, visible: { transition: { staggerChildren: 0.05 } } };
const fadeUp = {
hidden: { opacity: 0, y: 12 },
visible: { opacity: 1, y: 0, transition: { type: "spring" as const, stiffness: 340, damping: 28 } },
};
interface Row {
id: string;
kind: TxKind;
label: string;
pts: number;
date: string;
time: string;
}
function toRow(t: PointTransaction): Row {
const [date, time] = (t.transactionDateTime || "").split(" ");
return {
id: t.uuid,
kind: classifyTrxn(t),
label: t.description || t.refNo || "Transaction",
pts: signedPoints(t),
date: date ?? t.transactionDateTime ?? "",
time: time ?? "",
};
}
export default function TransactionsPage() {
const { isDark } = useTheme();
const [filter, setFilter] = useState<string>("All");
const [rows, setRows] = useState<Row[]>([]);
const [page, setPage] = useState(0);
const [hasNext, setHasNext] = useState(false);
const [loading, setLoading] = useState(true);
const [loadingMore, setLoadingMore] = useState(false);
const loadPage = useCallback(async (p: number) => {
const data = await apiPost<TransactionsPage>("/api/points/transactions", { page: p, pageSize: 20 });
setHasNext(Boolean(data.hasNext));
setRows((prev) => (p === 0 ? data.content.map(toRow) : [...prev, ...data.content.map(toRow)]));
setPage(p);
}, []);
useEffect(() => {
// eslint-disable-next-line react-hooks/set-state-in-effect -- mount-time page fetch; setState runs in the async .then/.finally callbacks, not synchronously.
loadPage(0)
.catch(() => {})
.finally(() => setLoading(false));
}, [loadPage]);
const onLoadMore = async () => {
setLoadingMore(true);
try {
await loadPage(page + 1);
} catch {
/* ignore */
} finally {
setLoadingMore(false);
}
};
const t = {
text: isDark ? "#FFFFFF" : "#1a1a1a",
textSec: isDark ? "rgba(255,255,255,0.8)" : "#5C5C5C",
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)",
divider: isDark ? "rgba(255,255,255,0.05)" : "rgba(0,0,0,0.06)",
};
const card = { background: t.card, border: `1px solid ${t.border}`, boxShadow: t.shadow, borderRadius: "1.25rem" };
const kind = filterMap[filter];
const filtered = kind ? rows.filter((r) => r.kind === kind) : rows;
const totalEarned = rows.filter((r) => r.pts > 0).reduce((s, r) => s + r.pts, 0);
const totalSpent = rows.filter((r) => r.pts < 0).reduce((s, r) => s + Math.abs(r.pts), 0);
return (
<div className="min-h-full pt-5 pb-28 space-y-5" style={{ background: "var(--bg-primary)" }}>
{/* Summary cards */}
<motion.div
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0 }}
transition={{ type: "spring", stiffness: 320, damping: 26 }}
className="grid grid-cols-2 gap-3 px-4"
>
<div className="p-3.5 rounded-2xl" style={card}>
<div className="flex items-center gap-2 mb-2">
<div className="w-7 h-7 rounded-lg flex items-center justify-center" style={{ background: "rgba(34,197,94,0.1)", border: "1px solid rgba(34,197,94,0.2)" }}>
<ArrowDownRight strokeWidth={2} className="w-3.5 h-3.5" style={{ stroke: "#22c55e" }} />
</div>
<span className="text-xs font-bold tracking-widest uppercase" style={{ color: t.textMuted }}>Earned</span>
</div>
<p className="text-xl font-extrabold" style={{ color: "#22c55e" }}>
{totalEarned > 0 && "+"}{totalEarned.toLocaleString()}
<span className="text-xs font-semibold ml-1" style={{ color: t.textMuted }}>pts</span>
</p>
</div>
<div className="p-3.5 rounded-2xl" style={card}>
<div className="flex items-center gap-2 mb-2">
<div className="w-7 h-7 rounded-lg flex items-center justify-center" style={{ background: "rgba(239,68,68,0.1)", border: "1px solid rgba(239,68,68,0.2)" }}>
<ArrowUpRight strokeWidth={2} className="w-3.5 h-3.5" style={{ stroke: "#ef4444" }} />
</div>
<span className="text-xs font-bold tracking-widest uppercase" style={{ color: t.textMuted }}>Spent</span>
</div>
<p className="text-xl font-extrabold" style={{ color: "#ef4444" }}>
{totalSpent > 0 && "-"}{totalSpent.toLocaleString()}
<span className="text-xs font-semibold ml-1" style={{ color: t.textMuted }}>pts</span>
</p>
</div>
</motion.div>
{/* Filter pills */}
<motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} transition={{ delay: 0.1 }} className="flex gap-2 overflow-x-auto scrollbar-hide px-4">
{FILTERS.map((f) => {
const active = filter === f;
return (
<motion.button
key={f}
onClick={() => setFilter(f)}
whileTap={{ scale: 0.93 }}
transition={{ type: "spring", stiffness: 420, damping: 26 }}
className="flex-shrink-0 px-3.5 py-1.5 rounded-full text-xs font-bold"
style={{
background: active ? "#FF6B00" : t.card,
border: active ? "none" : `1px solid ${t.border}`,
color: active ? "#FFFFFF" : t.textSec,
boxShadow: active ? "0 4px 12px rgba(255,107,0,0.35)" : t.shadow,
}}
>
{f}
</motion.button>
);
})}
</motion.div>
{/* List */}
<div className="px-4">
<div className="flex items-center justify-between mb-3">
<p className="text-xs font-bold tracking-widest uppercase" style={{ color: t.textMuted }}>History</p>
<span className="text-xs" style={{ color: t.textMuted }}>{filtered.length} shown</span>
</div>
{loading ? (
<div className="flex justify-center py-10" style={{ ...card }}>
<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>
) : (
<motion.div key={filter} variants={stagger} initial="hidden" animate="visible" style={{ ...card, padding: 0, overflow: "hidden" }}>
{filtered.length === 0 ? (
<div className="flex flex-col items-center justify-center py-10">
<Filter 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 transactions</p>
<p className="text-xs mt-0.5" style={{ color: t.textMuted }}>Try a different filter</p>
</div>
) : (
filtered.map((tx, idx) => {
const Icon = KIND_ICON[tx.kind];
const color = KIND_COLOR[tx.kind];
const positive = tx.pts > 0;
return (
<motion.div
key={tx.id}
variants={fadeUp}
className="flex items-center gap-3 px-4 py-3"
style={{ borderBottom: idx < filtered.length - 1 ? `1px solid ${t.divider}` : "none" }}
>
<div className="w-9 h-9 rounded-xl flex items-center justify-center flex-shrink-0" style={{ background: `${color}15`, border: `1px solid ${color}30` }}>
<Icon strokeWidth={2} className="w-4 h-4" style={{ stroke: color }} />
</div>
<div className="flex-1 min-w-0">
<p className="text-xs font-semibold leading-tight" style={{ color: t.text }}>{tx.label}</p>
<p className="text-xs mt-0.5" style={{ color: t.textMuted }}>{tx.date} {tx.time && `• ${tx.time}`}</p>
</div>
<span className="text-xs font-extrabold flex-shrink-0" style={{ color: positive ? "#22c55e" : "#ef4444" }}>
{positive ? "+" : ""}{tx.pts.toLocaleString()} pts
</span>
</motion.div>
);
})
)}
</motion.div>
)}
{hasNext && !loading && (
<motion.button
onClick={onLoadMore}
disabled={loadingMore}
whileTap={{ scale: 0.97 }}
className="w-full mt-4 py-3 rounded-2xl text-sm font-semibold flex items-center justify-center gap-2"
style={{ background: t.card, border: `1px solid ${t.border}`, color: t.textSec, boxShadow: t.shadow }}
>
{loadingMore ? (
<motion.div animate={{ rotate: 360 }} transition={{ repeat: Infinity, duration: 0.8, ease: "linear" }}>
<Loader2 strokeWidth={2} className="w-4 h-4" />
</motion.div>
) : (
"Load more"
)}
</motion.button>
)}
</div>
</div>
);
}