1
- import { Component } from '@angular/core' ;
1
+ import { Component , OnDestroy } from '@angular/core' ;
2
2
import { DSMService } from '../services/dsm.service' ;
3
3
import { finalize } from 'rxjs/operators' ;
4
4
import { SessionService } from '../services/session.service' ;
5
5
import { MatDialog } from '@angular/material/dialog' ;
6
6
import { LoadingModalComponent } from '../modals/loading-modal.component' ;
7
- import { HttpErrorResponse } from '@angular/common/http' ;
7
+ import { HttpErrorResponse , HttpResponse } from '@angular/common/http' ;
8
+ import { Subject , takeUntil } from 'rxjs' ;
8
9
9
- enum UploadStatus {
10
+ enum RequestStatus {
10
11
SUCCESS ,
11
12
FAIL ,
12
13
DEFAULT
@@ -17,18 +18,24 @@ enum UploadStatus {
17
18
templateUrl : 'oncHistoryUpload.component.html' ,
18
19
styleUrls : [ 'oncHistoryUpload.component.scss' ]
19
20
} )
20
- export class OncHistoryUploadComponent {
21
- public uploadStatus : UploadStatus = UploadStatus . DEFAULT ;
21
+ export class OncHistoryUploadComponent implements OnDestroy {
22
+ public requestStatus : RequestStatus = RequestStatus . DEFAULT ;
22
23
public errorMessage : string ;
23
24
public selectedTextFile : File ;
24
25
26
+ private subscriptionSubject = new Subject < void > ( ) ;
27
+
25
28
constructor ( private readonly dsmService : DSMService ,
26
29
private readonly session : SessionService ,
27
30
private readonly dialog : MatDialog ) { }
28
31
32
+ ngOnDestroy ( ) : void {
33
+ this . subscriptionSubject . next ( ) ;
34
+ this . subscriptionSubject . complete ( ) ;
35
+ }
29
36
30
37
upload ( ) : void {
31
- this . uploadStatus = UploadStatus . DEFAULT ;
38
+ this . requestStatus = RequestStatus . DEFAULT ;
32
39
33
40
const tempDialog = this . dialog
34
41
. open ( LoadingModalComponent , { data : { message : 'Uploading, this may take a while' } , disableClose : true } ) ;
@@ -37,17 +44,12 @@ export class OncHistoryUploadComponent {
37
44
this . session . selectedRealm ,
38
45
this . selectedTextFile
39
46
) . pipe (
47
+ takeUntil ( this . subscriptionSubject ) ,
40
48
finalize ( ( ) => tempDialog . close ( ) )
41
49
)
42
50
. subscribe ( {
43
- next : ( ) => this . uploadStatus = UploadStatus . SUCCESS ,
44
- error : ( error : any ) => {
45
- if ( error instanceof HttpErrorResponse ) {
46
- console . log ( error ) ;
47
- this . uploadStatus = UploadStatus . FAIL ;
48
- this . errorMessage = error . error ;
49
- }
50
- } ,
51
+ next : ( ) => this . requestStatus = RequestStatus . SUCCESS ,
52
+ error : ( error : any ) => this . handleError ( error )
51
53
} ) ;
52
54
}
53
55
@@ -57,4 +59,44 @@ export class OncHistoryUploadComponent {
57
59
}
58
60
this . selectedTextFile = file ;
59
61
}
62
+
63
+ public downloadTemplateAndDirectory ( ) : void {
64
+ const tempDialog = this . dialog
65
+ . open ( LoadingModalComponent , { data : { message : 'Downloading... this may take a while' } , disableClose : true } ) ;
66
+
67
+ this . dsmService . downloadOncHistoryTemplateAndDirectory ( this . session . selectedRealm )
68
+ . pipe ( takeUntil ( this . subscriptionSubject ) , finalize ( ( ) => tempDialog . close ( ) ) )
69
+ . subscribe ( {
70
+ next : ( response : HttpResponse < ArrayBuffer > ) => {
71
+ const arrayBuffer = response . body ;
72
+ const contentDisposition = response . headers . get ( 'Content-Disposition' ) ;
73
+ const blob = new Blob ( [ arrayBuffer ] , {
74
+ type : 'application/zip'
75
+ } ) ;
76
+
77
+ const filenameRegex = / f i l e n a m e [ ^ ; = \n ] * = ( ( [ ' " ] ) .* ?\2| [ ^ ; \n ] * ) / ;
78
+ const matches = filenameRegex . exec ( contentDisposition ) ;
79
+ let filename = 'Onc_history.zip' ; // Default filename in case extraction fails
80
+ if ( matches != null && matches [ 1 ] ) {
81
+ filename = matches [ 1 ] . replace ( / [ ' " ] / g, '' ) ;
82
+ }
83
+
84
+ const url = window . URL . createObjectURL ( blob ) ;
85
+ const link = document . createElement ( 'a' ) ;
86
+ link . href = url ;
87
+ link . download = filename ;
88
+
89
+ link . click ( ) ;
90
+ window . URL . revokeObjectURL ( url ) ;
91
+ } ,
92
+ error : ( error : any ) => this . handleError ( error )
93
+ } ) ;
94
+ }
95
+
96
+ private handleError ( error : any ) : void {
97
+ if ( error instanceof HttpErrorResponse ) {
98
+ this . requestStatus = RequestStatus . FAIL ;
99
+ this . errorMessage = error . error ;
100
+ }
101
+ }
60
102
}
0 commit comments