Spaces:
Sleeping
Sleeping
File size: 26,275 Bytes
eb6a2f9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 | // src/components/admin/UserProfileView.tsx
import { useState, useEffect } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import { useLanguage } from '../../lib/languageContext';
import { translations } from '../../lib/translations';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '../ui/card';
import { Button } from '../ui/button';
import { Badge } from '../ui/badge';
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from '../ui/alert-dialog';
import { Progress } from '../ui/progress';
import { Separator } from '../ui/separator';
import { toast } from 'sonner';
import {
ArrowLeft,
Mail,
Calendar,
MapPin,
Award,
Clock,
DollarSign,
Star,
Users,
Ban,
Trash2,
CheckCircle,
AlertTriangle,
Video,
BookOpen,
Target,
Loader2,
} from 'lucide-react';
// Import API
import adminUserManagementApi, {
type StudentDtoAdmin,
type SheikhDtoAdmin,
type UserManagementApiError,
type StudentProfileResponse,
type SheikhProfileResponse,
type SessionHistoryDto,
} from '../../lib/api/adminUserManagementApi';
// ─── Types & Interfaces ──────────────────────────────────────────────────────
type UserType = 'student' | 'sheikh' | 'admin';
interface DisplayUser {
id: number;
name: string;
email: string;
registrationDate: string;
type: UserType;
status: string;
// Student fields
streak?: number | null;
totalSessions?: number | null;
// Sheikh fields
numberOfSessions?: number | null;
totalRevenue?: number | null;
averageRating?: number | null;
}
interface StudentProfileData {
profile: StudentProfileResponse['studentProfile'];
sessions: SessionHistoryDto[];
}
interface SheikhProfileData {
profile: SheikhProfileResponse['sheikhProfile'];
sessions: SessionHistoryDto[];
}
// ─── Helpers ──────────────────────────────────────────────────────────────────
function formatDate(dateString: string, locale: string): string {
const d = new Date(dateString);
return d.toLocaleDateString(locale === 'ar' ? 'ar-EG' : 'en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
});
}
function formatNumber(value: number | null | undefined, locale: string): string {
if (value === null || value === undefined) return '—';
return value.toLocaleString(locale === 'ar' ? 'ar-EG' : 'en-US');
}
function formatCurrency(value: number | null | undefined, locale: string): string {
if (value === null || value === undefined) return '—';
return `${value.toLocaleString(locale === 'ar' ? 'ar-EG' : 'en-US')} ${locale === 'ar' ? 'ج.م' : 'EGP'}`;
}
function formatRating(value: number | null | undefined): string {
if (value === null || value === undefined) return '—';
return value.toFixed(1);
}
function formatDuration(minutes: number): string {
const hours = Math.floor(minutes / 60);
const mins = minutes % 60;
if (hours === 0) return `${mins} ${mins === 1 ? 'min' : 'mins'}`;
if (mins === 0) return `${hours} ${hours === 1 ? 'hr' : 'hrs'}`;
return `${hours}h ${mins}m`;
}
// ─── Main Component ───────────────────────────────────────────────────────────
export function UserProfileView() {
const { userId } = useParams<{ userId: string }>();
const navigate = useNavigate();
const { language } = useLanguage();
const t = translations[language];
const isArabic = language === 'ar';
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [userType, setUserType] = useState<UserType | null>(null);
const [studentData, setStudentData] = useState<StudentProfileData | null>(null);
const [sheikhData, setSheikhData] = useState<SheikhProfileData | null>(null);
const [actionLoading, setActionLoading] = useState(false);
// ── Load user profile ─────────────────────────────────────────────────────
useEffect(() => {
loadUserProfile();
}, [userId]);
const loadUserProfile = async () => {
if (!userId) {
setError('User ID not provided');
setLoading(false);
return;
}
setLoading(true);
setError(null);
try {
// Try to load as student first
try {
const studentProfile = await adminUserManagementApi.fetchStudentProfile(parseInt(userId));
setUserType('student');
setStudentData({
profile: studentProfile.studentProfile,
sessions: studentProfile.sessionHistories,
});
setLoading(false);
return;
} catch (err) {
// Not a student, continue to try sheikh
}
// Try to load as sheikh
try {
const sheikhProfile = await adminUserManagementApi.fetchSheikhProfile(parseInt(userId));
setUserType('sheikh');
setSheikhData({
profile: sheikhProfile.sheikhProfile,
sessions: sheikhProfile.sessionHistories,
});
setLoading(false);
return;
} catch (err) {
// Not a sheikh either
if (err instanceof UserManagementApiError) {
if (err.status === 404) {
setError(isArabic ? 'المستخدم غير موجود' : 'User not found');
} else if (err.status === 403) {
setError(isArabic ? 'صلاحية المسؤول مطلوبة' : 'Admin access required');
} else {
setError(err.message);
}
} else {
setError(isArabic ? 'فشل تحميل بيانات المستخدم' : 'Failed to load user data');
}
}
} finally {
setLoading(false);
}
};
// ── Actions ───────────────────────────────────────────────────────────────
const handleBlock = async () => {
if (!userId) return;
setActionLoading(true);
try {
await adminUserManagementApi.blockUser(parseInt(userId));
toast.success(
isArabic
? 'تم حظر المستخدم بنجاح'
: 'User has been blocked'
);
// Refresh data
await loadUserProfile();
} catch (err) {
console.error('Block failed:', err);
toast.error(
isArabic
? 'فشل حظر المستخدم'
: 'Failed to block user'
);
} finally {
setActionLoading(false);
}
};
const handleUnblock = async () => {
if (!userId) return;
setActionLoading(true);
try {
await adminUserManagementApi.unblockUser(parseInt(userId));
toast.success(
isArabic
? 'تم إلغاء حظر المستخدم بنجاح'
: 'User has been unblocked'
);
// Refresh data
await loadUserProfile();
} catch (err) {
console.error('Unblock failed:', err);
toast.error(
isArabic
? 'فشل إلغاء حظر المستخدم'
: 'Failed to unblock user'
);
} finally {
setActionLoading(false);
}
};
const handleDelete = async () => {
if (!userId) return;
setActionLoading(true);
try {
// Note: Delete API not available yet
toast.error(
isArabic
? 'حذف المستخدمين غير متاح حالياً'
: 'Delete user not available yet'
);
} catch (err) {
console.error('Delete failed:', err);
} finally {
setActionLoading(false);
}
};
const handleBack = () => {
navigate('/admin/users');
};
// ── Loading State ─────────────────────────────────────────────────────────
if (loading) {
return (
<div className="container mx-auto py-16 px-4 flex justify-center items-center">
<div className="text-center space-y-4">
<Loader2 className="h-12 w-12 animate-spin text-emerald-600 mx-auto" />
<p className="text-muted-foreground">
{isArabic ? 'جاري تحميل الملف الشخصي...' : 'Loading profile...'}
</p>
</div>
</div>
);
}
// ── Error State ───────────────────────────────────────────────────────────
if (error || !userType) {
return (
<div className="container mx-auto py-16 px-4">
<Card className="max-w-md mx-auto">
<CardHeader>
<CardTitle className="text-center text-destructive">
<AlertTriangle className="h-12 w-12 mx-auto mb-4 text-destructive" />
{isArabic ? 'خطأ' : 'Error'}
</CardTitle>
<CardDescription className="text-center">
{error || (isArabic ? 'المستخدم غير موجود' : 'User not found')}
</CardDescription>
</CardHeader>
<CardContent className="text-center">
<Button onClick={handleBack}>
<ArrowLeft className={`h-4 w-4 ${isArabic ? 'ml-2' : 'mr-2'}`} />
{isArabic ? 'العودة' : 'Go Back'}
</Button>
</CardContent>
</Card>
</div>
);
}
const isStudent = userType === 'student';
const isSheikh = userType === 'sheikh';
const currentStatus = isStudent ? studentData?.profile.status : sheikhData?.profile.status;
const isBlocked = currentStatus === 'BLOCKED';
const student = studentData?.profile;
const sheikh = sheikhData?.profile;
const sessions = studentData?.sessions || sheikhData?.sessions || [];
return (
<div dir={isArabic ? 'rtl' : 'ltr'} className="container mx-auto py-8 px-4 max-w-7xl space-y-8">
{/* Header + Actions */}
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-6">
<div className="flex items-center gap-4">
<Button variant="outline" size="sm" onClick={handleBack}>
<ArrowLeft className={`h-4 w-4 ${isArabic ? 'ml-2' : 'mr-2'}`} />
{isArabic ? 'العودة' : 'Back'}
</Button>
<div>
<h1 className="text-2xl font-bold tracking-tight">
{isArabic ? 'الملف الشخصي' : 'User Profile'}
</h1>
<p className="text-muted-foreground mt-1">
{isStudent ? student?.name : sheikh?.name}
</p>
</div>
</div>
<div className="flex flex-wrap gap-3">
<AlertDialog>
<AlertDialogTrigger asChild>
<Button variant="outline" size="sm" disabled={actionLoading}>
<Ban className={`h-4 w-4 ${isArabic ? 'ml-2' : 'mr-2'}`} />
{isBlocked
? (isArabic ? 'إلغاء الحظر' : 'Unblock')
: (isArabic ? 'حظر' : 'Block')}
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
{isBlocked
? (isArabic ? 'إلغاء حظر المستخدم؟' : 'Unblock User?')
: (isArabic ? 'حظر المستخدم؟' : 'Block User?')}
</AlertDialogTitle>
<AlertDialogDescription>
{isBlocked
? (isArabic
? 'سيُعاد للمستخدم الوصول إلى المنصة.'
: "This will restore the user's access to the platform.")
: (isArabic
? 'سيمنع هذا الإجراء المستخدم من الوصول إلى المنصة. يمكن إلغاء الحظر لاحقًا.'
: 'This will prevent the user from accessing the platform. They can be unblocked later.')}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{isArabic ? 'إلغاء' : 'Cancel'}</AlertDialogCancel>
<AlertDialogAction onClick={isBlocked ? handleUnblock : handleBlock}>
{isBlocked
? (isArabic ? 'إلغاء الحظر' : 'Unblock')
: (isArabic ? 'حظر' : 'Block')}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<AlertDialog>
<AlertDialogTrigger asChild>
<Button variant="destructive" size="sm" disabled={actionLoading}>
<Trash2 className={`h-4 w-4 ${isArabic ? 'ml-2' : 'mr-2'}`} />
{isArabic ? 'حذف' : 'Delete'}
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
{isArabic ? 'حذف المستخدم نهائيًا؟' : 'Delete User Permanently?'}
</AlertDialogTitle>
<AlertDialogDescription>
{isArabic
? 'هذا الإجراء لا يمكن التراجع عنه. سيتم حذف الحساب وجميع البيانات المرتبطة به نهائيًا.'
: 'This action cannot be undone. This will permanently delete the user account and all associated data.'}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{isArabic ? 'إلغاء' : 'Cancel'}</AlertDialogCancel>
<AlertDialogAction
onClick={handleDelete}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
{isArabic ? 'حذف نهائي' : 'Delete Permanently'}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
</div>
{/* Main User Card */}
<Card className="border shadow-sm">
<CardContent className="pt-6">
<div className="flex flex-col sm:flex-row gap-6">
{/* Avatar */}
<div className="shrink-0">
<div className="w-24 h-24 rounded-full bg-gradient-to-br from-emerald-500 to-teal-600 flex items-center justify-center text-white text-4xl font-bold shadow-md">
{(isStudent ? student?.name : sheikh?.name)?.charAt(0).toUpperCase() || '?'}
</div>
</div>
{/* Info */}
<div className="flex-1 space-y-4">
<div className="flex flex-wrap items-center gap-3">
<h2 className="text-2xl font-bold">{isStudent ? student?.name : sheikh?.name}</h2>
<Badge variant={isStudent ? 'default' : isSheikh ? 'secondary' : 'destructive'} className="px-3 py-1">
{userType.toUpperCase()}
</Badge>
<Badge
variant={!isBlocked ? 'outline' : 'destructive'}
className={!isBlocked ? 'text-emerald-600 border-emerald-600' : ''}
>
{!isBlocked ? (
<>
<CheckCircle className="h-3 w-3 mr-1" />
{isArabic ? 'نشط' : 'Active'}
</>
) : (
<>
<Ban className="h-3 w-3 mr-1" />
{isArabic ? 'محظور' : 'Blocked'}
</>
)}
</Badge>
</div>
<div className="grid gap-2 text-sm text-muted-foreground">
<div className="flex items-center gap-2">
<Mail className="h-4 w-4" />
{isStudent ? student?.email : sheikh?.email}
</div>
{isSheikh && sheikh?.country && (
<div className="flex items-center gap-2">
<MapPin className="h-4 w-4" />
{sheikh.country}
</div>
)}
<div className="flex items-center gap-2">
<Calendar className="h-4 w-4" />
{isArabic ? 'انضم في ' : 'Joined '}
{formatDate(
isStudent ? student?.registrationDate || '' : sheikh?.registrationDate || '',
language
)}
</div>
{isSheikh && sheikh?.bio && (
<div className="mt-2 text-sm border-t pt-2">
<p className="text-muted-foreground">{sheikh.bio}</p>
</div>
)}
</div>
</div>
{/* Right side stats */}
{isSheikh && sheikh && (
<div className="text-right space-y-2 min-w-[180px]">
<div className="text-3xl font-bold text-emerald-600">
{formatCurrency(sheikh.totalEarnings, language)}
</div>
<div className="text-sm text-muted-foreground">{isArabic ? 'الأرباح' : 'Earnings'}</div>
<div className="flex items-center justify-end gap-1 text-yellow-600">
<Star className="h-5 w-5 fill-yellow-500 text-yellow-500" />
<span className="font-bold text-xl">{formatRating(sheikh.averageRating)}</span>
</div>
</div>
)}
{isStudent && student && (
<div className="text-right space-y-2 min-w-[180px]">
<div className="text-3xl font-bold">{formatNumber(student.sessionCount, language)}</div>
<div className="text-sm text-muted-foreground">{isArabic ? 'الجلسات' : 'Sessions'}</div>
<div className="text-2xl font-bold text-orange-600">
{student.streak ?? 0} <span className="text-xl">🔥</span>
</div>
</div>
)}
</div>
</CardContent>
</Card>
{/* Statistics Cards Grid */}
{isStudent && student && (
<div className="grid gap-5 sm:grid-cols-2 lg:grid-cols-4">
<Card className="hover:shadow-md transition-shadow">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-medium">{isArabic ? 'إجمالي الجلسات' : 'Total Sessions'}</CardTitle>
<BookOpen className="w-4 h-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{formatNumber(student.sessionCount, language)}</div>
</CardContent>
</Card>
<Card className="hover:shadow-md transition-shadow">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-medium">{isArabic ? 'الجلسات المكتملة' : 'Completed Sessions'}</CardTitle>
<CheckCircle className="w-4 h-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{formatNumber(student.numberOfCompletedSessions, language)}</div>
</CardContent>
</Card>
<Card className="hover:shadow-md transition-shadow">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-medium">{isArabic ? 'وقت التعلم' : 'Learning Time'}</CardTitle>
<Clock className="w-4 h-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{formatDuration(student.totalSpentTime)}</div>
</CardContent>
</Card>
<Card className="hover:shadow-md transition-shadow">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-medium">{isArabic ? 'السلسلة' : 'Streak'}</CardTitle>
<Target className="w-4 h-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold text-orange-600">
{student.streak ?? 0} <span className="text-xl">🔥</span>
</div>
</CardContent>
</Card>
</div>
)}
{isSheikh && sheikh && (
<div className="grid gap-5 sm:grid-cols-2 lg:grid-cols-4">
<Card className="hover:shadow-md transition-shadow">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-medium">{isArabic ? 'إجمالي الجلسات' : 'Total Sessions'}</CardTitle>
<Video className="w-4 h-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{formatNumber(sheikh.numberOfCompletedSessions, language)}</div>
</CardContent>
</Card>
<Card className="hover:shadow-md transition-shadow">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-medium">{isArabic ? 'عدد الطلاب' : 'Students'}</CardTitle>
<Users className="w-4 h-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{formatNumber(sheikh.numberOfStudents, language)}</div>
</CardContent>
</Card>
<Card className="hover:shadow-md transition-shadow">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-medium">{isArabic ? 'التقييم' : 'Rating'}</CardTitle>
<Star className="w-4 h-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold text-yellow-600">{formatRating(sheikh.averageRating)}</div>
</CardContent>
</Card>
<Card className="hover:shadow-md transition-shadow">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-medium">{isArabic ? 'سعر الساعة' : 'Hourly Rate'}</CardTitle>
<DollarSign className="w-4 h-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{formatCurrency(sheikh.hourlyRate, language)}</div>
</CardContent>
</Card>
</div>
)}
{/* Session History */}
<Card>
<CardHeader>
<CardTitle>{isArabic ? 'سجل الجلسات' : 'Session History'}</CardTitle>
<CardDescription>
{isArabic ? 'آخر الجلسات والأنشطة' : 'Recent sessions and activities'}
</CardDescription>
</CardHeader>
<CardContent>
{sessions.length === 0 ? (
<div className="text-center py-8 text-muted-foreground">
{isArabic ? 'لا توجد جلسات سابقة' : 'No session history'}
</div>
) : (
<div className="space-y-4">
{sessions.map((session) => (
<div
key={session.sessionId}
className="flex items-center justify-between p-4 border rounded-lg hover:bg-muted/50 transition-colors"
>
<div className="flex items-center gap-4">
<div
className={`w-10 h-10 rounded-full flex items-center justify-center ${
session.status === 'COMPLETED' ? 'bg-emerald-100' : 'bg-red-100'
}`}
>
<Video
className={`h-5 w-5 ${
session.status === 'COMPLETED' ? 'text-emerald-600' : 'text-red-600'
}`}
/>
</div>
<div>
<p className="font-medium">
{isStudent ? session.sheikhName : session.studentName}
</p>
<p className="text-sm text-muted-foreground mt-0.5">
{formatDate(session.date, language)}
{session.durationInMinutes && (
<span className="ml-2">· {formatDuration(session.durationInMinutes)}</span>
)}
</p>
</div>
</div>
<div className="text-right">
<p className="font-semibold">{formatCurrency(session.price, language)}</p>
<Badge
variant={session.status === 'COMPLETED' ? 'default' : 'destructive'}
className="mt-1"
>
{session.status === 'COMPLETED'
? (isArabic ? 'مكتملة' : 'Completed')
: session.status === 'CANCELLED'
? (isArabic ? 'ملغية' : 'Cancelled')
: (isArabic ? 'معلقة' : 'Pending')}
</Badge>
</div>
</div>
))}
</div>
)}
</CardContent>
</Card>
</div>
);
} |