Persian age validation messages, clearer homework entry points, and seed assignment definitions so «تکالیف من» is not empty after deploy. Co-authored-by: Cursor <cursoragent@cursor.com>
200 lines
8.2 KiB
TypeScript
200 lines
8.2 KiB
TypeScript
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
import { Link, Navigate, useNavigate } from 'react-router-dom';
|
|
import { useAuth } from '@/hooks/useAuth';
|
|
import {
|
|
assignmentStatusLabel,
|
|
assignmentTypeLabel,
|
|
ASSIGNMENTS_REFRESH_EVENT,
|
|
fetchAssignmentCatalog,
|
|
type AssignmentCatalogItem,
|
|
} from '@/services/assignmentService';
|
|
import { getBackendErrorMessage } from '@/lib/backendError';
|
|
import { AnimationWrapper, StaggerItem, StaggeredList } from '@/components/motion';
|
|
import { PageSkeleton } from '@/components/skeletons';
|
|
import { Button } from '@/components/ui/button';
|
|
import { cn } from '@/lib/utils';
|
|
import { Check, ChevronLeft, Lock, NotebookPen } from 'lucide-react';
|
|
|
|
function formatActivity(iso: string | null | undefined): string {
|
|
if (!iso) return '—';
|
|
try {
|
|
return new Date(iso).toLocaleDateString('fa-IR');
|
|
} catch {
|
|
return '—';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* «تکالیف من» — ۹ تکلیف جلسهای؛ با زدن هر کدام به صفحه همان تمرین میروی.
|
|
*/
|
|
export default function AssignmentsPage() {
|
|
const { effectiveUserId, guestSessionHydrated, isGuest } = useAuth();
|
|
const navigate = useNavigate();
|
|
const [items, setItems] = useState<AssignmentCatalogItem[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const load = useCallback(async () => {
|
|
setLoading(true);
|
|
setError(null);
|
|
try {
|
|
const rows = await fetchAssignmentCatalog();
|
|
setItems(rows);
|
|
} catch (err) {
|
|
setError(getBackendErrorMessage(err, 'بارگذاری تکالیف ناموفق بود'));
|
|
setItems([]);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (!effectiveUserId || isGuest) return;
|
|
void load();
|
|
}, [effectiveUserId, isGuest, load]);
|
|
|
|
useEffect(() => {
|
|
if (!effectiveUserId || isGuest) return;
|
|
const onRefresh = () => {
|
|
void load();
|
|
};
|
|
window.addEventListener(ASSIGNMENTS_REFRESH_EVENT, onRefresh);
|
|
return () => window.removeEventListener(ASSIGNMENTS_REFRESH_EVENT, onRefresh);
|
|
}, [effectiveUserId, isGuest, load]);
|
|
|
|
const unlockedCount = useMemo(() => items.filter((i) => i.unlocked).length, [items]);
|
|
|
|
if (!guestSessionHydrated) {
|
|
return <PageSkeleton />;
|
|
}
|
|
|
|
if (!effectiveUserId) {
|
|
return <Navigate to="/login" replace />;
|
|
}
|
|
|
|
if (isGuest) {
|
|
return (
|
|
<div className="flex h-full flex-1 flex-col items-center justify-center px-4 text-center">
|
|
<p className="mb-3 text-sm text-muted-foreground">
|
|
برای ذخیره و پیگیری تکالیف باید وارد حساب کاربری شوید.
|
|
</p>
|
|
<Button onClick={() => navigate('/login')}>ورود</Button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="h-full min-h-0 flex-1 overflow-x-hidden overflow-y-auto bg-slate-50 px-4 py-6 transition-colors duration-300 dark:bg-slate-900">
|
|
<div className="mx-auto w-full max-w-md space-y-4">
|
|
<AnimationWrapper immediate className="space-y-1 pb-1">
|
|
<div className="flex items-center gap-2">
|
|
<NotebookPen className="h-5 w-5 text-sky-600 dark:text-sky-400" />
|
|
<h1 className="text-2xl font-bold tracking-tight text-foreground">تکالیف من</h1>
|
|
</div>
|
|
<p className="text-sm leading-6 text-body">
|
|
برای هر جلسه یک بخش جداگانه هست. روی تکلیف بزن تا دستورالعمل را ببینی و فیلدهای گزارش
|
|
را پر کنی.
|
|
</p>
|
|
<p className="text-xs text-muted-foreground">
|
|
{unlockedCount} از {items.length || 9} تکلیف باز شده
|
|
</p>
|
|
</AnimationWrapper>
|
|
|
|
{loading ? (
|
|
<PageSkeleton />
|
|
) : error ? (
|
|
<div className="rounded-xl border border-red-200 bg-red-50 p-4 text-sm text-red-700 dark:border-red-900 dark:bg-red-950/40 dark:text-red-300">
|
|
{error}
|
|
<Button variant="link" className="mt-1 px-0" onClick={() => void load()}>
|
|
تلاش مجدد
|
|
</Button>
|
|
</div>
|
|
) : items.length === 0 ? (
|
|
<div className="rounded-xl border border-dashed border-slate-300 p-6 text-center text-sm text-muted-foreground dark:border-slate-600">
|
|
<p>فهرست تکالیف هنوز از سرور بارگذاری نشده است.</p>
|
|
<p className="mt-2 text-xs">
|
|
پس از تکمیل هر یک از جلسات ۲ تا ۱۰، تکلیف همان جلسه اینجا باز میشود.
|
|
</p>
|
|
<Button variant="link" className="mt-2 px-0" onClick={() => void load()}>
|
|
تلاش مجدد
|
|
</Button>
|
|
</div>
|
|
) : (
|
|
<StaggeredList itemCount={items.length} className="space-y-3">
|
|
{items.map((item) => {
|
|
const ua = item.userAssignment;
|
|
const isOpen = item.unlocked && Boolean(ua);
|
|
const completed = ua?.status === 'COMPLETED';
|
|
|
|
const content = (
|
|
<>
|
|
<div className="mb-2 flex items-start justify-between gap-2">
|
|
<div className="min-w-0 space-y-1">
|
|
<p className="text-[11px] font-medium text-sky-700 dark:text-sky-300">
|
|
جلسه {item.definition.sessionOrder}
|
|
</p>
|
|
<h2 className="text-base font-semibold text-foreground">
|
|
{item.definition.title}
|
|
</h2>
|
|
</div>
|
|
{isOpen ? (
|
|
<ChevronLeft className="mt-1 h-4 w-4 shrink-0 text-muted-foreground" />
|
|
) : (
|
|
<Lock className="mt-1 h-4 w-4 shrink-0 text-muted-foreground" />
|
|
)}
|
|
</div>
|
|
<p className="mb-3 line-clamp-2 text-xs leading-5 text-muted-foreground">
|
|
{item.definition.instructions}
|
|
</p>
|
|
<div className="flex flex-wrap gap-2 text-[11px] text-muted-foreground">
|
|
<span className="rounded-md bg-slate-100 px-2 py-0.5 dark:bg-slate-700">
|
|
{assignmentTypeLabel(item.definition.assignmentType)}
|
|
</span>
|
|
{isOpen && ua ? (
|
|
<>
|
|
<span
|
|
className={cn(
|
|
'inline-flex items-center gap-1 rounded-md px-2 py-0.5',
|
|
completed
|
|
? 'bg-emerald-50 text-emerald-800 dark:bg-emerald-950 dark:text-emerald-200'
|
|
: 'bg-sky-50 text-sky-800 dark:bg-sky-950 dark:text-sky-200',
|
|
)}
|
|
>
|
|
{completed ? <Check className="h-3 w-3" /> : null}
|
|
{assignmentStatusLabel(ua.status)}
|
|
</span>
|
|
<span>آخرین فعالیت: {formatActivity(ua.lastActivityAt)}</span>
|
|
</>
|
|
) : (
|
|
<span className="rounded-md bg-slate-100 px-2 py-0.5 dark:bg-slate-700">
|
|
{item.lockReason ?? 'پس از پایان جلسه مربوطه باز میشود'}
|
|
</span>
|
|
)}
|
|
</div>
|
|
</>
|
|
);
|
|
|
|
return (
|
|
<StaggerItem key={item.definition.id}>
|
|
{isOpen && ua ? (
|
|
<Link
|
|
to={`/assignments/${ua.id}`}
|
|
className="block rounded-xl border border-slate-200/80 bg-white p-4 shadow-sm transition hover:border-sky-300 dark:border-slate-700 dark:bg-slate-800 dark:hover:border-sky-600"
|
|
>
|
|
{content}
|
|
</Link>
|
|
) : (
|
|
<div className="rounded-xl border border-dashed border-slate-200 bg-slate-50/80 p-4 opacity-80 dark:border-slate-700 dark:bg-slate-800/50">
|
|
{content}
|
|
</div>
|
|
)}
|
|
</StaggerItem>
|
|
);
|
|
})}
|
|
</StaggeredList>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|