@@ -8,15 +8,14 @@ import {
88} from '@angular/core' ;
99import { MatPaginator } from '@angular/material/paginator' ;
1010import { MatSort } from '@angular/material/sort' ;
11- import { MatTableDataSource } from '@angular/material/table' ;
1211import { ActivatedRoute } from '@angular/router' ;
1312import { environment } from '@environments/environment' ;
1413import { TranslateService } from '@ngx-translate/core' ;
1514import { DeleteDialogService } from '@shared/components/delete-dialog/delete-dialog.service' ;
16- import { tableSorter } from '@shared/helpers/table-sorting.helper' ;
1715import { MeService } from '@shared/services/me.service' ;
1816import { SnackService } from '@shared/services/snack.service' ;
19- import { Subscription } from 'rxjs' ;
17+ import { merge , Observable , Subscription , of as observableOf } from 'rxjs' ;
18+ import { catchError , map , startWith , switchMap } from 'rxjs/operators' ;
2019import { Multicast , MulticastData } from '../multicast.model' ;
2120import { MulticastService } from '../multicast.service' ;
2221
@@ -30,15 +29,12 @@ export class MulticastTableComponent
3029 @ViewChild ( MatPaginator ) paginator : MatPaginator ;
3130 @ViewChild ( MatSort ) sort : MatSort ;
3231 displayedColumns : string [ ] = [ 'groupName' , 'groupType' , 'menu' ] ;
33- dataSource = new MatTableDataSource < Multicast > ( ) ;
34- multicasts : Multicast [ ] ;
32+ multicasts : Multicast [ ] = [ ] ;
3533 resultsLength = 0 ;
3634 public canEdit = false ;
3735 @Input ( ) isLoadingResults : boolean = true ;
3836 public pageSize = environment . tablePageSize ;
39- @Input ( ) pageLimit : number ;
4037 public pageOffset = 0 ;
41- public pageTotal : number ;
4238 public applicationId : number ;
4339
4440 private multicastSubscription : Subscription ;
@@ -57,38 +53,52 @@ export class MulticastTableComponent
5753
5854 ngOnInit ( ) : void {
5955 this . applicationId = + Number ( this . route . parent . snapshot . paramMap . get ( 'id' ) ) ;
60- this . getMulticasts ( ) ;
6156 this . canEdit = this . meService . canWriteInTargetOrganization ( ) ;
6257 }
6358
6459 ngAfterViewInit ( ) {
65- this . dataSource . paginator = this . paginator ;
66- this . dataSource . sort = this . sort ;
60+ // If the user changes the sort order, reset back to the first page.
61+ this . sort . sortChange . subscribe ( ( ) => ( this . paginator . pageIndex = 0 ) ) ;
62+
63+ merge ( this . sort . sortChange , this . paginator . page )
64+ . pipe (
65+ startWith ( { } ) ,
66+ switchMap ( ( ) => {
67+ this . isLoadingResults = true ;
68+ const multicasts = this . getMulticasts (
69+ this . sort . active ,
70+ this . sort . direction
71+ ) ;
72+ //TODO::: Snack here
73+ return multicasts ;
74+ } ) ,
75+ map ( ( data ) => {
76+ // Flip flag to show that loading has finished.
77+ this . isLoadingResults = false ;
78+ this . resultsLength = data . count ;
79+
80+ return data . data ;
81+ } ) ,
82+ catchError ( ( ) => {
83+ this . isLoadingResults = false ;
84+ return observableOf ( [ ] ) ;
85+ } )
86+ )
87+ . subscribe ( ( data ) => ( this . multicasts = data ) ) ;
6788 }
6889
69- getMulticasts ( ) : void {
90+ getMulticasts (
91+ orderByColumn : string ,
92+ orderByDirection : string
93+ ) : Observable < MulticastData > {
7094 if ( this . applicationId ) {
71- this . multicastSubscription = this . multicastService
72- . getMulticastsByApplicationId (
73- this . pageLimit ,
74- this . pageOffset * this . pageLimit ,
75- this . applicationId
76- )
77- . subscribe ( ( multicasts : MulticastData ) => {
78- this . multicasts = multicasts . data ;
79- this . dataSource = new MatTableDataSource < Multicast > ( this . multicasts ) ; // these lines of code is inspired/taken from datatarget.
80- this . dataSource . paginator = this . paginator ;
81- this . dataSource . sort = this . sort ;
82- this . dataSource . sortingDataAccessor = tableSorter ;
83- this . isLoadingResults = false ;
84- if ( this . pageLimit ) {
85- this . pageTotal = Math . ceil ( multicasts . count / this . pageLimit ) ;
86- }
87- if ( multicasts . ok === false ) {
88- // ok is only defined when it's an error.
89- this . snackService . showLoadFailSnack ( ) ;
90- }
91- } ) ;
95+ return this . multicastService . getMulticastsByApplicationId (
96+ this . paginator . pageSize ,
97+ this . paginator . pageIndex * this . paginator . pageSize ,
98+ orderByDirection ,
99+ orderByColumn ,
100+ this . applicationId
101+ ) ;
92102 }
93103 }
94104
@@ -101,7 +111,11 @@ export class MulticastTableComponent
101111 this . multicastService . delete ( multicast . id ) . subscribe ( ( response ) => {
102112 if ( response . ok && response . body . affected > 0 ) {
103113 // if deleted succesfully, get the new array of multicasts and show a succesful snack.
104- this . getMulticasts ( ) ;
114+ this . paginator . page . emit ( {
115+ pageIndex : this . paginator . pageIndex ,
116+ pageSize : this . paginator . pageSize ,
117+ length : this . resultsLength ,
118+ } ) ;
105119 this . snackService . showDeletedSnack ( ) ;
106120 } else {
107121 this . snackService . showFailSnack ( ) ;
0 commit comments