source: Sources/frontend/src/app/materials/materials.component.ts@ 8423429

Last change on this file since 8423429 was 8423429, checked in by AngelNasev <angel.nasev@…>, 17 months ago

Add backend and frontend projects

  • Property mode set to 100644
File size: 2.7 KB
Line 
1import {Component, OnInit} from '@angular/core';
2import {ActiveUserInterface} from "../ActiveUserInterface";
3import {LoginService} from "../login.service";
4import {MaterialsService} from "../materials.service";
5import {HttpErrorResponse, HttpEvent, HttpEventType} from "@angular/common/http";
6import {saveAs} from "file-saver";
7import {ActivatedRoute} from "@angular/router";
8
9@Component({
10 selector: 'app-materials',
11 templateUrl: './materials.component.html',
12 styleUrls: ['./materials.component.css']
13})
14export class MaterialsComponent implements OnInit{
15
16 activeUser: ActiveUserInterface | undefined;
17 courseId : number | undefined;
18 files: string[] | any = []
19 fileStatus = {status: '', requestType: '', percent: 0};
20
21 constructor(private loginService: LoginService,
22 private materialService: MaterialsService,
23 private _router: ActivatedRoute) {
24 }
25 ngOnInit() {
26 this.activeUser = this.loginService.activeUser;
27 this.courseId = +this._router.snapshot.paramMap.get("id");
28 this.materialService.findAllMaterialsForCourse(this.courseId)
29 .subscribe(response => {
30 console.log("files", this.files)
31 this.files = response
32 })
33 }
34
35 onDownloadFile(filename: string): void {
36 this.materialService.download(filename).subscribe(
37 event => {
38 console.log(event);
39 this.resportProgress(event);
40 },
41 (error: HttpErrorResponse) => {
42 console.log(error);
43 }
44 );
45 }
46
47 private resportProgress(httpEvent: HttpEvent<string[] | Blob>): void {
48 switch (httpEvent.type) {
49 case HttpEventType.UploadProgress:
50 this.updateStatus(httpEvent.loaded, httpEvent.total!, 'Uploading... ');
51 break;
52 case HttpEventType.DownloadProgress:
53 this.updateStatus(httpEvent.loaded, httpEvent.total!, 'Downloading... ');
54 break;
55 case HttpEventType.ResponseHeader:
56 console.log('Header returned', httpEvent);
57 break;
58 case HttpEventType.Response:
59 if (httpEvent.body instanceof Array) {
60 this.fileStatus.status = 'done';
61 for (const filename of httpEvent.body) {
62 this.files.unshift(filename);
63 }
64 } else {
65 saveAs(new Blob([httpEvent.body!],
66 { type: `${httpEvent.headers.get('Content-Type')};charset=utf-8`}),
67 httpEvent.headers.get('File-Name'));
68 }
69 this.fileStatus.status = 'done';
70 break;
71 default:
72 console.log(httpEvent);
73 break;
74
75 }
76 }
77
78 private updateStatus(loaded: number, total: number, requestType: string): void {
79 this.fileStatus.status = 'progress';
80 this.fileStatus.requestType = requestType;
81 this.fileStatus.percent = Math.round(100 * loaded / total);
82 }
83
84}
Note: See TracBrowser for help on using the repository browser.