2026-10-07 19:40:26 +03:30
|
|
|
"use client";
|
|
|
|
|
|
2026-10-18 12:09:30 +03:30
|
|
|
import { useState } from "react";
|
2026-10-07 19:40:26 +03:30
|
|
|
import { useRouter } from "next/navigation";
|
2026-10-18 12:09:30 +03:30
|
|
|
import { useQuery } from "@tanstack/react-query";
|
2026-10-18 10:14:22 +03:30
|
|
|
import { Award, Clock, Copy, ExternalLink, Loader2, Lock } from "lucide-react";
|
2026-10-07 19:40:26 +03:30
|
|
|
import { http, toUserError } from "@/lib/api";
|
2026-10-18 10:29:58 +03:30
|
|
|
import type { Certificate } from "@/lib/types";
|
2026-10-07 19:40:26 +03:30
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { toast } from "@/components/providers";
|
2026-10-18 10:14:22 +03:30
|
|
|
import { cn } from "@/lib/utils";
|
2026-10-07 19:40:26 +03:30
|
|
|
|
|
|
|
|
type MineCertificateResponse = {
|
|
|
|
|
certificate: Certificate | null;
|
|
|
|
|
eligible: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function ProfileCertificateSlot() {
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
|
|
const { data, isLoading, isError, error, refetch } = useQuery({
|
|
|
|
|
queryKey: ["me", "certificate"],
|
|
|
|
|
queryFn: () => http.get<MineCertificateResponse>("/api/me/certificate"),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const cert = data?.certificate ?? null;
|
|
|
|
|
const eligible = !!data?.eligible;
|
|
|
|
|
|
|
|
|
|
const copyLink = async () => {
|
|
|
|
|
if (!cert?.publicId) return;
|
|
|
|
|
const url = `${window.location.origin}/c/${cert.publicId}`;
|
|
|
|
|
try {
|
|
|
|
|
await navigator.clipboard.writeText(url);
|
|
|
|
|
toast.success("لینک گواهینامه کپی شد");
|
|
|
|
|
} catch {
|
|
|
|
|
toast.error("کپی ممکن نشد");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const openCert = () => {
|
|
|
|
|
if (!cert?.publicId) return;
|
|
|
|
|
router.push(`/c/${cert.publicId}`);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<section
|
|
|
|
|
aria-label="جایگاه گواهینامه"
|
|
|
|
|
className="rounded-xl border border-dashed border-border bg-muted/20 p-3"
|
|
|
|
|
>
|
|
|
|
|
<div className="mb-2 flex items-center gap-2">
|
|
|
|
|
<span className="grid h-8 w-8 place-items-center rounded-lg bg-primary/10 text-primary">
|
|
|
|
|
<Award className="h-4 w-4" />
|
|
|
|
|
</span>
|
|
|
|
|
<div className="min-w-0 flex-1">
|
|
|
|
|
<p className="text-xs font-extrabold">گواهینامه پایان دوره</p>
|
|
|
|
|
<p className="text-[10px] text-muted-foreground">جایگاه ثابت گواهی شما در پروفایل</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{isError ? (
|
|
|
|
|
<div className="space-y-2 py-2 text-xs">
|
|
|
|
|
<p className="font-medium text-destructive">{toUserError(error)}</p>
|
|
|
|
|
<Button type="button" size="sm" variant="outline" className="h-8" onClick={() => refetch()}>
|
|
|
|
|
تلاش دوباره
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2026-10-18 12:09:30 +03:30
|
|
|
) : isLoading ? (
|
2026-10-07 19:40:26 +03:30
|
|
|
<div className="flex items-center gap-2 py-3 text-xs text-muted-foreground">
|
|
|
|
|
<Loader2 className="h-3.5 w-3.5 animate-spin" />
|
|
|
|
|
در حال بارگذاری…
|
|
|
|
|
</div>
|
2026-10-18 10:14:22 +03:30
|
|
|
) : cert?.status === "pending" ? (
|
|
|
|
|
<div className="flex items-start gap-2 rounded-lg border border-border/70 bg-background/60 px-3 py-2.5">
|
|
|
|
|
<Clock className="mt-0.5 h-3.5 w-3.5 shrink-0 text-warning" />
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-[11px] font-bold text-foreground">در انتظار تأیید</p>
|
|
|
|
|
<p className="mt-0.5 text-[10px] leading-relaxed text-muted-foreground">
|
|
|
|
|
درخواست گواهینامه ثبت شد و پس از بررسی توسط تیم آموزش صادر میشود.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
) : cert?.status === "issued" && cert.publicId ? (
|
2026-10-07 19:40:26 +03:30
|
|
|
<div className="space-y-2.5">
|
|
|
|
|
<div className="rounded-lg border border-primary/25 bg-primary/5 px-3 py-2.5">
|
|
|
|
|
<p className="text-sm font-bold text-foreground">{cert.fullName}</p>
|
|
|
|
|
<p className="mt-0.5 text-[11px] text-muted-foreground">
|
|
|
|
|
صادر شده در {new Date(cert.issuedAt).toLocaleDateString("fa-IR")}
|
|
|
|
|
</p>
|
|
|
|
|
<p className="mt-1 truncate font-mono text-[10px] text-muted-foreground" dir="ltr">
|
|
|
|
|
{cert.publicId}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex flex-wrap gap-1.5">
|
|
|
|
|
<Button type="button" size="sm" className="h-8 flex-1" onClick={openCert}>
|
|
|
|
|
<ExternalLink className="h-3.5 w-3.5" />
|
|
|
|
|
مشاهده
|
|
|
|
|
</Button>
|
|
|
|
|
<Button type="button" size="sm" variant="outline" className="h-8" onClick={() => void copyLink()}>
|
|
|
|
|
<Copy className="h-3.5 w-3.5" />
|
|
|
|
|
کپی لینک
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
) : eligible ? (
|
2026-10-18 12:09:30 +03:30
|
|
|
<div className="flex items-start gap-2 rounded-lg border border-border/70 bg-background/60 px-3 py-2.5">
|
|
|
|
|
<Clock className="mt-0.5 h-3.5 w-3.5 shrink-0 text-warning" />
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-[11px] font-bold text-foreground">در انتظار تأیید</p>
|
|
|
|
|
<p className="mt-0.5 text-[10px] leading-relaxed text-muted-foreground">
|
|
|
|
|
درخواست گواهینامه ثبت شد و پس از بررسی توسط تیم آموزش صادر میشود.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
2026-10-07 19:40:26 +03:30
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"flex items-start gap-2 rounded-lg border border-border/70 bg-background/60 px-3 py-2.5",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<Lock className="mt-0.5 h-3.5 w-3.5 shrink-0 text-muted-foreground" />
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-[11px] font-bold text-foreground">هنوز صادر نشده</p>
|
|
|
|
|
<p className="mt-0.5 text-[10px] leading-relaxed text-muted-foreground">
|
2026-10-18 10:14:22 +03:30
|
|
|
با پاس کردن همه درسهای فعال، گواهینامه همینجا نمایش داده میشود.
|
2026-10-07 19:40:26 +03:30
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</section>
|
|
|
|
|
);
|
|
|
|
|
}
|