feat: refactor file URL resolution and enhance component imports
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import jsPDF from "jspdf";
|
||||
import JSZip from "jszip";
|
||||
import type { Certificate } from "../../types/certificate.types";
|
||||
import { generateCertificate, resolveImageUrl } from "./SertificateGenerator";
|
||||
import { generateCertificate } from "./SertificateGenerator";
|
||||
import { resolveFileUrl } from "./resolveFileFromUrl";
|
||||
|
||||
function formatDateIndo(date: string): string {
|
||||
return new Intl.DateTimeFormat("id-ID", {
|
||||
@@ -14,7 +15,7 @@ function formatDateIndo(date: string): string {
|
||||
function formatDateRange(startDate: string, endDate: string): string {
|
||||
return `${formatDateIndo(startDate)} - ${formatDateIndo(endDate)}`;
|
||||
}
|
||||
// 1. Preview di tab baru
|
||||
// Preview di tab baru
|
||||
export const previewCertificate = async (
|
||||
cert: Certificate,
|
||||
templateUrl: string,
|
||||
@@ -22,7 +23,7 @@ export const previewCertificate = async (
|
||||
const tab = window.open("", "_blank");
|
||||
|
||||
const blob = await generateCertificate(
|
||||
resolveImageUrl(templateUrl),
|
||||
resolveFileUrl(templateUrl),
|
||||
cert.user.full_name,
|
||||
cert.project.project_name,
|
||||
cert.certificate_no,
|
||||
@@ -79,14 +80,14 @@ export const previewCertificate = async (
|
||||
tab.document.close();
|
||||
};
|
||||
|
||||
// ── 2. Download PDF ───────────────────────────────────────────────────────────
|
||||
// Download PDF
|
||||
export const downloadCertificatePdf = async (
|
||||
cert: Certificate,
|
||||
templateUrl: string,
|
||||
): Promise<void> => {
|
||||
const fileName = `Sertifikat - ${cert.user.full_name} - ${cert.certificate_no}`;
|
||||
const blob = await generateCertificate(
|
||||
resolveImageUrl(templateUrl),
|
||||
resolveFileUrl(templateUrl),
|
||||
cert.user.full_name,
|
||||
cert.project.project_name,
|
||||
cert.certificate_no,
|
||||
@@ -118,7 +119,7 @@ export const downloadCertificatePdf = async (
|
||||
pdf.save(`${fileName}.pdf`);
|
||||
};
|
||||
|
||||
// ── 3. Download PNG atau JPG ──────────────────────────────────────────────────
|
||||
// Download PNG atau JPG
|
||||
export const downloadCertificateImage = async (
|
||||
cert: Certificate,
|
||||
templateUrl: string,
|
||||
@@ -126,7 +127,7 @@ export const downloadCertificateImage = async (
|
||||
): Promise<void> => {
|
||||
const fileName = `Sertifikat - ${cert.user.full_name} - ${cert.certificate_no}`;
|
||||
const blob = await generateCertificate(
|
||||
resolveImageUrl(templateUrl),
|
||||
resolveFileUrl(templateUrl),
|
||||
cert.user.full_name,
|
||||
cert.project.project_name,
|
||||
cert.certificate_no,
|
||||
@@ -176,7 +177,7 @@ export const downloadCertificateImage = async (
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
|
||||
// ── 4. Download All ZIP ───────────────────────────────────────────────────────
|
||||
// Download All ZIP
|
||||
export const downloadAllCertificatesZip = async (
|
||||
certificates: Certificate[],
|
||||
templateUrl: string,
|
||||
@@ -189,7 +190,7 @@ export const downloadAllCertificatesZip = async (
|
||||
await Promise.all(
|
||||
certificates.map(async (cert, i) => {
|
||||
const blob = await generateCertificate(
|
||||
resolveImageUrl(templateUrl),
|
||||
resolveFileUrl(templateUrl),
|
||||
cert.user.full_name,
|
||||
cert.project.project_name,
|
||||
cert.certificate_no,
|
||||
|
||||
@@ -1,24 +1,13 @@
|
||||
import api from "../../lib/api";
|
||||
import { resolveFileUrl } from "./resolveFileFromUrl";
|
||||
import QRCode from "qrcode";
|
||||
|
||||
export function resolveImageUrl(path: string): string {
|
||||
if (!path) return "";
|
||||
if (path.startsWith("data:")) return path;
|
||||
if (path.startsWith("http://") || path.startsWith("https://")) {
|
||||
return path;
|
||||
}
|
||||
const apiBaseURL = api.defaults.baseURL ?? "";
|
||||
const origin = new URL(apiBaseURL).origin;
|
||||
return `${origin}${path.startsWith("/") ? "" : "/"}${path}`;
|
||||
}
|
||||
|
||||
export function loadImage(src: string): Promise<HTMLImageElement> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const img = new Image();
|
||||
if (!src.startsWith("data:")) img.crossOrigin = "anonymous";
|
||||
img.onload = () => resolve(img);
|
||||
img.onerror = () => reject(new Error(`Gagal load gambar: ${src}`));
|
||||
img.src = resolveImageUrl(src);
|
||||
img.src = resolveFileUrl(src);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -30,14 +19,13 @@ export async function ensureFontsLoaded(): Promise<void> {
|
||||
await document.fonts.ready;
|
||||
}
|
||||
|
||||
|
||||
const LAYOUT = {
|
||||
internName: { x: 0.57, y: 0.48, size: 140, font: "Great Vibes", color: "#800000" },
|
||||
internNameSmall: { x: 0.82, y: 0.889, size: 33, font: "Great Vibes", color: "#800000" },
|
||||
internNameSmall: { x: 0.82, y: 0.889, size: 47, font: "Great Vibes", color: "#090909" },
|
||||
projectName: { x: 0.815, y: 0.598, size: 46, font: "Grenze", color: "#800000" },
|
||||
duration: { x: 0.692, y: 0.645, size: 48, font: "Grenze", color: "#090909" },
|
||||
certNo: { x: 0.6, y: 0.28, size: 50, font: "Grenze", color: "#090909" },
|
||||
certNoSmall: { x: 0.825, y: 0.91, size: 22, font: "Grenze", color: "#800000" },
|
||||
duration: { x: 0.7, y: 0.645, size: 48, font: "Grenze", color: "#090909" },
|
||||
certNo: { x: 0.597, y: 0.305, size: 50, font: "Grenze", color: "#090909" },
|
||||
certNoSmall: { x: 0.827, y: 0.92, size: 32, font: "Grenze", color: "#090909"},
|
||||
qr: { x: 0.77, y: 0.715, size: 0.103 },
|
||||
};
|
||||
|
||||
@@ -74,7 +62,7 @@ export async function generateCertificate(
|
||||
text(LAYOUT.projectName, projectName);
|
||||
text(LAYOUT.duration, ProjectDuration);
|
||||
text(LAYOUT.certNo, certificateNo);
|
||||
text(LAYOUT.certNoSmall, certificateNo);
|
||||
text(LAYOUT.certNoSmall, "No.2026/SPI3/" + certificateNo);
|
||||
|
||||
// QR code — nanti akan diganti ke alamat domain
|
||||
const qrSize = Math.round(canvas.width * LAYOUT.qr.size);
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import {API_BASE_URL} from "../../lib/api";
|
||||
|
||||
|
||||
export function resolveFileUrl(path?: string | null): string {
|
||||
if (!path) return "";
|
||||
if (/^(https?:|data:|blob:)/i.test(path)) return path;
|
||||
return `${API_BASE_URL}${path.startsWith("/") ? path : `/${path}`}`;
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
// Ubah message backend (string ATAU object field-errors) jadi satu string aman
|
||||
export function resolveErrorMessage(message: unknown, fallback = "Silakan coba lagi."): string {
|
||||
if (typeof message === "string") return message;
|
||||
if (message && typeof message === "object") {
|
||||
// ambil semua value error, gabung: { end_date: "...", start_date: "..." } -> "... \n ..."
|
||||
return Object.values(message as Record<string, unknown>)
|
||||
.flat()
|
||||
.filter((v): v is string => typeof v === "string")
|
||||
.join(" ") || fallback;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
@@ -12,9 +12,7 @@
|
||||
<script>
|
||||
const TEMPLATE_PATH = "../../assets/template.png";
|
||||
|
||||
// ====== KOORDINAT (rasio terhadap ukuran template) ======
|
||||
// fillText -> titik TENGAH teks (textAlign=center, textBaseline=middle)
|
||||
// drawImage -> pojok KIRI-ATAS gambar
|
||||
// LAYOUT
|
||||
const LAYOUT = {
|
||||
internName: { x: 0.57, y: 0.48, size: 140, font: "Great Vibes", color: "#800000" },
|
||||
internNameSmall: { x: 0.82, y: 0.889, size: 33, font: "Great Vibes", color: "#800000" },
|
||||
@@ -25,7 +23,6 @@
|
||||
qr: { x: 0.77, y: 0.715, size: 0.103 },
|
||||
};
|
||||
|
||||
// ====== DATA DUMMY ======
|
||||
const DATA = {
|
||||
internName: "Jonathan Kristina",
|
||||
projectName: "Internify Proyek Humic",
|
||||
|
||||
Reference in New Issue
Block a user