@@ -24,9 +24,11 @@ const ManageSign = () => {
2424 const [ Initials , setInitials ] = useState ( "" ) ;
2525 const [ isInitials , setIsInitials ] = useState ( false ) ;
2626 const [ id , setId ] = useState ( "" ) ;
27+ const [ imgInitials , setImgInitials ] = useState ( "" ) ;
2728 const canvasRef = useRef ( null ) ;
2829 const imageRef = useRef ( null ) ;
2930 const initailsRef = useRef ( null ) ;
31+ const imgInitialsRef = useRef ( null ) ;
3032 useEffect ( ( ) => {
3133 fetchUserSign ( ) ;
3234 // eslint-disable-next-line
@@ -54,8 +56,9 @@ const ManageSign = () => {
5456 }
5557 setImage ( res ?. ImageURL ) ;
5658 if ( res && res . Initials ) {
57- setInitials ( res . Initials ) ;
59+ // setInitials(res.Initials);
5860 setIsInitials ( true ) ;
61+ setImgInitials ( res ?. Initials ) ;
5962 }
6063 } else {
6164 if ( User ?. get ( "name" ) ) {
@@ -95,6 +98,10 @@ const ManageSign = () => {
9598 if ( initailsRef . current ) {
9699 initailsRef . current . clear ( ) ;
97100 }
101+ if ( imgInitialsRef . current ) {
102+ imgInitialsRef . current . value = "" ;
103+ }
104+ setImgInitials ( "" ) ;
98105 setInitials ( "" ) ;
99106 if ( image ) {
100107 setIsValue ( true ) ;
@@ -119,8 +126,7 @@ const ManageSign = () => {
119126 } ;
120127 const handleSubmit = async ( e ) => {
121128 e . preventDefault ( ) ;
122- const isUrl = image . includes ( "https" ) || image . includes ( "http" ) ;
123-
129+ const isUrl = image ?. includes ( "https" ) || image ?. includes ( "http" ) ;
124130 if ( ! isvalue ) {
125131 setWarning ( true ) ;
126132 setTimeout ( ( ) => setWarning ( false ) , 1000 ) ;
@@ -142,15 +148,25 @@ const ManageSign = () => {
142148 } else {
143149 imgUrl = image ;
144150 }
145- let initialsUrl = "" ;
146151 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 ;
154170 }
155171 if ( imgUrl ) {
156172 await saveEntry ( {
@@ -191,7 +207,7 @@ const ManageSign = () => {
191207
192208 const saveEntry = async ( obj ) => {
193209 const signCls = "contracts_Signature" ;
194- const User = Parse . User . current ( ) . id ;
210+ const User = Parse ? .User ? .current ( ) ? .id ;
195211 const userId = { __type : "Pointer" , className : "_User" , objectId : User } ;
196212 if ( id ) {
197213 try {
@@ -202,20 +218,13 @@ const ManageSign = () => {
202218 updateSign . set ( "SignatureName" , obj . name ) ;
203219 updateSign . set ( "UserId" , userId ) ;
204220 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" ) } ) ;
211222 return res ;
212223 } catch ( err ) {
213224 console . log ( err ) ;
225+ setIsAlert ( { type : "danger" , message : `${ err . message } ` } ) ;
226+ } finally {
214227 setIsLoader ( false ) ;
215- setIsAlert ( {
216- type : "danger" ,
217- message : `${ err . message } `
218- } ) ;
219228 setTimeout ( ( ) => setIsAlert ( { } ) , 2000 ) ;
220229 }
221230 } else {
@@ -226,20 +235,13 @@ const ManageSign = () => {
226235 updateSign . set ( "SignatureName" , obj . name ) ;
227236 updateSign . set ( "UserId" , userId ) ;
228237 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" ) } ) ;
235239 return res ;
236240 } catch ( err ) {
237241 console . log ( err ) ;
242+ setIsAlert ( { type : "success" , message : `${ err . message } ` } ) ;
243+ } finally {
238244 setIsLoader ( false ) ;
239- setIsAlert ( {
240- type : "success" ,
241- message : `${ err . message } `
242- } ) ;
243245 setTimeout ( ( ) => setIsAlert ( { } ) , 2000 ) ;
244246 }
245247 }
@@ -250,7 +252,29 @@ const ManageSign = () => {
250252 } ;
251253 const handleInitialsChange = ( ) => {
252254 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+ }
254278 } ;
255279 return (
256280 < div className = "relative h-full bg-base-100 text-base-content flex shadow-md rounded-box overflow-auto" >
@@ -348,7 +372,7 @@ const ManageSign = () => {
348372 className = "op-link"
349373 onClick = { ( ) => handleUploadBtn ( ) }
350374 >
351- { t ( "upload-image " ) }
375+ { t ( "upload" ) }
352376 </ div >
353377 < div
354378 type = "button"
@@ -372,17 +396,21 @@ const ManageSign = () => {
372396 < span className = "font-medium select-none flex mb-[10px] pl-[10px]" >
373397 { t ( "initials" ) }
374398 </ span >
399+ < input
400+ type = "file"
401+ onChange = { onImgInitialsChange }
402+ className = "filetype"
403+ accept = "image/*"
404+ ref = { imgInitialsRef }
405+ hidden
406+ />
375407 < div >
376- { isInitials ? (
408+ { imgInitials ? (
377409 < div className = "intialSignature relative border-[1px] border-[#888] rounded-box overflow-hidden" >
378410 < img
379411 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"
386414 />
387415 </ div >
388416 ) : (
@@ -393,9 +421,7 @@ const ManageSign = () => {
393421 className : "intialSignature rounded-box"
394422 } }
395423 // backgroundColor="rgb(255, 255, 255)"
396- onEnd = { ( ) =>
397- handleInitialsChange ( initailsRef . current . toDataURL ( ) )
398- }
424+ onEnd = { ( ) => handleInitialsChange ( ) }
399425 dotSize = { 1 }
400426 />
401427 ) }
@@ -418,6 +444,7 @@ const ManageSign = () => {
418444 ? "2px solid black"
419445 : "2px solid white"
420446 } }
447+ className = "fa-light fa-pen-nib"
421448 onClick = { ( ) => {
422449 if ( key === 0 ) {
423450 setInitialPen ( "blue" ) ;
@@ -428,7 +455,6 @@ const ManageSign = () => {
428455 }
429456 } }
430457 key = { key }
431- className = "fa-light fa-pen-nib"
432458 width = { 20 }
433459 height = { 20 }
434460 > </ i >
@@ -437,13 +463,22 @@ const ManageSign = () => {
437463 </ div >
438464 ) }
439465 </ div >
440- < div >
466+ < div className = "flex flex-row gap-1" >
441467 < div
442468 type = "button"
443469 className = "op-link text-sm md:text-base mr-1"
444- onClick = { ( ) => handleClearInitials ( ) }
470+ onClick = { ( ) => handleUploadInitials ( ) }
445471 >
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 >
447482 </ div >
448483 </ div >
449484 </ div >
0 commit comments