acupedia-store-ui/src/components/auth/ProfileDrawer.tsx

404 lines
19 KiB
TypeScript

import React, { useState } from 'react';
import {
XCircle, Users, ShieldCheck, CheckCircle, AlertCircle,
Eye, EyeOff, RefreshCw, LogOut
} from 'lucide-react';
import type { UserProfile, ModuleItem, Currency } from '../../types';
import { COUNTRY_CODES, getCountryRuleByName } from '../../constants/countries';
import { cleanPhoneInput } from '../../utils/validation';
interface ProfileDrawerProps {
isOpen: boolean;
onClose: () => void;
loggedInUser: UserProfile | null;
profileDisplayName: string;
setProfileDisplayName: (name: string) => void;
profilePhone: string;
setProfilePhone: (phone: string) => void;
profileCountry: string;
setProfileCountry: (country: string) => void;
setCurrency: (currency: Currency) => void;
profileSaving: boolean;
profileError: string | null;
profileSuccess: boolean;
onUpdateProfile: (e: React.FormEvent) => Promise<void>;
showPasswordChange: boolean;
setShowPasswordChange: (show: boolean) => void;
changeOldPassword: string;
setChangeOldPassword: (pwd: string) => void;
changeNewPassword: string;
setChangeNewPassword: (pwd: string) => void;
changeConfirmPassword: string;
setChangeConfirmPassword: (pwd: string) => void;
changePasswordLoading: boolean;
changePasswordError: string | null;
changePasswordSuccess: boolean;
onChangePassword: (e: React.FormEvent) => Promise<void>;
onLogout: () => void;
onBrowseStorefront: () => void;
curriculumModules: ModuleItem[];
}
export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
isOpen,
onClose,
loggedInUser,
profileDisplayName,
setProfileDisplayName,
profilePhone,
setProfilePhone,
profileCountry,
setProfileCountry,
setCurrency,
profileSaving,
profileError,
profileSuccess,
onUpdateProfile,
showPasswordChange,
setShowPasswordChange,
changeOldPassword,
setChangeOldPassword,
changeNewPassword,
setChangeNewPassword,
changeConfirmPassword,
setChangeConfirmPassword,
changePasswordLoading,
changePasswordError,
changePasswordSuccess,
onChangePassword,
onLogout,
onBrowseStorefront,
curriculumModules,
}) => {
const [showOldPassword, setShowOldPassword] = useState(false);
const [showNewPassword, setShowNewPassword] = useState(false);
const activeRule = getCountryRuleByName(profileCountry);
if (!isOpen || !loggedInUser) return null;
return (
<div className="fixed inset-0 z-50 overflow-hidden flex justify-end">
{/* Overlay Backdrop */}
<div
className="absolute inset-0 bg-slate-900/60 backdrop-blur-sm transition-opacity"
onClick={onClose}
/>
{/* Drawer Container */}
<div className="relative w-full max-w-md bg-white h-full shadow-2xl flex flex-col justify-between z-10 animate-slide-in overflow-hidden border-l border-slate-200">
{/* Drawer Header */}
<div className="px-6 py-5 bg-[#0A0A0E] text-white flex items-center justify-between border-b border-slate-800">
<div className="flex items-center gap-3">
<div className="h-10 w-10 rounded-full bg-gradient-to-tr from-indigo-600 to-violet-500 border border-indigo-400/30 flex items-center justify-center text-white font-black text-base shadow-inner">
{(loggedInUser.display_name || loggedInUser.email || 'U')[0].toUpperCase()}
</div>
<div>
<h3 className="text-sm font-bold text-white leading-tight">
{loggedInUser.display_name || 'My Account'}
</h3>
<p className="text-[11px] text-slate-400 font-medium">{loggedInUser.email}</p>
</div>
</div>
<button
onClick={onClose}
className="text-slate-400 hover:text-white transition-colors p-1"
>
<XCircle className="h-5 w-5" />
</button>
</div>
{/* Drawer Body (Scrollable) */}
<div className="flex-1 p-6 space-y-8 overflow-y-auto">
{/* Profile Details Form */}
<div className="space-y-4">
<h4 className="text-xs font-bold text-slate-400 uppercase tracking-widest flex items-center gap-1.5">
<Users className="h-4 w-4 text-indigo-500" /> Update Account details
</h4>
<form
onSubmit={onUpdateProfile}
className="space-y-4 bg-slate-50 border border-slate-200/60 p-4 rounded-2xl"
>
{profileSuccess && (
<div className="p-3 bg-emerald-50 border border-emerald-200 text-emerald-700 text-xs font-semibold rounded-xl">
Changes saved successfully! ✓
</div>
)}
{profileError && (
<div className="p-3 bg-rose-50 border border-rose-200 text-rose-700 text-xs font-semibold rounded-xl">
{profileError}
</div>
)}
<div className="space-y-1.5">
<label className="text-xs font-bold text-slate-500">Display Name</label>
<input
type="text"
value={profileDisplayName}
onChange={(e) => setProfileDisplayName(e.target.value)}
required
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500"
/>
</div>
<div className="space-y-1.5">
<label className="text-xs font-bold text-slate-500">Phone Number</label>
<input
type="text"
value={profilePhone}
onChange={(e) => setProfilePhone(cleanPhoneInput(e.target.value, activeRule.maxLength))}
placeholder={activeRule.placeholder}
maxLength={activeRule.maxLength}
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500"
/>
<p className="text-[10px] text-slate-400 font-medium">
{activeRule.minLength === activeRule.maxLength
? `${activeRule.label || activeRule.country}: Requires exactly ${activeRule.maxLength} digits`
: `${activeRule.label || activeRule.country}: ${activeRule.minLength} to ${activeRule.maxLength} digits`}
</p>
</div>
<div className="space-y-1.5">
<label className="text-xs font-bold text-slate-500">Billing Country</label>
<select
value={profileCountry}
onChange={(e) => {
const newCountry = e.target.value;
setProfileCountry(newCountry);
const rule = getCountryRuleByName(newCountry);
setCurrency(rule.currency);
}}
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500"
>
{COUNTRY_CODES.map((cc) => (
<option key={cc.code} value={cc.country}>
{cc.country}
</option>
))}
</select>
</div>
<button
type="submit"
disabled={profileSaving}
className="w-full py-2 bg-indigo-600 hover:bg-indigo-500 text-white font-bold text-xs rounded-xl transition-colors disabled:bg-slate-300 flex items-center justify-center gap-1.5 shadow shadow-indigo-600/10"
>
{profileSaving ? <RefreshCw className="h-3.5 w-3.5 animate-spin" /> : 'Save Profile Details'}
</button>
</form>
</div>
{/* Security & Password Section */}
<div className="space-y-3">
<div className="flex justify-between items-center">
<h4 className="text-xs font-bold text-slate-400 uppercase tracking-widest flex items-center gap-1.5">
<ShieldCheck className="h-4 w-4 text-indigo-500" /> Security & Password
</h4>
<button
type="button"
onClick={() => {
setShowPasswordChange(!showPasswordChange);
}}
className="text-xs text-indigo-600 hover:text-indigo-700 font-bold"
>
{showPasswordChange ? 'Hide Form' : 'Change Password'}
</button>
</div>
{showPasswordChange && (
<form
onSubmit={onChangePassword}
className="space-y-3.5 bg-slate-50 border border-slate-200/60 p-4 rounded-2xl animate-in fade-in duration-200"
>
{changePasswordSuccess && (
<div className="p-3 bg-emerald-50 border border-emerald-200 text-emerald-700 text-xs font-semibold rounded-xl flex items-center gap-2">
<CheckCircle className="h-4 w-4 flex-shrink-0" />
<span>Password updated successfully!</span>
</div>
)}
{changePasswordError && (
<div className="p-3 bg-rose-50 border border-rose-200 text-rose-700 text-xs font-semibold rounded-xl flex items-center gap-2">
<AlertCircle className="h-4 w-4 flex-shrink-0" />
<span>{changePasswordError}</span>
</div>
)}
<div className="space-y-1">
<label className="text-xs font-bold text-slate-500">Current Password</label>
<div className="relative">
<input
type={showOldPassword ? 'text' : 'password'}
required
value={changeOldPassword}
onChange={(e) => setChangeOldPassword(e.target.value)}
placeholder="Enter current password"
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 pr-10 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500"
/>
<button
type="button"
onClick={() => setShowOldPassword(!showOldPassword)}
className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600 p-0.5"
>
{showOldPassword ? <EyeOff className="h-3.5 w-3.5" /> : <Eye className="h-3.5 w-3.5" />}
</button>
</div>
</div>
<div className="space-y-1">
<label className="text-xs font-bold text-slate-500">New Password</label>
<div className="relative">
<input
type={showNewPassword ? 'text' : 'password'}
required
value={changeNewPassword}
onChange={(e) => setChangeNewPassword(e.target.value)}
placeholder="Min 8 chars, 1 uppercase, 1 digit, 1 special"
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 pr-10 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500"
/>
<button
type="button"
onClick={() => setShowNewPassword(!showNewPassword)}
className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600 p-0.5"
>
{showNewPassword ? <EyeOff className="h-3.5 w-3.5" /> : <Eye className="h-3.5 w-3.5" />}
</button>
</div>
</div>
<div className="space-y-1">
<label className="text-xs font-bold text-slate-500">Confirm New Password</label>
<input
type="password"
required
value={changeConfirmPassword}
onChange={(e) => setChangeConfirmPassword(e.target.value)}
placeholder="Repeat new password"
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500"
/>
</div>
<div className="flex flex-wrap gap-2 text-[10px] text-slate-500 pt-1">
<span className={changeNewPassword.length >= 8 ? 'text-emerald-600 font-bold' : ''}>
&bull; 8+ chars
</span>
<span className={/[A-Z]/.test(changeNewPassword) ? 'text-emerald-600 font-bold' : ''}>
&bull; 1 uppercase
</span>
<span className={/[0-9]/.test(changeNewPassword) ? 'text-emerald-600 font-bold' : ''}>
&bull; 1 number
</span>
<span className={/[^A-Za-z0-9\s]/.test(changeNewPassword) ? 'text-emerald-600 font-bold' : ''}>
&bull; 1 special char
</span>
</div>
<button
type="submit"
disabled={changePasswordLoading}
className="w-full py-2 bg-indigo-600 hover:bg-indigo-500 text-white font-bold text-xs rounded-xl transition-colors disabled:bg-slate-300 flex items-center justify-center gap-1.5 shadow shadow-indigo-600/10"
>
{changePasswordLoading ? <RefreshCw className="h-3.5 w-3.5 animate-spin" /> : 'Update Password'}
</button>
</form>
)}
</div>
{/* Subscriptions Nested Details */}
<div className="space-y-4">
<h4 className="text-xs font-bold text-slate-400 uppercase tracking-widest flex items-center gap-1.5">
<CheckCircle className="h-4 w-4 text-emerald-500" /> Ongoing subscriptions
</h4>
{!loggedInUser.active_subscriptions || loggedInUser.active_subscriptions.length === 0 ? (
<div className="bg-slate-50 border border-slate-200/60 rounded-2xl p-6 text-center space-y-2">
<p className="text-xs text-slate-500 font-semibold">No active subscriptions owned.</p>
<button
onClick={onBrowseStorefront}
className="text-xs text-indigo-600 font-bold hover:underline"
>
Browse storefront catalog
</button>
</div>
) : (
<div className="space-y-4">
{loggedInUser.active_subscriptions.map((sub: any) => {
const moduleMeta = curriculumModules.find((m) => m.code === sub.plan_code);
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="bg-slate-50 border border-slate-200 rounded-2xl p-4 space-y-3">
<div className="flex justify-between items-start gap-2">
<h5 className="text-xs font-bold text-slate-800 leading-tight">
{moduleMeta ? moduleMeta.name : sub.plan_code.replace(/_/g, ' ').toUpperCase()}
</h5>
<span
className={`text-[8px] font-black uppercase px-2 py-0.5 rounded flex-shrink-0 ${
sub.plan_code === 'LIFETIME_ALL'
? 'bg-amber-100 text-amber-800 border border-amber-200'
: 'bg-indigo-50 text-indigo-700 border border-indigo-200'
}`}
>
{sub.plan_code === 'LIFETIME_ALL' ? 'Lifetime' : '1 Year'}
</span>
</div>
<div className="text-[10px] text-slate-400 font-semibold space-y-1">
<div className="flex justify-between">
<span>Activated:</span>
<span className="text-slate-600 font-bold">
{sub.started_at
? new Date(sub.started_at).toLocaleDateString(undefined, { dateStyle: 'medium' })
: 'N/A'}
</span>
</div>
<div className="flex justify-between">
<span>Expires:</span>
<span className="text-slate-600 font-bold">
{hasExpiry
? new Date(sub.expires_at).toLocaleDateString(undefined, { dateStyle: 'medium' })
: 'Lifetime'}
</span>
</div>
</div>
<div className="pt-2 border-t border-slate-200/60 flex justify-between items-center">
<span className="text-[9px] text-slate-400 font-bold uppercase tracking-wider">Status:</span>
{!hasExpiry || sub.plan_code === 'LIFETIME_ALL' ? (
<span className="text-[9px] bg-amber-100 text-amber-800 border border-amber-200 font-black px-2 py-0.5 rounded-lg">
👑 Lifetime Pass
</span>
) : (
<span className="text-[9px] bg-emerald-100 text-emerald-800 border border-emerald-200 font-black px-2 py-0.5 rounded-lg">
{daysLeft > 0 ? `${daysLeft} Days Left` : 'Expired'}
</span>
)}
</div>
</div>
);
})}
</div>
)}
</div>
</div>
{/* Log Out at the bottom of the drawer */}
<div className="p-6 border-t border-slate-100 bg-slate-50 flex-shrink-0">
<button
onClick={onLogout}
className="w-full flex items-center justify-center gap-2 py-2.5 rounded-xl text-xs font-bold bg-slate-800 hover:bg-slate-700 text-slate-300 transition-colors duration-300"
>
<LogOut className="h-4 w-4" /> Log Out Session
</button>
</div>
</div>
</div>
);
};