@@ -21,7 +21,6 @@ export class ImportEntryComponent implements OnInit, OnDestroy {
2121 protected uploadMessage : string = "Upload File" ;
2222 protected uploadError : boolean = false ;
2323 protected showUploadProgress : boolean = false ;
24- protected uploadProgress : number = 0 ;
2524
2625 protected showStationSelection : boolean = false ;
2726 protected selectedStationId ! : string | null ;
@@ -53,7 +52,6 @@ export class ImportEntryComponent implements OnInit, OnDestroy {
5352 } else {
5453 this . onlyIncludeStationIds = [ ] ;
5554 }
56- console . log ( 'station permission: ' , user . permissions . entryPermissions . stationIds )
5755 } else {
5856 throw new Error ( 'Data entry not allowed' ) ;
5957 }
@@ -86,7 +84,6 @@ export class ImportEntryComponent implements OnInit, OnDestroy {
8684 this . destroy$ . complete ( ) ;
8785 }
8886
89-
9087 protected onFileSelected ( fileInputEvent : any ) : void {
9188 if ( fileInputEvent . target . files . length === 0 ) {
9289 return ;
@@ -100,68 +97,50 @@ export class ImportEntryComponent implements OnInit, OnDestroy {
10097
10198 this . disableUpload = true ;
10299 this . showUploadProgress = true ;
103- this . uploadProgress = 0 ;
104100 this . uploadError = false ;
105- this . uploadMessage = "Uploading file..."
101+ this . uploadMessage = "Uploading and processing file..."
106102
107103 // Get the file and append it as the form data to be sent
108104 const selectedFile = fileInputEvent . target . files [ 0 ] as File ;
109105 const formData = new FormData ( ) ;
110106 formData . append ( 'file' , selectedFile ) ;
111107
112- const params = new HttpParams ( ) ;
113108 let url = `${ this . appConfigService . apiBaseUrl } /observations/upload/${ this . viewSource . id } ` ;
114109 // If station id is provided, append it as a route parameter
115110 if ( this . showStationSelection && this . selectedStationId ) {
116111 url = url + "/" + this . selectedStationId ;
117112 }
118113
119- this . http . post (
120- url ,
121- formData ,
122- {
123- reportProgress : true ,
124- observe : 'events' ,
125- params : params
126- } ) . pipe (
127- catchError ( error => {
128- console . log ( "Error returned: " , error ) ;
129- return throwError ( ( ) => new Error ( 'Something bad happened. Please try again later.' ) ) ;
130- } )
131- ) . subscribe ( event => {
132- if ( event . type === HttpEventType . UploadProgress ) {
133- if ( event . total ) {
134- this . uploadProgress = Math . round ( 100 * ( event . loaded / event . total ) ) ;
135- this . uploadMessage = this . uploadProgress < 100 ? "Uploading file..." : "Processing file..." ;
136- }
137- } else if ( event . type === HttpEventType . Response ) {
138- this . disableUpload = false ;
139- // Clear the file input
140- fileInputEvent . target . value = null ;
141-
142- // Reset upload progress
143- this . showUploadProgress = false ;
144- this . uploadProgress = 0 ;
145- this . uploadError = false ;
146-
147- if ( ! event . body ) {
148- this . uploadMessage = "Something went wrong!" ;
149- this . uploadError = true ;
150- return ;
151- }
152-
153- let response : string = ( event . body as any ) . message ;
154- if ( response === "success" ) {
155- this . uploadMessage = "Imported data successfully saved!" ;
156- } else {
157- this . uploadMessage = response ;
158- this . uploadError = true ;
159- }
114+ this . http . post ( url , formData ) . pipe (
115+ take ( 1 ) ,
116+ catchError ( error => {
117+ this . disableUpload = false ;
118+ this . showUploadProgress = false ;
119+ this . uploadMessage = 'Something bad happened. Please try again later.' ;
120+ this . uploadError = true ;
121+ console . log ( "Error returned: " , error ) ;
122+ return error ;
123+ } ) ,
124+ ) . subscribe ( res => {
125+ this . disableUpload = false ;
126+ this . showUploadProgress = false ;
127+ // Clear the file input
128+ fileInputEvent . target . value = null ;
129+
130+ if ( res ) {
131+ let message : string = ( res as any ) . message ;
132+ if ( message === "success" ) {
133+ this . uploadMessage = "File successfully uploaded and processed!" ;
134+ } else {
135+ this . uploadMessage = message ;
136+ this . uploadError = true ;
160137 }
161- } ) ;
138+ } else {
139+ this . uploadMessage = "Something went wrong!" ;
140+ this . uploadError = true ;
141+ }
162142
143+ } ) ;
163144 }
164145
165-
166-
167146}
0 commit comments