1- import { Component , ElementRef , OnInit , ViewChild } from "@angular/core" ;
1+ import {
2+ Component ,
3+ ElementRef ,
4+ Inject ,
5+ OnInit ,
6+ ViewChild ,
7+ } from "@angular/core" ;
28import { EditComponent } from "../../../core/entity/default-datatype/edit-component" ;
39import { DynamicComponent } from "../../../core/config/dynamic-components/dynamic-component.decorator" ;
410import { AlertService } from "../../../core/alerts/alert.service" ;
@@ -14,6 +20,8 @@ import { MatTooltipModule } from "@angular/material/tooltip";
1420import { MatButtonModule } from "@angular/material/button" ;
1521import { FontAwesomeModule } from "@fortawesome/angular-fontawesome" ;
1622import { ErrorHintComponent } from "../../../core/common-components/error-hint/error-hint.component" ;
23+ import { NotAvailableOfflineError } from "../../../core/session/not-available-offline.error" ;
24+ import { NAVIGATOR_TOKEN } from "../../../utils/di-tokens" ;
1725
1826/**
1927 * This component should be used as a `editComponent` when a property should store files.
@@ -47,6 +55,7 @@ export class EditFileComponent extends EditComponent<string> implements OnInit {
4755 protected fileService : FileService ,
4856 private alertService : AlertService ,
4957 private entityMapper : EntityMapperService ,
58+ @Inject ( NAVIGATOR_TOKEN ) protected navigator : Navigator ,
5059 ) {
5160 super ( ) ;
5261 }
@@ -99,18 +108,34 @@ export class EditFileComponent extends EditComponent<string> implements OnInit {
99108 }
100109
101110 private handleError ( err ) {
102- Logging . error ( "Failed uploading file: " + JSON . stringify ( err ) ) ;
103-
104- let errorMessage = $localize `:File Upload Error Message:Failed uploading file. Please try again.` ;
111+ let errorMessage : string ;
105112 if ( err ?. status === 413 ) {
106113 errorMessage = $localize `:File Upload Error Message:File too large. Usually files up to 5 MB are supported.` ;
114+ } else if ( err instanceof NotAvailableOfflineError ) {
115+ errorMessage = $localize `:File Upload Error Message:Changes to file attachments are not available offline.` ;
116+ } else {
117+ Logging . error ( "Failed to update file: " + JSON . stringify ( err ) ) ;
118+ errorMessage = $localize `:File Upload Error Message:Failed to update file attachment. Please try again.` ;
107119 }
108120 this . alertService . addDanger ( errorMessage ) ;
109121
122+ return this . revertEntityChanges ( ) ;
123+ }
124+
125+ private async revertEntityChanges ( ) {
126+ // ensure we have latest _rev of entity
127+ this . entity = await this . entityMapper . load (
128+ this . entity . getConstructor ( ) ,
129+ this . entity . getId ( ) ,
130+ ) ;
131+
110132 // Reset entity to how it was before
111133 this . entity [ this . formControlName ] = this . initialValue ;
112134 this . formControl . setValue ( this . initialValue ) ;
113- return this . entityMapper . save ( this . entity ) ;
135+
136+ await this . entityMapper . save ( this . entity ) ;
137+
138+ this . resetFile ( ) ;
114139 }
115140
116141 formClicked ( ) {
@@ -136,15 +161,16 @@ export class EditFileComponent extends EditComponent<string> implements OnInit {
136161 }
137162
138163 protected deleteExistingFile ( ) {
139- this . fileService
140- . removeFile ( this . entity , this . formControlName )
141- . subscribe ( ( ) => {
164+ this . fileService . removeFile ( this . entity , this . formControlName ) . subscribe ( {
165+ error : ( err ) => this . handleError ( err ) ,
166+ complete : ( ) => {
142167 this . alertService . addInfo (
143168 $localize `:Message for user:File "${ this . initialValue } " deleted` ,
144169 ) ;
145170 this . initialValue = undefined ;
146171 this . removeClicked = false ;
147- } ) ;
172+ } ,
173+ } ) ;
148174 }
149175
150176 protected resetFile ( ) {
0 commit comments