Skip to content

Commit

Permalink
load correct total counts after search; fix #SNRGY-2909
Browse files Browse the repository at this point in the history
  • Loading branch information
hahahannes committed Nov 1, 2023
1 parent 2a22395 commit c80b03c
Show file tree
Hide file tree
Showing 22 changed files with 321 additions and 164 deletions.
32 changes: 21 additions & 11 deletions src/app/modules/devices/device-groups/device-groups.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
*/

import {AfterViewInit, Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {forkJoin, Observable, Subscription} from 'rxjs';
import {forkJoin, Observable, Subscription, map} from 'rxjs';
import {MatSnackBar} from '@angular/material/snack-bar';
import {Router} from '@angular/router';
import {DialogsService} from '../../../core/services/dialogs.service';
import {DeviceGroupsPermSearchModel} from './shared/device-groups-perm-search.model';
import {DeviceGroupsService} from './shared/device-groups.service';
import {MatTableDataSource} from '@angular/material/table';
import {MatSort, Sort, SortDirection} from '@angular/material/sort';
import {UntypedFormControl} from '@angular/forms';
import {debounceTime} from 'rxjs/operators';
import { SelectionModel } from '@angular/cdk/collections';
import { MatPaginator } from '@angular/material/paginator';
import { SearchbarService } from 'src/app/core/components/searchbar/shared/searchbar.service';
Expand Down Expand Up @@ -68,11 +66,16 @@ export class DeviceGroupsComponent implements OnInit, OnDestroy, AfterViewInit {
) {}

ngOnInit() {
this.deviceGroupsService.getTotalCountOfDeviceGroups().subscribe(totalCount => this.totalCount = totalCount)
this.initSearch();
this.checkAuthorization()
}

getTotalCount(): Observable<number> {
return this.deviceGroupsService.getTotalCountOfDeviceGroups(this.searchText).pipe(
map(totalCount => this.totalCount = totalCount)
)
}

ngAfterViewInit(): void {
this.paginator.page.subscribe(()=>{
this.pageSize = this.paginator.pageSize
Expand Down Expand Up @@ -107,7 +110,7 @@ export class DeviceGroupsComponent implements OnInit, OnDestroy, AfterViewInit {
private initSearch() {
this.searchSub = this.searchbarService.currentSearchText.subscribe((searchText: string) => {
this.searchText = searchText;
this.reload();
this.reload()
});
}

Expand Down Expand Up @@ -163,24 +166,31 @@ export class DeviceGroupsComponent implements OnInit, OnDestroy, AfterViewInit {
this.getDeviceGroups();
}

private getDeviceGroups() {
private getDeviceGroups(): Observable<DeviceGroupsPermSearchModel[]> {
let query = this.deviceGroupsService.getDeviceGroups(this.searchText, this.pageSize, this.offset, this.sortBy, this.sortDirection);
if(this.hideGenerated) {
query = this.deviceGroupsService.getDeviceGroupsWithoutGenerated(this.searchText, this.pageSize, this.offset, this.sortBy, this.sortDirection);
}

query.subscribe((deviceGroups: DeviceGroupsPermSearchModel[]) => {
this.dataSource.data = deviceGroups;
this.ready = true;
});
return query.pipe(
map((deviceGroups: DeviceGroupsPermSearchModel[]) => {
this.dataSource.data = deviceGroups;
return deviceGroups
}
)
)
}

public reload() {
this.offset = 0;
this.pageSize = 20;
this.ready = false;
this.selectionClear();
this.getDeviceGroups();

var jobs = [this.getDeviceGroups(), this.getTotalCount()]
forkJoin(jobs).subscribe(_ => {
this.ready = true;
})
}

deleteMultipleItems() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {HttpClient, HttpParams} from '@angular/common/http';
import {ErrorHandlerService} from '../../../../core/services/error-handler.service';
import {Observable} from 'rxjs';
import {environment} from '../../../../../environments/environment';
Expand Down Expand Up @@ -245,9 +245,12 @@ export class DeviceGroupsService {
);
}

getTotalCountOfDeviceGroups(): Observable<any> {
getTotalCountOfDeviceGroups(searchText: string): Observable<any> {
const options = searchText ?
{ params: new HttpParams().set('search', searchText) } : {};

return this.http
.get(environment.permissionSearchUrl + '/v3/total/device-groups')
.get(environment.permissionSearchUrl + '/v3/total/device-groups', options)
.pipe(
catchError(
this.errorHandlerService.handleError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {NetworksService} from '../networks/shared/networks.service';
import {MatSort, Sort, SortDirection} from '@angular/material/sort';
import { SelectionModel } from '@angular/cdk/collections';
import { MatPaginator } from '@angular/material/paginator';
import { forkJoin, Observable, Subscription } from 'rxjs';
import { forkJoin, Observable, map, Subscription, of } from 'rxjs';
import { SearchbarService } from 'src/app/core/components/searchbar/shared/searchbar.service';

export interface DeviceInstancesRouterState {
Expand Down Expand Up @@ -108,12 +108,17 @@ export class DeviceInstancesComponent implements OnInit, AfterViewInit {
userHasDeleteAuthorization: boolean = false

ngOnInit(): void {
this.deviceInstancesService.getTotalCountOfDevices().subscribe(totalCount => this.totalCount = totalCount);
this.loadFilterOptions();
this.initSearch(); // does automatically load data on first page load
this.checkAuthorization()
}

getTotalCount(): Observable<number> {
return this.deviceInstancesService.getTotalCountOfDevices(this.searchText).pipe(
map(totalCount => this.totalCount = totalCount)
)
}

ngAfterViewInit(): void {
this.paginator.page.subscribe(()=>{
this.pageSize = this.paginator.pageSize;
Expand Down Expand Up @@ -159,89 +164,108 @@ export class DeviceInstancesComponent implements OnInit, AfterViewInit {
this.reload();
}

private loadDevicesOfNetwork() {
private loadDevicesOfNetwork(): Observable<DeviceInstancesModel[]> {
if(this.routerNetwork) {
this.selectedTag = this.routerNetwork.name;
this.selectedTagTransformed = this.routerNetwork.name;
this.deviceInstancesService
return this.deviceInstancesService
.getDeviceInstancesByHub(
this.routerNetwork.id,
this.pageSize,
this.offset,
this.sortBy,
this.sortDirection == "desc",
)
.subscribe((deviceInstances: DeviceInstancesModel[]) => {
this.setDevices(deviceInstances);
});
.pipe(
map((deviceInstances: DeviceInstancesModel[]) => {
this.setDevices(deviceInstances)
return deviceInstances
})
)
}
return of([])

}

private loadDevicesOfDeviceType() {
private loadDevicesOfDeviceType(): Observable<DeviceInstancesModel[]> {
if(this.routerDeviceType) {
this.selectedTag = this.routerDeviceType.name;
this.selectedTagTransformed = this.routerDeviceType.name;
this.deviceInstancesService
return this.deviceInstancesService
.getDeviceInstancesByDeviceTypes(
[this.routerDeviceType.id],
this.pageSize,
this.offset,
this.sortBy,
this.sortDirection == "desc"
)
.subscribe((deviceInstances) => {
this.setDevices(deviceInstances);
});
.pipe(
map(deviceInstances => {
this.setDevices(deviceInstances);
return deviceInstances
})
)
}
return of([])

}

private loadDevicesByIds() {
private loadDevicesByIds(): Observable<DeviceInstancesModel[]> {
if(this.routerDeviceIds) {
this.deviceInstancesService.getDeviceInstancesByIds(this.routerDeviceIds, this.pageSize, this.offset).subscribe((deviceInstances) => {
this.setDevices(deviceInstances);
});
return this.deviceInstancesService.getDeviceInstancesByIds(this.routerDeviceIds, this.pageSize, this.offset).pipe(
map(deviceInstances => {
this.setDevices(deviceInstances);
return deviceInstances
})
)
}
return of([])

}

private loadDevicesByLocation() {
private loadDevicesByLocation(): Observable<DeviceInstancesModel[]> {
if(this.routerLocation !== null) {
this.selectedTag = this.routerLocation.id;
this.selectedTagTransformed = this.routerLocation.name;
this.deviceInstancesService.getDeviceInstancesByLocation(this.routerLocation.id, this.pageSize, this.offset, this.sortBy, this.sortDirection == "desc").subscribe((deviceInstances) => {
this.setDevices(deviceInstances);
});
return this.deviceInstancesService.getDeviceInstancesByLocation(this.routerLocation.id, this.pageSize, this.offset, this.sortBy, this.sortDirection == "desc").pipe(
map(deviceInstances => {
this.setDevices(deviceInstances);
return deviceInstances
})
)
}
return of([])
}

private load() {
this.ready = false;

private load(): Observable<DeviceInstancesModel[]> {
if (this.routerNetwork !== null) {
this.loadDevicesOfNetwork()
return this.loadDevicesOfNetwork()
} else if (this.routerDeviceType !== null) {
this.loadDevicesOfDeviceType()
return this.loadDevicesOfDeviceType()
} else if (this.routerDeviceIds !== null) {
this.loadDevicesByIds()
return this.loadDevicesByIds()
} else if (this.routerLocation !== null) {
this.loadDevicesByLocation()
return this.loadDevicesByLocation()
} else {
this.deviceInstancesService
return this.deviceInstancesService
.getDeviceInstances(
this.pageSize,
this.offset,
this.sortBy,
this.sortDirection == "desc",
this.searchText,
)
.subscribe((deviceInstances: DeviceInstancesModel[]) => {
this.setDevices(deviceInstances);
});
.pipe(
map((deviceInstances: DeviceInstancesModel[]) => {
this.setDevices(deviceInstances);
return deviceInstances
})
);
}
}

private setDevices(instances: DeviceInstancesModel[]) {
this.dataSource.data = instances;
this.ready = true;
}

tagRemoved(): void {
Expand Down Expand Up @@ -303,7 +327,8 @@ export class DeviceInstancesComponent implements OnInit, AfterViewInit {
this.ready = false;
this.pageSize = 20;
this.selectionClear();
this.load();

forkJoin(this.load(), this.getTotalCount()).subscribe(_ => this.ready = true)
}

showInfoOfDevice(device: DeviceInstancesModel): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {HttpClient, HttpParams} from '@angular/common/http';
import {ErrorHandlerService} from '../../../../core/services/error-handler.service';
import {environment} from '../../../../../environments/environment';
import {catchError, map, reduce, share, concatMap} from 'rxjs/operators';
Expand Down Expand Up @@ -438,9 +438,12 @@ export class DeviceInstancesService {
}
}

getTotalCountOfDevices(): Observable<any> {
getTotalCountOfDevices(searchText: string): Observable<any> {
const options = searchText ?
{ params: new HttpParams().set('search', searchText) } : {};

return this.http
.get(environment.permissionSearchUrl + '/v3/total/devices')
.get(environment.permissionSearchUrl + '/v3/total/devices', options)
.pipe(
catchError(
this.errorHandlerService.handleError(
Expand Down
28 changes: 19 additions & 9 deletions src/app/modules/devices/locations/locations.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import {AfterViewInit, Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {forkJoin, Observable, Subscription} from 'rxjs';
import {forkJoin, Observable, Subscription, map} from 'rxjs';
import {MatSnackBar} from '@angular/material/snack-bar';
import {Router} from '@angular/router';
import {DialogsService} from '../../../core/services/dialogs.service';
Expand Down Expand Up @@ -64,7 +64,6 @@ export class LocationsComponent implements OnInit, OnDestroy, AfterViewInit {
) {}

ngOnInit() {
this.locationsService.getTotalCountOfLocations().subscribe(totalCount => this.totalCount = totalCount)
this.initSearch();
this.checkAuthorization()
}
Expand All @@ -73,6 +72,12 @@ export class LocationsComponent implements OnInit, OnDestroy, AfterViewInit {
this.searchSub.unsubscribe();
}

getTotalCounts(): Observable<number> {
return this.locationsService.getTotalCountOfLocations(this.searchText).pipe(
map((totalCount: number) => this.totalCount = totalCount)
)
}

ngAfterViewInit(): void {
this.paginator.page.subscribe(()=>{
this.pageSize = this.paginator.pageSize
Expand Down Expand Up @@ -145,13 +150,15 @@ export class LocationsComponent implements OnInit, OnDestroy, AfterViewInit {
return false;
}

private getLocations() {
this.locationsService
private getLocations(): Observable<LocationModel[]> {
return this.locationsService
.searchLocations(this.searchText, this.pageSize, this.offset, this.sortBy, this.sortDirection)
.subscribe((locations: LocationModel[]) => {
this.dataSource.data = locations;
this.ready = true;
});
.pipe(
map((locations: LocationModel[]) => {
this.dataSource.data = locations;
return locations
})
)
}

private reloadLocations() {
Expand All @@ -165,7 +172,10 @@ export class LocationsComponent implements OnInit, OnDestroy, AfterViewInit {
this.pageSize = 20;
this.ready = false;
this.selectionClear()
this.getLocations();

forkJoin([this.getLocations(), this.getTotalCounts()]).subscribe(_ => {
this.ready = true;
})
}

isAllSelected() {
Expand Down
9 changes: 6 additions & 3 deletions src/app/modules/devices/locations/shared/locations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpParams } from '@angular/common/http';
import { ErrorHandlerService } from '../../../../core/services/error-handler.service';
import { MatSnackBar } from '@angular/material/snack-bar';
import { Observable } from 'rxjs';
Expand Down Expand Up @@ -100,9 +100,12 @@ export class LocationsService {
.pipe(catchError(this.errorHandlerService.handleError(LocationsService.name, 'updateLocation', null)));
}

getTotalCountOfLocations(): Observable<any> {
getTotalCountOfLocations(searchText: string): Observable<any> {
const options = searchText ?
{ params: new HttpParams().set('search', searchText) } : {};

return this.http
.get(environment.permissionSearchUrl + '/v3/total/locations')
.get(environment.permissionSearchUrl + '/v3/total/locations', options)
.pipe(
catchError(
this.errorHandlerService.handleError(
Expand Down
Loading

0 comments on commit c80b03c

Please sign in to comment.