diff --git a/apps/OpenSign/src/components/pdf/PdfHeader.js b/apps/OpenSign/src/components/pdf/PdfHeader.js index 4d6a280f9..075945526 100644 --- a/apps/OpenSign/src/components/pdf/PdfHeader.js +++ b/apps/OpenSign/src/components/pdf/PdfHeader.js @@ -1,15 +1,15 @@ import React, { useState } from "react"; import PrevNext from "./PrevNext"; -import printModule from "print-js"; -import { getBase64FromUrl } from "../../constant/Utils"; -import { saveAs } from "file-saver"; +import { + handleDownloadCertificate, + handleDownloadPdf, + handleToPrint +} from "../../constant/Utils"; import "../../styles/signature.css"; import * as DropdownMenu from "@radix-ui/react-dropdown-menu"; import { useNavigate } from "react-router-dom"; import { themeColor } from "../../constant/const"; -import axios from "axios"; import ModalUi from "../../primitives/ModalUi"; -import { appInfo } from "../../constant/appinfo"; function Header({ isPdfRequestFiles, @@ -59,123 +59,6 @@ function Header({ setIsDecline(currentDecline); }; - //function for print digital sign pdf - const handleToPrint = async (event) => { - event.preventDefault(); - - try { - // const url = await Parse.Cloud.run("getsignedurl", { url: pdfUrl }); - const axiosRes = await axios.post( - `${appInfo.baseUrl}/functions/getsignedurl`, - { url: pdfUrl }, - { - headers: { - "content-type": "Application/json", - "X-Parse-Application-Id": appInfo.appId, - "X-Parse-Session-Token": localStorage.getItem("accesstoken") - } - } - ); - const url = axiosRes.data.result; - const pdf = await getBase64FromUrl(url); - const isAndroidDevice = navigator.userAgent.match(/Android/i); - const isAppleDevice = - (/iPad|iPhone|iPod/.test(navigator.platform) || - (navigator.platform === "MacIntel" && - navigator.maxTouchPoints > 1)) && - !window.MSStream; - if (isAndroidDevice || isAppleDevice) { - const byteArray = Uint8Array.from( - atob(pdf) - .split("") - .map((char) => char.charCodeAt(0)) - ); - const blob = new Blob([byteArray], { type: "application/pdf" }); - const blobUrl = URL.createObjectURL(blob); - window.open(blobUrl, "_blank"); - } else { - printModule({ printable: pdf, type: "pdf", base64: true }); - } - } catch (err) { - console.log("err in getsignedurl", err); - alert("something went wrong, please try again later."); - } - }; - - //handle download signed pdf - const handleDownloadPdf = async () => { - const pdfName = pdfDetails[0] && pdfDetails[0].Name; - try { - // const url = await Parse.Cloud.run("getsignedurl", { url: pdfUrl }); - const axiosRes = await axios.post( - `${appInfo.baseUrl}/functions/getsignedurl`, - { url: pdfUrl }, - { - headers: { - "content-type": "Application/json", - "X-Parse-Application-Id": appInfo.appId, - "X-Parse-Session-Token": localStorage.getItem("accesstoken") - } - } - ); - const url = axiosRes.data.result; - saveAs(url, `${sanitizeFileName(pdfName)}_signed_by_OpenSign™.pdf`); - } catch (err) { - console.log("err in getsignedurl", err); - alert("something went wrong, please try again later."); - } - }; - - const sanitizeFileName = (pdfName) => { - // Replace spaces with underscore - return pdfName.replace(/ /g, "_"); - }; - - //handle download signed pdf - const handleDownloadCertificate = async () => { - if (pdfDetails?.length > 0 && pdfDetails[0]?.CertificateUrl) { - try { - await fetch(pdfDetails[0] && pdfDetails[0]?.CertificateUrl); - const certificateUrl = pdfDetails[0] && pdfDetails[0]?.CertificateUrl; - saveAs(certificateUrl, `Certificate_signed_by_OpenSign™.pdf`); - } catch (err) { - console.log("err in download in certificate", err); - } - } else { - setIsCertificate(true); - try { - const data = { - docId: pdfDetails[0]?.objectId - }; - const docDetails = await axios.post( - `${localStorage.getItem("baseUrl")}functions/getDocument`, - data, - { - headers: { - "Content-Type": "application/json", - "X-Parse-Application-Id": localStorage.getItem("parseAppId"), - sessionToken: localStorage.getItem("accesstoken") - } - } - ); - if (docDetails.data && docDetails.data.result) { - const doc = docDetails.data.result; - if (doc?.CertificateUrl) { - await fetch(doc?.CertificateUrl); - const certificateUrl = doc?.CertificateUrl; - saveAs(certificateUrl, `Certificate_signed_by_OpenSign™.pdf`); - setIsCertificate(false); - } else { - setIsCertificate(true); - } - } - } catch (err) { - console.log("err in download in certificate", err); - alert("something went wrong, please try again later."); - } - } - }; - return (
{isMobile && isShowHeader ? ( @@ -248,7 +131,7 @@ function Header({ > handleDownloadPdf()} + onClick={() => handleDownloadPdf(pdfDetails, pdfUrl)} >
handleDownloadCertificate()} + onClick={() => + handleDownloadCertificate( + pdfDetails, + setIsCertificate + ) + } >
handleToPrint(e, pdfUrl)} >
handleDownloadPdf()} + onClick={() => + handleDownloadPdf(pdfDetails, pdfUrl) + } > handleDownloadCertificate()} + onClick={() => + handleDownloadCertificate(pdfDetails, setIsCertificate) + } className="flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#08bc66]" > handleToPrint(e, pdfUrl)} type="button" className="flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#188ae2]" > @@ -498,7 +390,7 @@ function Header({ + {isCompleted?.message ? ( +

{isCompleted?.message}

+ ) : ( +
+ + Congratulations! 🎉 This document has been + successfully + +
+ signed by all participants! +
+ )} + {!isCompleted?.message && ( +
+ + + +
+ )} +
+ + setIsCertificate(false)} + > +
+

+ Your completion certificate is being generated. Please + wait momentarily. +

+

+ If the download doesn't start shortly, click the + button again. +

- {/* this component is used for signature pad modal */} { return ( @@ -18,11 +18,9 @@ const ModalUi = ({ {isOpen && (
{showHeader && (