forked from kukhariev/ngx-uploadx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathon-push.component.ts
48 lines (42 loc) · 1.44 KB
/
on-push.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { ChangeDetectionStrategy, Component, OnDestroy } from '@angular/core';
import { Uploader, UploadState, UploadxOptions, UploadxService } from 'ngx-uploadx';
import { Observable } from 'rxjs';
import { environment } from '../../environments/environment';
import { AuthService } from '../auth.service';
import { injectDigestHeader } from '../digest';
@Component({
selector: 'app-on-push',
templateUrl: './on-push.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class OnPushComponent implements OnDestroy {
state$: Observable<UploadState>;
uploads$: Observable<Uploader[]>;
options: UploadxOptions = {
endpoint: `${environment.api}/files?uploadType=uploadx`,
prerequest: injectDigestHeader,
authorize: (req, token) => {
token && (req.headers.Authorization = `Token ${token}`);
return req;
}
};
constructor(private uploadService: UploadxService, private auth: AuthService) {
this.uploads$ = this.uploadService.connect(this.options);
this.state$ = this.uploadService.events;
this.auth.getToken().subscribe(token => {
this.uploadService.control({ token });
});
}
ngOnDestroy(): void {
this.uploadService.disconnect();
}
cancelAll(): void {
this.uploadService.control({ action: 'cancel' });
}
pauseAll(): void {
this.uploadService.control({ action: 'pause' });
}
uploadAll(): void {
this.uploadService.control({ action: 'upload' });
}
}