@@ -639,13 +639,19 @@ export class PostEditComponent extends BaseComponent implements OnInit, OnChange
639639 const originalValue = this . post ?. post_content [ 0 ] ?. fields . filter (
640640 ( fieldValue : { key : string | number } ) => fieldValue . key === field . key ,
641641 ) [ 0 ] ;
642- if ( this . form . value [ field . key ] ?. upload && this . form . value [ field . key ] ?. photo ) {
642+ let formValue = this . form . value [ field . key ] ;
643+ if ( Array . isArray ( formValue ) ) formValue = formValue [ 0 ] ;
644+
645+ // No image uploaded in field at all
646+ if ( ! formValue ) value . value = [ ] ;
647+ // Image uploaded
648+ else if ( formValue . upload && formValue . photo ) {
643649 try {
644650 this . maxSizeError = false ;
645- if ( this . maxImageSize > this . form . value [ field . key ] . photo . size ) {
651+ if ( this . maxImageSize > formValue . photo . size ) {
646652 const uploadObservable = this . mediaService . uploadFile (
647- this . form . value [ field . key ] ? .photo ,
648- this . form . value [ field . key ] ? .caption ,
653+ formValue . photo ,
654+ formValue . caption ,
649655 ) ;
650656 const response : any = await lastValueFrom ( uploadObservable ) ;
651657 value . value = [ response . result . id ] ;
@@ -656,46 +662,46 @@ export class PostEditComponent extends BaseComponent implements OnInit, OnChange
656662 } catch ( error : any ) {
657663 throw new Error ( `Error uploading file: ${ error . message } ` ) ;
658664 }
659- } else if ( this . form . value [ field . key ] ?. delete && this . form . value [ field . key ] ?. id ) {
665+ // Image deleted
666+ } else if ( formValue . delete && formValue . id ) {
660667 try {
661- const deleteObservable = this . mediaService . delete (
662- this . form . value [ field . key ] ?. id ,
663- ) ;
668+ const deleteObservable = this . mediaService . delete ( formValue . id ) ;
664669 await lastValueFrom ( deleteObservable ) ;
665670 value . value = [ ] ;
666671 } catch ( error : any ) {
667672 throw new Error ( `Error deleting file: ${ error . message } ` ) ;
668673 }
674+ // Caption updated
669675 } else if (
670676 originalValue ?. value ?. length > 0 &&
671- originalValue . value [ 0 ] . caption !== value . value . caption
677+ originalValue . value [ 0 ] . caption !== formValue . caption
672678 ) {
673679 try {
674680 const captionObservable = await this . mediaService . updateCaption (
675681 originalValue . value [ 0 ] . value ,
676- value . value . caption ,
682+ formValue . caption ,
677683 ) ;
678684 await lastValueFrom ( captionObservable ) ;
679685 value . value = [ originalValue . value [ 0 ] . value ] ;
680686 } catch ( error : any ) {
681687 throw new Error ( `Error updating caption: ${ error . message } ` ) ;
682688 }
689+ // Nothing updated
683690 } else {
684- if ( this . form . value [ field . key ] ) value . value = [ this . form . value [ field . key ] ?. id ] ;
685- else value . value = [ ] ;
691+ value . value = [ formValue . id ] ;
686692 }
687693 break ;
688694 case 'image' :
689695 value . value =
690- this . form . value [ field . key ] ?. map ( ( formValue : any ) => formValue . value ) || [ ] ;
696+ this . form . value [ field . key ] ?. map ( ( fieldValue : any ) => fieldValue . value ) || [ ] ;
691697 break ;
692698 case 'audio' :
693699 value . value =
694- this . form . value [ field . key ] ?. map ( ( formValue : any ) => formValue . value ) || [ ] ;
700+ this . form . value [ field . key ] ?. map ( ( fieldValue : any ) => fieldValue . value ) || [ ] ;
695701 break ;
696702 case 'document' :
697703 value . value =
698- this . form . value [ field . key ] ?. map ( ( formValue : any ) => formValue . value ) || [ ] ;
704+ this . form . value [ field . key ] ?. map ( ( fieldValue : any ) => fieldValue . value ) || [ ] ;
699705 break ;
700706 default :
701707 value . value = this . form . value [ field . key ] || null ;
0 commit comments