@@ -24,9 +24,11 @@ const ManageSign = () => {
24
24
const [ Initials , setInitials ] = useState ( "" ) ;
25
25
const [ isInitials , setIsInitials ] = useState ( false ) ;
26
26
const [ id , setId ] = useState ( "" ) ;
27
+ const [ imgInitials , setImgInitials ] = useState ( "" ) ;
27
28
const canvasRef = useRef ( null ) ;
28
29
const imageRef = useRef ( null ) ;
29
30
const initailsRef = useRef ( null ) ;
31
+ const imgInitialsRef = useRef ( null ) ;
30
32
useEffect ( ( ) => {
31
33
fetchUserSign ( ) ;
32
34
// eslint-disable-next-line
@@ -54,8 +56,9 @@ const ManageSign = () => {
54
56
}
55
57
setImage ( res ?. ImageURL ) ;
56
58
if ( res && res . Initials ) {
57
- setInitials ( res . Initials ) ;
59
+ // setInitials(res.Initials);
58
60
setIsInitials ( true ) ;
61
+ setImgInitials ( res ?. Initials ) ;
59
62
}
60
63
} else {
61
64
if ( User ?. get ( "name" ) ) {
@@ -95,6 +98,10 @@ const ManageSign = () => {
95
98
if ( initailsRef . current ) {
96
99
initailsRef . current . clear ( ) ;
97
100
}
101
+ if ( imgInitialsRef . current ) {
102
+ imgInitialsRef . current . value = "" ;
103
+ }
104
+ setImgInitials ( "" ) ;
98
105
setInitials ( "" ) ;
99
106
if ( image ) {
100
107
setIsValue ( true ) ;
@@ -119,8 +126,7 @@ const ManageSign = () => {
119
126
} ;
120
127
const handleSubmit = async ( e ) => {
121
128
e . preventDefault ( ) ;
122
- const isUrl = image . includes ( "https" ) || image . includes ( "http" ) ;
123
-
129
+ const isUrl = image ?. includes ( "https" ) || image ?. includes ( "http" ) ;
124
130
if ( ! isvalue ) {
125
131
setWarning ( true ) ;
126
132
setTimeout ( ( ) => setWarning ( false ) , 1000 ) ;
@@ -142,15 +148,25 @@ const ManageSign = () => {
142
148
} else {
143
149
imgUrl = image ;
144
150
}
145
- let initialsUrl = "" ;
146
151
const isInitialsUrl =
147
- Initials . includes ( "https" ) || Initials . includes ( "http" ) ;
148
- if ( ! isInitialsUrl && Initials ) {
149
- const initialsImg = base64StringtoFile (
150
- Initials ,
151
- `${ replaceSpace } _initials.png`
152
- ) ;
153
- initialsUrl = await uploadFile ( initialsImg ) ;
152
+ imgInitials ?. includes ( "https" ) || imgInitials ?. includes ( "http" ) ;
153
+
154
+ let initialFile ;
155
+ if ( Initials ) {
156
+ initialFile = base64StringtoFile ( Initials , `${ replaceSpace } _sign.png` ) ;
157
+ } else {
158
+ if ( imgInitials && ! isInitialsUrl ) {
159
+ initialFile = base64StringtoFile (
160
+ imgInitials ,
161
+ `${ replaceSpace } __sign.png`
162
+ ) ;
163
+ }
164
+ }
165
+ let initialsUrl ;
166
+ if ( initialFile && ! isInitialsUrl ) {
167
+ initialsUrl = await uploadFile ( initialFile ) ;
168
+ } else {
169
+ initialsUrl = imgInitials ;
154
170
}
155
171
if ( imgUrl ) {
156
172
await saveEntry ( {
@@ -191,7 +207,7 @@ const ManageSign = () => {
191
207
192
208
const saveEntry = async ( obj ) => {
193
209
const signCls = "contracts_Signature" ;
194
- const User = Parse . User . current ( ) . id ;
210
+ const User = Parse ? .User ? .current ( ) ? .id ;
195
211
const userId = { __type : "Pointer" , className : "_User" , objectId : User } ;
196
212
if ( id ) {
197
213
try {
@@ -202,20 +218,13 @@ const ManageSign = () => {
202
218
updateSign . set ( "SignatureName" , obj . name ) ;
203
219
updateSign . set ( "UserId" , userId ) ;
204
220
const res = await updateSign . save ( ) ;
205
- setIsLoader ( false ) ;
206
- setIsAlert ( {
207
- type : "success" ,
208
- message : t ( "signature-saved-alert" )
209
- } ) ;
210
- setTimeout ( ( ) => setIsAlert ( { } ) , 2000 ) ;
221
+ setIsAlert ( { type : "success" , message : t ( "signature-saved-alert" ) } ) ;
211
222
return res ;
212
223
} catch ( err ) {
213
224
console . log ( err ) ;
225
+ setIsAlert ( { type : "danger" , message : `${ err . message } ` } ) ;
226
+ } finally {
214
227
setIsLoader ( false ) ;
215
- setIsAlert ( {
216
- type : "danger" ,
217
- message : `${ err . message } `
218
- } ) ;
219
228
setTimeout ( ( ) => setIsAlert ( { } ) , 2000 ) ;
220
229
}
221
230
} else {
@@ -226,20 +235,13 @@ const ManageSign = () => {
226
235
updateSign . set ( "SignatureName" , obj . name ) ;
227
236
updateSign . set ( "UserId" , userId ) ;
228
237
const res = await updateSign . save ( ) ;
229
- setIsLoader ( false ) ;
230
- setIsAlert ( {
231
- type : "success" ,
232
- message : t ( "signature-saved-alert" )
233
- } ) ;
234
- setTimeout ( ( ) => setIsAlert ( { } ) , 2000 ) ;
238
+ setIsAlert ( { type : "success" , message : t ( "signature-saved-alert" ) } ) ;
235
239
return res ;
236
240
} catch ( err ) {
237
241
console . log ( err ) ;
242
+ setIsAlert ( { type : "success" , message : `${ err . message } ` } ) ;
243
+ } finally {
238
244
setIsLoader ( false ) ;
239
- setIsAlert ( {
240
- type : "success" ,
241
- message : `${ err . message } `
242
- } ) ;
243
245
setTimeout ( ( ) => setIsAlert ( { } ) , 2000 ) ;
244
246
}
245
247
}
@@ -250,7 +252,29 @@ const ManageSign = () => {
250
252
} ;
251
253
const handleInitialsChange = ( ) => {
252
254
setInitials ( initailsRef . current . toDataURL ( ) ) ;
253
- setIsValue ( true ) ;
255
+ if ( image || signature ) {
256
+ setIsValue ( true ) ;
257
+ }
258
+ } ;
259
+ const handleUploadInitials = ( ) => {
260
+ imgInitialsRef . current . click ( ) ;
261
+ } ;
262
+ const onImgInitialsChange = async ( event ) => {
263
+ if ( initailsRef . current ) {
264
+ initailsRef . current . clear ( ) ;
265
+ }
266
+ setInitials ( "" ) ;
267
+ if ( event . target . files && event . target . files [ 0 ] ) {
268
+ const file = event . target . files [ 0 ] ;
269
+ const base64Img = await toDataUrl ( file ) ;
270
+ setImgInitials ( base64Img ) ;
271
+ if ( image || signature ) {
272
+ setIsValue ( true ) ;
273
+ }
274
+ } else {
275
+ setImgInitials ( "" ) ;
276
+ setIsValue ( false ) ;
277
+ }
254
278
} ;
255
279
return (
256
280
< div className = "relative h-full bg-base-100 text-base-content flex shadow-md rounded-box overflow-auto" >
@@ -348,7 +372,7 @@ const ManageSign = () => {
348
372
className = "op-link"
349
373
onClick = { ( ) => handleUploadBtn ( ) }
350
374
>
351
- { t ( "upload-image " ) }
375
+ { t ( "upload" ) }
352
376
</ div >
353
377
< div
354
378
type = "button"
@@ -372,17 +396,21 @@ const ManageSign = () => {
372
396
< span className = "font-medium select-none flex mb-[10px] pl-[10px]" >
373
397
{ t ( "initials" ) }
374
398
</ span >
399
+ < input
400
+ type = "file"
401
+ onChange = { onImgInitialsChange }
402
+ className = "filetype"
403
+ accept = "image/*"
404
+ ref = { imgInitialsRef }
405
+ hidden
406
+ />
375
407
< div >
376
- { isInitials ? (
408
+ { imgInitials ? (
377
409
< div className = "intialSignature relative border-[1px] border-[#888] rounded-box overflow-hidden" >
378
410
< img
379
411
alt = "inititals"
380
- src = { Initials }
381
- style = { {
382
- width : "100%" ,
383
- height : "100%" ,
384
- objectFit : "contain"
385
- } }
412
+ src = { imgInitials }
413
+ className = "w-[100%] h-[100%] object-contain"
386
414
/>
387
415
</ div >
388
416
) : (
@@ -393,9 +421,7 @@ const ManageSign = () => {
393
421
className : "intialSignature rounded-box"
394
422
} }
395
423
// backgroundColor="rgb(255, 255, 255)"
396
- onEnd = { ( ) =>
397
- handleInitialsChange ( initailsRef . current . toDataURL ( ) )
398
- }
424
+ onEnd = { ( ) => handleInitialsChange ( ) }
399
425
dotSize = { 1 }
400
426
/>
401
427
) }
@@ -418,6 +444,7 @@ const ManageSign = () => {
418
444
? "2px solid black"
419
445
: "2px solid white"
420
446
} }
447
+ className = "fa-light fa-pen-nib"
421
448
onClick = { ( ) => {
422
449
if ( key === 0 ) {
423
450
setInitialPen ( "blue" ) ;
@@ -428,7 +455,6 @@ const ManageSign = () => {
428
455
}
429
456
} }
430
457
key = { key }
431
- className = "fa-light fa-pen-nib"
432
458
width = { 20 }
433
459
height = { 20 }
434
460
> </ i >
@@ -437,13 +463,22 @@ const ManageSign = () => {
437
463
</ div >
438
464
) }
439
465
</ div >
440
- < div >
466
+ < div className = "flex flex-row gap-1" >
441
467
< div
442
468
type = "button"
443
469
className = "op-link text-sm md:text-base mr-1"
444
- onClick = { ( ) => handleClearInitials ( ) }
470
+ onClick = { ( ) => handleUploadInitials ( ) }
445
471
>
446
- { t ( "clear" ) }
472
+ { t ( "upload" ) }
473
+ </ div >
474
+ < div >
475
+ < div
476
+ type = "button"
477
+ className = "op-link text-sm md:text-base mr-1"
478
+ onClick = { ( ) => handleClearInitials ( ) }
479
+ >
480
+ { t ( "clear" ) }
481
+ </ div >
447
482
</ div >
448
483
</ div >
449
484
</ div >
0 commit comments