@@ -2,12 +2,13 @@ import React, { useState, useRef, useEffect } from "react";
2
2
import SignatureCanvas from "react-signature-canvas" ;
3
3
import "../styles/managesign.css" ;
4
4
import "../styles/signature.css" ;
5
- import { toDataUrl } from "../constant/Utils" ;
5
+ import { generateTitleFromFilename , toDataUrl } from "../constant/Utils" ;
6
6
import Parse from "parse" ;
7
7
import { SaveFileSize } from "../constant/saveFileSize" ;
8
8
import Alert from "../primitives/Alert" ;
9
9
import Loader from "../primitives/Loader" ;
10
10
import { useTranslation } from "react-i18next" ;
11
+ import sanitizeFileName from "../primitives/sanitizeFileName" ;
11
12
const ManageSign = ( ) => {
12
13
const { t } = useTranslation ( ) ;
13
14
const [ penColor , setPenColor ] = useState ( "blue" ) ;
@@ -46,14 +47,22 @@ const ManageSign = () => {
46
47
if ( signRes ) {
47
48
const res = signRes . toJSON ( ) ;
48
49
setId ( res . objectId ) ;
49
- setSignName ( res ?. SignatureName ) ;
50
- setImage ( res . ImageURL ) ;
50
+ if ( res ?. SignatureName ) {
51
+ const sanitizename = generateTitleFromFilename ( res ?. SignatureName ) ;
52
+ const replaceSpace = sanitizeFileName ( sanitizename ) ;
53
+ setSignName ( replaceSpace ) ;
54
+ }
55
+ setImage ( res ?. ImageURL ) ;
51
56
if ( res && res . Initials ) {
52
57
setInitials ( res . Initials ) ;
53
58
setIsInitials ( true ) ;
54
59
}
55
60
} else {
56
- setSignName ( User ?. get ( "name" ) || "" ) ;
61
+ if ( User ?. get ( "name" ) ) {
62
+ const sanitizename = generateTitleFromFilename ( User ?. get ( "name" ) ) ;
63
+ const replaceSpace = sanitizeFileName ( sanitizename ) ;
64
+ setSignName ( replaceSpace ) ;
65
+ }
57
66
}
58
67
setIsLoader ( false ) ;
59
68
} catch ( err ) {
@@ -87,7 +96,9 @@ const ManageSign = () => {
87
96
initailsRef . current . clear ( ) ;
88
97
}
89
98
setInitials ( "" ) ;
90
- setIsValue ( true ) ;
99
+ if ( image ) {
100
+ setIsValue ( true ) ;
101
+ }
91
102
setIsInitials ( false ) ;
92
103
} ;
93
104
@@ -101,34 +112,39 @@ const ManageSign = () => {
101
112
const base64Img = await toDataUrl ( file ) ;
102
113
setImage ( base64Img ) ;
103
114
setIsValue ( true ) ;
115
+ } else {
116
+ setImage ( "" ) ;
117
+ setIsValue ( false ) ;
104
118
}
105
119
} ;
106
120
const handleSubmit = async ( e ) => {
107
121
e . preventDefault ( ) ;
108
- const isUrl = image . includes ( "https" ) ;
122
+ const isUrl = image . includes ( "https" ) || image . includes ( "http" ) ;
109
123
110
124
if ( ! isvalue ) {
111
125
setWarning ( true ) ;
112
126
setTimeout ( ( ) => setWarning ( false ) , 1000 ) ;
113
127
} else {
114
128
setIsLoader ( true ) ;
115
- const replaceSpace = signName . replace ( / / g, "_" ) ;
129
+ const sanitizename = generateTitleFromFilename ( signName ) ;
130
+ const replaceSpace = sanitizeFileName ( sanitizename ) ;
116
131
let file ;
117
132
if ( signature ) {
118
133
file = base64StringtoFile ( signature , `${ replaceSpace } _sign.png` ) ;
119
134
} else {
120
- if ( ! isUrl ) {
135
+ if ( image && ! isUrl ) {
121
136
file = base64StringtoFile ( image , `${ replaceSpace } __sign.png` ) ;
122
137
}
123
138
}
124
139
let imgUrl ;
125
- if ( ! isUrl ) {
140
+ if ( file && ! isUrl ) {
126
141
imgUrl = await uploadFile ( file ) ;
127
142
} else {
128
143
imgUrl = image ;
129
144
}
130
145
let initialsUrl = "" ;
131
- const isInitialsUrl = Initials . includes ( "https" ) ;
146
+ const isInitialsUrl =
147
+ Initials . includes ( "https" ) || Initials . includes ( "http" ) ;
132
148
if ( ! isInitialsUrl && Initials ) {
133
149
const initialsImg = base64StringtoFile (
134
150
Initials ,
@@ -146,7 +162,7 @@ const ManageSign = () => {
146
162
}
147
163
} ;
148
164
function base64StringtoFile ( base64String , filename ) {
149
- var arr = base64String . split ( "," ) ,
165
+ let arr = base64String . split ( "," ) ,
150
166
mime = arr [ 0 ] . match ( / : ( .* ?) ; / ) [ 1 ] ,
151
167
bstr = atob ( arr [ 1 ] ) ,
152
168
n = bstr . length ,
@@ -182,7 +198,7 @@ const ManageSign = () => {
182
198
const updateSign = new Parse . Object ( signCls ) ;
183
199
updateSign . id = id ;
184
200
updateSign . set ( "Initials" , obj . initialsUrl ? obj . initialsUrl : "" ) ;
185
- updateSign . set ( "ImageURL" , obj . url ) ;
201
+ updateSign . set ( "ImageURL" , obj . url ? obj . url : "" ) ;
186
202
updateSign . set ( "SignatureName" , obj . name ) ;
187
203
updateSign . set ( "UserId" , userId ) ;
188
204
const res = await updateSign . save ( ) ;
@@ -281,7 +297,7 @@ const ManageSign = () => {
281
297
height : "180px" ,
282
298
className : "signatureCanvas rounded-box"
283
299
} }
284
- backgroundColor = "rgb(255, 255, 255)"
300
+ // backgroundColor="rgb(255, 255, 255)"
285
301
onEnd = { ( ) =>
286
302
handleSignatureChange ( canvasRef . current . toDataURL ( ) )
287
303
}
@@ -376,7 +392,7 @@ const ManageSign = () => {
376
392
canvasProps = { {
377
393
className : "intialSignature rounded-box"
378
394
} }
379
- backgroundColor = "rgb(255, 255, 255)"
395
+ // backgroundColor="rgb(255, 255, 255)"
380
396
onEnd = { ( ) =>
381
397
handleInitialsChange ( initailsRef . current . toDataURL ( ) )
382
398
}
0 commit comments