This repository has been archived on 2026-11-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
hope-events/frontend/src/components/events/MyCertificates.tsx
T

61 lines
2.5 KiB
TypeScript

"use client";
import React, { useEffect, useState } from "react";
import { Award, Download } from "lucide-react";
import { API_BASE } from "@/lib/api";
import { fetchApiFile, saveBlob } from "@/lib/export";
import { formatDate } from "@/lib/date";
import { errorMessage } from "@/lib/utils";
import type { MyCertificate } from "@/types";
import { apiV2Data } from "@/lib/apiV2";
// Completion certificates on My Events. Hidden until the person has one.
export function MyCertificates({ token }: { token: string | null }) {
const [certs, setCerts] = useState<MyCertificate[]>([]);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
if (!token) return;
(apiV2Data("get", "/me/certificates", { authToken: token }) as Promise<MyCertificate[]>)
.then(list => setCerts(Array.isArray(list) ? list : []))
.catch(() => setCerts([]));
}, [token]);
if (certs.length === 0) return null;
const download = async (c: MyCertificate) => {
if (!token) return;
setError(null);
try {
saveBlob(await fetchApiFile(API_BASE, token, `/api/v2/certificates/${c.id}/pdf`), `Certificate - ${c.event.title.replace(/[^a-z0-9 -]/gi, "")}.pdf`);
} catch (e) {
setError(errorMessage(e) || "Download failed");
}
};
return (
<section className="mb-6 border rounded-xl p-5 bg-white shadow-sm">
<div className="flex items-center gap-2 mb-3">
<Award className="w-5 h-5 text-brand-600" />
<h2 className="text-lg font-semibold text-gray-900">My Certificates</h2>
</div>
{error && <div className="mb-3 p-2 border rounded bg-red-50 text-red-700 text-sm">{error}</div>}
<ul className="divide-y">
{certs.map(c => (
<li key={c.id} className="flex flex-wrap items-center justify-between gap-2 py-2.5">
<div className="min-w-0">
<div className="font-medium text-gray-900">{c.event.title}</div>
<div className="text-xs text-gray-500">
{c.name} · {c.totalSessions > 1 ? `${c.sessionsAttended} of ${c.totalSessions} sessions · ` : ""}issued {formatDate(new Date(c.issuedAt))}
</div>
</div>
<button type="button" onClick={() => download(c)} className="inline-flex items-center gap-1.5 px-3 py-1.5 text-sm rounded-lg border border-brand-200 bg-brand-50 text-brand-700 hover:bg-brand-100">
<Download className="w-4 h-4" /> Download
</button>
</li>
))}
</ul>
</section>
);
}