From 6e66b7facccde25f327197d63b2bab404470ac01 Mon Sep 17 00:00:00 2001 From: VK-RED Date: Sat, 11 May 2024 12:31:45 +0530 Subject: [PATCH] Get Certificate Data from useCert --- src/components/Certificate.tsx | 92 ++++++++++++++++++++-------- src/components/CertificateVerify.tsx | 40 ++++++++---- 2 files changed, 93 insertions(+), 39 deletions(-) diff --git a/src/components/Certificate.tsx b/src/components/Certificate.tsx index 3eef44215..79a92a819 100644 --- a/src/components/Certificate.tsx +++ b/src/components/Certificate.tsx @@ -10,36 +10,68 @@ import { } from '@/components/ui/card'; import { Button } from './ui/button'; import { FaDownload, FaFileImage, FaLinkedin, FaTwitter } from 'react-icons/fa'; +import { useCertPdf, useCertPng } from '@/hooks/useCert'; + +import { useEffect } from 'react'; export const CertificateComponent = ({ course, certificateId }: any) => { + //calls the useCertPdf and useCertPng and passes certificateId and course.id gets the pdfData and ImageData + + const { pdfData, setPdfClicked, generatingPdf } = useCertPdf( + certificateId, + course.id, + ); + const { imgUri, setImgClicked, generatingImg } = useCertPng( + certificateId, + course.id, + ); + useEffect(() => { + if (pdfData) { + const a = document.createElement('a'); + a.download = 'certificate.pdf'; + a.href = pdfData; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + } + }, [pdfData]); + + useEffect(() => { + if (imgUri) { + const imgBlob = getImgBlob(imgUri); + const downloadImgUri = URL.createObjectURL(imgBlob); + window.open(downloadImgUri); + const a = document.createElement('a'); + a.download = 'certificate.png'; + a.href = downloadImgUri; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + URL.revokeObjectURL(downloadImgUri); + } + }, [imgUri]); + + function getImgBlob(dataUri: string) { + const base64Data = dataUri.split(',')[1]; + const binaryData = atob(base64Data); + const arrayBuffer = new ArrayBuffer(binaryData.length); + const view = new Uint8Array(arrayBuffer); + for (let i = 0; i < binaryData.length; i++) { + view[i] = binaryData.charCodeAt(i); + } + + // Convert ArrayBuffer to Blob + const blob = new Blob([arrayBuffer], { type: 'image/png' }); + + return blob; + } + const handleDownloadPDF = async () => { - const response = await fetch( - `/api/certificate/get?courseId=${course.id}&type=pdf`, - ); - const blob = await response.blob(); - const url = window.URL.createObjectURL(blob); - const a = document.createElement('a'); - a.href = url; - a.download = 'certificate.pdf'; - document.body.appendChild(a); - a.click(); - window.URL.revokeObjectURL(url); - document.body.removeChild(a); + setPdfClicked(true); }; const handleDownloadPNG = async () => { - const response = await fetch( - `/api/certificate/get?courseId=${course.id}&type=png`, - ); - const blob = await response.blob(); - const url = window.URL.createObjectURL(blob); - const a = document.createElement('a'); - a.href = url; - a.download = 'certificate.png'; - document.body.appendChild(a); - a.click(); - window.URL.revokeObjectURL(url); - document.body.removeChild(a); + setImgClicked(true); }; const handleShareLinkedIn = async () => { @@ -67,11 +99,19 @@ export const CertificateComponent = ({ course, certificateId }: any) => { {course.description} - - diff --git a/src/components/CertificateVerify.tsx b/src/components/CertificateVerify.tsx index 41f1db20b..e612662e2 100644 --- a/src/components/CertificateVerify.tsx +++ b/src/components/CertificateVerify.tsx @@ -9,6 +9,7 @@ import { } from '@/components/ui/card'; import { useEffect, useState } from 'react'; import { Certificate, Course, User } from '@prisma/client'; +import { useCertPng } from '@/hooks/useCert'; export const CertificateVerify = ({ certificate, @@ -16,23 +17,36 @@ export const CertificateVerify = ({ certificate: Certificate & { user: User; course: Course }; }) => { const [imageUrl, setImageUrl] = useState(''); + const { imgUri, setImgClicked } = useCertPng(certificate.id); useEffect(() => { - const generateImage = async () => { - try { - const response = await fetch( - `/api/certificate/get?certificateId=${certificate.id}&userName=${certificate.user.name || ''}`, - ); - const blob = await response.blob(); - const url = URL.createObjectURL(blob); - setImageUrl(url); - } catch (error) { - console.error('Error generating image:', error); - } - }; - if (!imageUrl) generateImage(); + setImgClicked(true); }, []); + useEffect(() => { + if (imgUri) { + //convert dataUri to downloadable URI + const imgBlob = getImgBlob(imgUri); + const downloadImgUri = URL.createObjectURL(imgBlob); + setImageUrl(downloadImgUri); + } + }, [imgUri]); + + function getImgBlob(dataUri: string) { + const base64Data = dataUri.split(',')[1]; + const binaryData = atob(base64Data); + const arrayBuffer = new ArrayBuffer(binaryData.length); + const view = new Uint8Array(arrayBuffer); + for (let i = 0; i < binaryData.length; i++) { + view[i] = binaryData.charCodeAt(i); + } + + // Convert ArrayBuffer to Blob + const blob = new Blob([arrayBuffer], { type: 'image/png' }); + + return blob; + } + return (