acupedia-store-ui/src/components/subscriptions/SubscriptionsView.tsx

177 lines
8.5 KiB
TypeScript

import React, { useState } from 'react';
import { CheckCircle } from 'lucide-react';
import { Pagination } from '../common/Pagination';
import type { UserProfile, ModuleItem } from '../../types';
interface SubscriptionsViewProps {
loggedInUser: UserProfile | null;
onOpenAuth: (mode: 'login') => void;
onSwitchTab: (tab: 'storefront') => void;
curriculumModules: ModuleItem[];
}
export const SubscriptionsView: React.FC<SubscriptionsViewProps> = ({
loggedInUser,
onOpenAuth,
onSwitchTab,
curriculumModules,
}) => {
const [currentPage, setCurrentPage] = useState(1);
const [pageSize, setPageSize] = useState(6);
const subscriptions = loggedInUser?.active_subscriptions || [];
const totalCount = subscriptions.length;
const startIndex = (currentPage - 1) * pageSize;
const paginatedSubscriptions = subscriptions.slice(startIndex, startIndex + pageSize);
return (
<div className="max-w-4xl mx-auto space-y-6 flex-1 w-full">
<div className="space-y-2">
<div className="inline-flex items-center gap-1.5 bg-indigo-50 border border-indigo-200 text-indigo-700 text-xs px-3 py-1 rounded-full font-semibold">
<CheckCircle className="h-3.5 w-3.5" /> Entitlements Ledger
</div>
<h2 className="text-3xl font-extrabold text-slate-900 tracking-tight">My Active Subscriptions</h2>
<p className="text-slate-500 text-sm leading-relaxed font-semibold">
Track your active learning passes, their validity periods, and remaining countdowns. All active subscriptions synchronize automatically inside the Acupedia iOS/Android mobile apps.
</p>
</div>
{/* Subscriptions ledger grid */}
{!loggedInUser ? (
<div className="bg-white border border-slate-200 rounded-3xl p-10 text-center space-y-4 shadow-sm">
<p className="text-sm text-slate-500 font-semibold">
Please register or log in to view active entitlements.
</p>
<button
onClick={() => onOpenAuth('login')}
className="bg-indigo-600 hover:bg-indigo-500 text-white font-bold text-xs px-4 py-2.5 rounded-xl shadow-md transition-colors duration-300"
>
Log In
</button>
</div>
) : subscriptions.length === 0 ? (
<div className="bg-white border border-slate-200 rounded-3xl p-12 text-center space-y-4 shadow-sm">
<div className="h-14 w-14 rounded-2xl bg-indigo-50 flex items-center justify-center mx-auto text-indigo-500">
<CheckCircle className="h-6 w-6" />
</div>
<div className="space-y-1">
<h3 className="text-base font-bold text-slate-800">No active subscriptions</h3>
<p className="text-xs text-slate-500 font-semibold max-w-[320px] mx-auto leading-relaxed">
You do not currently possess any active course passes. Visit the Storefront catalog to unlock TCM learning modules.
</p>
</div>
<button
onClick={() => onSwitchTab('storefront')}
className="bg-indigo-600 hover:bg-indigo-500 text-white font-bold text-xs px-5 py-3 rounded-2xl shadow-lg shadow-indigo-600/10 transition-all duration-300 inline-flex items-center gap-1.5"
>
Explore Modules Catalog
</button>
</div>
) : (
<div className="space-y-6">
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
{paginatedSubscriptions.map((sub: any) => {
// Find module metadata matching sub.plan_code
const moduleMeta = curriculumModules.find((m) => m.code === sub.plan_code);
// Calculate days left
let daysLeft = 0;
const hasExpiry = !!sub.expires_at;
if (hasExpiry) {
const expiry = new Date(sub.expires_at);
const now = new Date();
const diffTime = expiry.getTime() - now.getTime();
daysLeft = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
}
return (
<div
key={sub.id}
className="rounded-3xl p-6 bg-white border border-slate-200/80 shadow-sm flex flex-col justify-between space-y-4 hover:border-slate-300 transition-all duration-300"
>
<div className="space-y-2">
<div className="flex justify-between items-center">
<span className="text-[10px] bg-emerald-50 text-emerald-600 border border-emerald-200 font-bold tracking-widest uppercase px-2.5 py-1 rounded-md">
ACTIVE
</span>
{sub.plan_code === 'LIFETIME_ALL' ? (
<span className="text-[10px] bg-amber-50 text-amber-700 border border-amber-200 font-bold tracking-widest uppercase px-2.5 py-1 rounded-md">
👑 10 Years Pass
</span>
) : (
<span className="text-[10px] bg-indigo-50 text-indigo-600 border border-indigo-200 font-bold tracking-widest uppercase px-2.5 py-1 rounded-md">
1 Year Pass
</span>
)}
</div>
<h3 className="text-lg font-bold text-slate-800">
{moduleMeta ? moduleMeta.name : sub.plan_code.replace(/_/g, ' ').toUpperCase()}
</h3>
<p className="text-xs text-slate-500 leading-relaxed font-semibold">
{moduleMeta ? moduleMeta.desc : 'Unlocked subscription access to module contents.'}
</p>
</div>
<div className="pt-4 border-t border-slate-100 flex flex-col gap-2">
<div className="flex justify-between text-xs font-semibold text-slate-500">
<span>Activated:</span>
<span className="text-slate-800 font-bold">
{sub.started_at
? new Date(sub.started_at).toLocaleDateString(undefined, { dateStyle: 'medium' })
: 'N/A'}
</span>
</div>
<div className="flex justify-between text-xs font-semibold text-slate-500">
<span>Expires:</span>
<span className="text-slate-800 font-bold">
{hasExpiry
? new Date(sub.expires_at).toLocaleDateString(undefined, { dateStyle: 'medium' })
: 'Lifetime'}
</span>
</div>
<div className="mt-2 flex justify-between items-center bg-slate-50 p-3 rounded-2xl border border-slate-100">
<span className="text-[11px] text-slate-500 font-bold uppercase tracking-wider">
Validity Remaining:
</span>
{!hasExpiry ? (
<span className="text-xs bg-amber-100 text-amber-800 border border-amber-200 font-extrabold px-3 py-1 rounded-xl shadow-sm animate-pulse">
Lifetime Access
</span>
) : sub.plan_code === 'LIFETIME_ALL' ? (
<span className="text-xs bg-amber-100 text-amber-800 border border-amber-200 font-extrabold px-3 py-1 rounded-xl shadow-sm animate-pulse">
{daysLeft > 0 ? `${(daysLeft / 365).toFixed(1)} Years Left` : 'Expired'}
</span>
) : (
<span className="text-xs bg-emerald-100 text-emerald-800 border border-emerald-200 font-extrabold px-3 py-1 rounded-xl shadow-sm">
{daysLeft > 0 ? `${daysLeft} Days Left` : 'Expired'}
</span>
)}
</div>
</div>
</div>
);
})}
</div>
{/* Pagination Controls */}
{totalCount > 6 && (
<div className="bg-white rounded-2xl border border-slate-200/80 p-2 shadow-sm">
<Pagination
currentPage={currentPage}
totalItems={totalCount}
itemsPerPage={pageSize}
onPageChange={setCurrentPage}
onItemsPerPageChange={(size) => {
setPageSize(size);
setCurrentPage(1);
}}
pageSizeOptions={[6, 12, 24]}
/>
</div>
)}
</div>
)}
</div>
);
};