1
1
import React , { useState } from "react" ;
2
2
import PrevNext from "./PrevNext" ;
3
- import printModule from "print-js" ;
4
- import { getBase64FromUrl } from "../../constant/Utils" ;
5
- import { saveAs } from "file-saver" ;
3
+ import {
4
+ handleDownloadCertificate ,
5
+ handleDownloadPdf ,
6
+ handleToPrint
7
+ } from "../../constant/Utils" ;
6
8
import "../../styles/signature.css" ;
7
9
import * as DropdownMenu from "@radix-ui/react-dropdown-menu" ;
8
10
import { useNavigate } from "react-router-dom" ;
9
11
import { themeColor } from "../../constant/const" ;
10
- import axios from "axios" ;
11
12
import ModalUi from "../../primitives/ModalUi" ;
12
- import { appInfo } from "../../constant/appinfo" ;
13
13
14
14
function Header ( {
15
15
isPdfRequestFiles,
@@ -59,123 +59,6 @@ function Header({
59
59
setIsDecline ( currentDecline ) ;
60
60
} ;
61
61
62
- //function for print digital sign pdf
63
- const handleToPrint = async ( event ) => {
64
- event . preventDefault ( ) ;
65
-
66
- try {
67
- // const url = await Parse.Cloud.run("getsignedurl", { url: pdfUrl });
68
- const axiosRes = await axios . post (
69
- `${ appInfo . baseUrl } /functions/getsignedurl` ,
70
- { url : pdfUrl } ,
71
- {
72
- headers : {
73
- "content-type" : "Application/json" ,
74
- "X-Parse-Application-Id" : appInfo . appId ,
75
- "X-Parse-Session-Token" : localStorage . getItem ( "accesstoken" )
76
- }
77
- }
78
- ) ;
79
- const url = axiosRes . data . result ;
80
- const pdf = await getBase64FromUrl ( url ) ;
81
- const isAndroidDevice = navigator . userAgent . match ( / A n d r o i d / i) ;
82
- const isAppleDevice =
83
- ( / i P a d | i P h o n e | i P o d / . test ( navigator . platform ) ||
84
- ( navigator . platform === "MacIntel" &&
85
- navigator . maxTouchPoints > 1 ) ) &&
86
- ! window . MSStream ;
87
- if ( isAndroidDevice || isAppleDevice ) {
88
- const byteArray = Uint8Array . from (
89
- atob ( pdf )
90
- . split ( "" )
91
- . map ( ( char ) => char . charCodeAt ( 0 ) )
92
- ) ;
93
- const blob = new Blob ( [ byteArray ] , { type : "application/pdf" } ) ;
94
- const blobUrl = URL . createObjectURL ( blob ) ;
95
- window . open ( blobUrl , "_blank" ) ;
96
- } else {
97
- printModule ( { printable : pdf , type : "pdf" , base64 : true } ) ;
98
- }
99
- } catch ( err ) {
100
- console . log ( "err in getsignedurl" , err ) ;
101
- alert ( "something went wrong, please try again later." ) ;
102
- }
103
- } ;
104
-
105
- //handle download signed pdf
106
- const handleDownloadPdf = async ( ) => {
107
- const pdfName = pdfDetails [ 0 ] && pdfDetails [ 0 ] . Name ;
108
- try {
109
- // const url = await Parse.Cloud.run("getsignedurl", { url: pdfUrl });
110
- const axiosRes = await axios . post (
111
- `${ appInfo . baseUrl } /functions/getsignedurl` ,
112
- { url : pdfUrl } ,
113
- {
114
- headers : {
115
- "content-type" : "Application/json" ,
116
- "X-Parse-Application-Id" : appInfo . appId ,
117
- "X-Parse-Session-Token" : localStorage . getItem ( "accesstoken" )
118
- }
119
- }
120
- ) ;
121
- const url = axiosRes . data . result ;
122
- saveAs ( url , `${ sanitizeFileName ( pdfName ) } _signed_by_OpenSign™.pdf` ) ;
123
- } catch ( err ) {
124
- console . log ( "err in getsignedurl" , err ) ;
125
- alert ( "something went wrong, please try again later." ) ;
126
- }
127
- } ;
128
-
129
- const sanitizeFileName = ( pdfName ) => {
130
- // Replace spaces with underscore
131
- return pdfName . replace ( / / g, "_" ) ;
132
- } ;
133
-
134
- //handle download signed pdf
135
- const handleDownloadCertificate = async ( ) => {
136
- if ( pdfDetails ?. length > 0 && pdfDetails [ 0 ] ?. CertificateUrl ) {
137
- try {
138
- await fetch ( pdfDetails [ 0 ] && pdfDetails [ 0 ] ?. CertificateUrl ) ;
139
- const certificateUrl = pdfDetails [ 0 ] && pdfDetails [ 0 ] ?. CertificateUrl ;
140
- saveAs ( certificateUrl , `Certificate_signed_by_OpenSign™.pdf` ) ;
141
- } catch ( err ) {
142
- console . log ( "err in download in certificate" , err ) ;
143
- }
144
- } else {
145
- setIsCertificate ( true ) ;
146
- try {
147
- const data = {
148
- docId : pdfDetails [ 0 ] ?. objectId
149
- } ;
150
- const docDetails = await axios . post (
151
- `${ localStorage . getItem ( "baseUrl" ) } functions/getDocument` ,
152
- data ,
153
- {
154
- headers : {
155
- "Content-Type" : "application/json" ,
156
- "X-Parse-Application-Id" : localStorage . getItem ( "parseAppId" ) ,
157
- sessionToken : localStorage . getItem ( "accesstoken" )
158
- }
159
- }
160
- ) ;
161
- if ( docDetails . data && docDetails . data . result ) {
162
- const doc = docDetails . data . result ;
163
- if ( doc ?. CertificateUrl ) {
164
- await fetch ( doc ?. CertificateUrl ) ;
165
- const certificateUrl = doc ?. CertificateUrl ;
166
- saveAs ( certificateUrl , `Certificate_signed_by_OpenSign™.pdf` ) ;
167
- setIsCertificate ( false ) ;
168
- } else {
169
- setIsCertificate ( true ) ;
170
- }
171
- }
172
- } catch ( err ) {
173
- console . log ( "err in download in certificate" , err ) ;
174
- alert ( "something went wrong, please try again later." ) ;
175
- }
176
- }
177
- } ;
178
-
179
62
return (
180
63
< div style = { { padding : "5px 0px 5px 0px" } } className = "mobileHead" >
181
64
{ isMobile && isShowHeader ? (
@@ -248,7 +131,7 @@ function Header({
248
131
>
249
132
< DropdownMenu . Item
250
133
className = "DropdownMenuItem"
251
- onClick = { ( ) => handleDownloadPdf ( ) }
134
+ onClick = { ( ) => handleDownloadPdf ( pdfDetails , pdfUrl ) }
252
135
>
253
136
< div
254
137
style = { {
@@ -267,7 +150,12 @@ function Header({
267
150
{ isCompleted && (
268
151
< DropdownMenu . Item
269
152
className = "DropdownMenuItem"
270
- onClick = { ( ) => handleDownloadCertificate ( ) }
153
+ onClick = { ( ) =>
154
+ handleDownloadCertificate (
155
+ pdfDetails ,
156
+ setIsCertificate
157
+ )
158
+ }
271
159
>
272
160
< div
273
161
style = { {
@@ -306,7 +194,7 @@ function Header({
306
194
) }
307
195
< DropdownMenu . Item
308
196
className = "DropdownMenuItem"
309
- onClick = { handleToPrint }
197
+ onClick = { ( e ) => handleToPrint ( e , pdfUrl ) }
310
198
>
311
199
< div
312
200
style = { {
@@ -388,7 +276,9 @@ function Header({
388
276
>
389
277
< DropdownMenu . Item
390
278
className = "flex flex-row justify-center items-center text-[13px] focus:outline-none cursor-pointer"
391
- onClick = { ( ) => handleDownloadPdf ( ) }
279
+ onClick = { ( ) =>
280
+ handleDownloadPdf ( pdfDetails , pdfUrl )
281
+ }
392
282
>
393
283
< i
394
284
className = "fa fa-arrow-down mr-[5px]"
@@ -475,7 +365,9 @@ function Header({
475
365
{ isCompleted && (
476
366
< button
477
367
type = "button"
478
- onClick = { ( ) => handleDownloadCertificate ( ) }
368
+ onClick = { ( ) =>
369
+ handleDownloadCertificate ( pdfDetails , setIsCertificate )
370
+ }
479
371
className = "flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#08bc66]"
480
372
>
481
373
< i
@@ -487,7 +379,7 @@ function Header({
487
379
) }
488
380
489
381
< button
490
- onClick = { handleToPrint }
382
+ onClick = { ( e ) => handleToPrint ( e , pdfUrl ) }
491
383
type = "button"
492
384
className = "flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#188ae2]"
493
385
>
@@ -498,7 +390,7 @@ function Header({
498
390
< button
499
391
type = "button"
500
392
className = "flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#f14343]"
501
- onClick = { ( ) => handleDownloadPdf ( ) }
393
+ onClick = { ( ) => handleDownloadPdf ( pdfDetails , pdfUrl ) }
502
394
>
503
395
< i className = "fa fa-download py-[3px]" aria-hidden = "true" > </ i >
504
396
< span className = "hidden lg:block ml-1" > Download</ span >
@@ -547,7 +439,9 @@ function Header({
547
439
>
548
440
< DropdownMenu . Item
549
441
className = "flex flex-row justify-center items-center text-[13px] focus:outline-none cursor-pointer"
550
- onClick = { ( ) => handleDownloadPdf ( ) }
442
+ onClick = { ( ) =>
443
+ handleDownloadPdf ( pdfDetails , pdfUrl )
444
+ }
551
445
>
552
446
< i
553
447
className = "fa fa-arrow-down mr-[5px]"
@@ -567,7 +461,9 @@ function Header({
567
461
{ isCompleted && (
568
462
< button
569
463
type = "button"
570
- onClick = { ( ) => handleDownloadCertificate ( ) }
464
+ onClick = { ( ) =>
465
+ handleDownloadCertificate ( pdfDetails , setIsCertificate )
466
+ }
571
467
className = "flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#08bc66]"
572
468
>
573
469
< i
@@ -578,7 +474,7 @@ function Header({
578
474
</ button >
579
475
) }
580
476
< button
581
- onClick = { handleToPrint }
477
+ onClick = { ( e ) => handleToPrint ( e , pdfUrl ) }
582
478
type = "button"
583
479
className = "flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#188ae2]"
584
480
>
@@ -588,7 +484,7 @@ function Header({
588
484
< button
589
485
type = "button"
590
486
className = "flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#f14343]"
591
- onClick = { ( ) => handleDownloadPdf ( ) }
487
+ onClick = { ( ) => handleDownloadPdf ( pdfDetails , pdfUrl ) }
592
488
>
593
489
< i className = "fa fa-download py-[3px]" aria-hidden = "true" > </ i >
594
490
< span className = "hidden lg:block ml-1" > Download</ span >
0 commit comments