1- import { Component , OnInit } from '@angular/core' ;
1+ import { Component , OnDestroy , OnInit } from '@angular/core' ;
22import { ActivatedRoute , Router } from '@angular/router' ;
33
44import { Subscription } from 'rxjs' ;
55
6+ import { FileService } from '@services/file.service' ;
67import { FolderService } from '@services/folder.service' ;
78
9+ import { File } from '@models/file.model' ;
810import { Folder } from '@models/folder.model' ;
911
1012import { isMongoId } from '@utils/mongo.util' ;
@@ -13,19 +15,34 @@ import { isMongoId } from '@utils/mongo.util';
1315 templateUrl : './folder.component.html' ,
1416 styleUrls : [ './folder.component.scss' ]
1517} )
16- export class FolderComponent implements OnInit {
18+ export class FolderComponent implements OnInit , OnDestroy {
1719 folder : Folder ;
1820 folderCreatedSubscripion : Subscription ;
21+ filesUploadedSubscription : Subscription ;
22+ loading = false ;
23+ allFiles : ( Folder | File ) [ ] ;
24+
1925 constructor (
2026 private activatedRoute : ActivatedRoute ,
2127 private router : Router ,
22- private folderService : FolderService
28+ private folderService : FolderService ,
29+ private fileService : FileService ,
2330 ) { }
2431
32+ ngOnDestroy ( ) : void {
33+ this . folderCreatedSubscripion . unsubscribe ( ) ;
34+ this . filesUploadedSubscription . unsubscribe ( ) ;
35+ }
36+
2537 ngOnInit ( ) : void {
2638 this . folderCreatedSubscripion = this . folderService . folderCreated . subscribe ( ( folder ) => {
2739 this . folder . folders . push ( folder ) ;
2840 this . orderFolders ( ) ;
41+ this . mixFilesAndFolders ( ) ;
42+ } ) ;
43+ this . filesUploadedSubscription = this . fileService . filesCreated . subscribe ( ( files : File [ ] ) => {
44+ this . folder . files . push ( ...files ) ;
45+ this . mixFilesAndFolders ( ) ;
2946 } ) ;
3047 this . activatedRoute . params . subscribe ( ( { folderId} ) => {
3148 if ( ! isMongoId ( folderId ) ) return this . router . navigate ( [ '/' ] ) ;
@@ -34,15 +51,23 @@ export class FolderComponent implements OnInit {
3451 }
3552
3653 getFolder ( id : string ) : void {
54+ this . loading = true ;
3755 this . folderService . getFolder ( id ) . subscribe ( {
3856 next : ( folder ) => {
3957 this . folder = folder ;
40- }
58+ this . mixFilesAndFolders ( ) ;
59+ } ,
60+ complete : ( ) => this . loading = false
4161 } ) ;
4262 }
4363
4464 private orderFolders ( ) : void {
4565 this . folder . folders . sort ( ( a , b ) => a . name > b . name ? 1 : - 1 ) ;
4666 }
4767
68+ mixFilesAndFolders ( ) : void {
69+ const { folders, files } = this . folder ;
70+ this . allFiles = [ ...folders , ...files ] ;
71+ }
72+
4873}
0 commit comments