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,130 +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
- setIsDownloading ( "pdf" ) ;
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
- setIsDownloading ( "" ) ;
97
- } else {
98
- printModule ( { printable : pdf , type : "pdf" , base64 : true } ) ;
99
- setIsDownloading ( "" ) ;
100
- }
101
- } catch ( err ) {
102
- setIsDownloading ( "" ) ;
103
- console . log ( "err in getsignedurl" , err ) ;
104
- alert ( "something went wrong, please try again later." ) ;
105
- }
106
- } ;
107
-
108
- //handle download signed pdf
109
- const handleDownloadPdf = async ( ) => {
110
- setIsDownloading ( "pdf" ) ;
111
- const pdfName = pdfDetails [ 0 ] && pdfDetails [ 0 ] . Name ;
112
- try {
113
- // const url = await Parse.Cloud.run("getsignedurl", { url: pdfUrl });
114
- const axiosRes = await axios . post (
115
- `${ appInfo . baseUrl } /functions/getsignedurl` ,
116
- { url : pdfUrl } ,
117
- {
118
- headers : {
119
- "content-type" : "Application/json" ,
120
- "X-Parse-Application-Id" : appInfo . appId ,
121
- "X-Parse-Session-Token" : localStorage . getItem ( "accesstoken" )
122
- }
123
- }
124
- ) ;
125
- const url = axiosRes . data . result ;
126
- saveAs ( url , `${ sanitizeFileName ( pdfName ) } _signed_by_OpenSign™.pdf` ) ;
127
- setIsDownloading ( "" ) ;
128
- } catch ( err ) {
129
- console . log ( "err in getsignedurl" , err ) ;
130
- setIsDownloading ( "" ) ;
131
- alert ( "something went wrong, please try again later." ) ;
132
- }
133
- } ;
134
-
135
- const sanitizeFileName = ( pdfName ) => {
136
- // Replace spaces with underscore
137
- return pdfName . replace ( / / g, "_" ) ;
138
- } ;
139
-
140
- //handle download signed pdf
141
- const handleDownloadCertificate = async ( ) => {
142
- if ( pdfDetails ?. length > 0 && pdfDetails [ 0 ] ?. CertificateUrl ) {
143
- try {
144
- await fetch ( pdfDetails [ 0 ] && pdfDetails [ 0 ] ?. CertificateUrl ) ;
145
- const certificateUrl = pdfDetails [ 0 ] && pdfDetails [ 0 ] ?. CertificateUrl ;
146
- saveAs ( certificateUrl , `Certificate_signed_by_OpenSign™.pdf` ) ;
147
- } catch ( err ) {
148
- console . log ( "err in download in certificate" , err ) ;
149
- }
150
- } else {
151
- setIsDownloading ( "certificate" ) ;
152
- try {
153
- const data = {
154
- docId : pdfDetails [ 0 ] ?. objectId
155
- } ;
156
- const docDetails = await axios . post (
157
- `${ localStorage . getItem ( "baseUrl" ) } functions/getDocument` ,
158
- data ,
159
- {
160
- headers : {
161
- "Content-Type" : "application/json" ,
162
- "X-Parse-Application-Id" : localStorage . getItem ( "parseAppId" ) ,
163
- sessionToken : localStorage . getItem ( "accesstoken" )
164
- }
165
- }
166
- ) ;
167
- if ( docDetails . data && docDetails . data . result ) {
168
- const doc = docDetails . data . result ;
169
- if ( doc ?. CertificateUrl ) {
170
- await fetch ( doc ?. CertificateUrl ) ;
171
- const certificateUrl = doc ?. CertificateUrl ;
172
- saveAs ( certificateUrl , `Certificate_signed_by_OpenSign™.pdf` ) ;
173
- setIsDownloading ( "" ) ;
174
- } else {
175
- setIsDownloading ( "certificate" ) ;
176
- }
177
- }
178
- } catch ( err ) {
179
- setIsDownloading ( "" ) ;
180
- console . log ( "err in download in certificate" , err ) ;
181
- alert ( "something went wrong, please try again later." ) ;
182
- }
183
- }
184
- } ;
185
-
186
62
return (
187
63
< div style = { { padding : "5px 0px 5px 0px" } } className = "mobileHead" >
188
64
{ isMobile && isShowHeader ? (
@@ -255,7 +131,9 @@ function Header({
255
131
>
256
132
< DropdownMenu . Item
257
133
className = "DropdownMenuItem"
258
- onClick = { ( ) => handleDownloadPdf ( ) }
134
+ onClick = { ( ) =>
135
+ handleDownloadPdf ( pdfDetails , pdfUrl , setIsDownloading )
136
+ }
259
137
>
260
138
< div
261
139
style = { {
@@ -274,7 +152,12 @@ function Header({
274
152
{ isCompleted && (
275
153
< DropdownMenu . Item
276
154
className = "DropdownMenuItem"
277
- onClick = { ( ) => handleDownloadCertificate ( ) }
155
+ onClick = { ( ) =>
156
+ handleDownloadCertificate (
157
+ pdfDetails ,
158
+ setIsDownloading
159
+ )
160
+ }
278
161
>
279
162
< div
280
163
style = { {
@@ -313,7 +196,9 @@ function Header({
313
196
) }
314
197
< DropdownMenu . Item
315
198
className = "DropdownMenuItem"
316
- onClick = { handleToPrint }
199
+ onClick = { ( e ) =>
200
+ handleToPrint ( e , pdfUrl , setIsDownloading )
201
+ }
317
202
>
318
203
< div
319
204
style = { {
@@ -395,7 +280,13 @@ function Header({
395
280
>
396
281
< DropdownMenu . Item
397
282
className = "flex flex-row justify-center items-center text-[13px] focus:outline-none cursor-pointer"
398
- onClick = { ( ) => handleDownloadPdf ( ) }
283
+ onClick = { ( ) =>
284
+ handleDownloadPdf (
285
+ pdfDetails ,
286
+ pdfUrl ,
287
+ setIsDownloading
288
+ )
289
+ }
399
290
>
400
291
< i
401
292
className = "fa fa-arrow-down mr-[5px]"
@@ -482,7 +373,9 @@ function Header({
482
373
{ isCompleted && (
483
374
< button
484
375
type = "button"
485
- onClick = { ( ) => handleDownloadCertificate ( ) }
376
+ onClick = { ( ) =>
377
+ handleDownloadCertificate ( pdfDetails , setIsDownloading )
378
+ }
486
379
className = "flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#08bc66]"
487
380
>
488
381
< i
@@ -494,7 +387,7 @@ function Header({
494
387
) }
495
388
496
389
< button
497
- onClick = { handleToPrint }
390
+ onClick = { ( e ) => handleToPrint ( e , pdfUrl , setIsDownloading ) }
498
391
type = "button"
499
392
className = "flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#188ae2]"
500
393
>
@@ -505,7 +398,9 @@ function Header({
505
398
< button
506
399
type = "button"
507
400
className = "flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#f14343]"
508
- onClick = { ( ) => handleDownloadPdf ( ) }
401
+ onClick = { ( ) =>
402
+ handleDownloadPdf ( pdfDetails , pdfUrl , setIsDownloading )
403
+ }
509
404
>
510
405
< i className = "fa fa-download py-[3px]" aria-hidden = "true" > </ i >
511
406
< span className = "hidden lg:block ml-1" > Download</ span >
@@ -554,7 +449,13 @@ function Header({
554
449
>
555
450
< DropdownMenu . Item
556
451
className = "flex flex-row justify-center items-center text-[13px] focus:outline-none cursor-pointer"
557
- onClick = { ( ) => handleDownloadPdf ( ) }
452
+ onClick = { ( ) =>
453
+ handleDownloadPdf (
454
+ pdfDetails ,
455
+ pdfUrl ,
456
+ setIsDownloading
457
+ )
458
+ }
558
459
>
559
460
< i
560
461
className = "fa fa-arrow-down mr-[5px]"
@@ -574,7 +475,9 @@ function Header({
574
475
{ isCompleted && (
575
476
< button
576
477
type = "button"
577
- onClick = { ( ) => handleDownloadCertificate ( ) }
478
+ onClick = { ( ) =>
479
+ handleDownloadCertificate ( pdfDetails , setIsDownloading )
480
+ }
578
481
className = "flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#08bc66]"
579
482
>
580
483
< i
@@ -585,7 +488,7 @@ function Header({
585
488
</ button >
586
489
) }
587
490
< button
588
- onClick = { handleToPrint }
491
+ onClick = { ( e ) => handleToPrint ( e , pdfUrl , setIsDownloading ) }
589
492
type = "button"
590
493
className = "flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#188ae2]"
591
494
>
@@ -595,7 +498,9 @@ function Header({
595
498
< button
596
499
type = "button"
597
500
className = "flex flex-row items-center shadow rounded-[3px] py-[3px] px-[11px] text-white font-[500] text-[13px] mr-[5px] bg-[#f14343]"
598
- onClick = { ( ) => handleDownloadPdf ( ) }
501
+ onClick = { ( ) =>
502
+ handleDownloadPdf ( pdfDetails , pdfUrl , setIsDownloading )
503
+ }
599
504
>
600
505
< i className = "fa fa-download py-[3px]" aria-hidden = "true" > </ i >
601
506
< span className = "hidden lg:block ml-1" > Download</ span >
0 commit comments