Browse Source

refactor(iot-hub): read the hero popup's sections as sections

pull/16148/head
dshvaika 2 months ago
parent
commit
09dbf7b0c0
  1. 13
      ui-ngx/src/app/core/http/iot-hub-api.service.ts
  2. 2
      ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-home.component.html
  3. 51
      ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-home.component.ts
  4. 15
      ui-ngx/src/app/shared/models/iot-hub/iot-hub-version.models.ts

13
ui-ngx/src/app/core/http/iot-hub-api.service.ts

@ -6,7 +6,7 @@ import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { PageData } from '@shared/models/page/page-data';
import { PageLink } from '@shared/models/page/page-link';
import { MpItemVersionGroupedView, MpItemVersionQuery, MpItemVersionView } from '@shared/models/iot-hub/iot-hub-version.models';
import { MpItemVersionQuery, MpItemVersionSection, MpItemVersionView } from '@shared/models/iot-hub/iot-hub-version.models';
import { CreatorView } from '@shared/models/iot-hub/iot-hub-creator.models';
import { IotHubInstalledItem, InstallItemVersionResult, InstallPlan, InstallPlanResult, UpdateItemVersionResult, ItemPublishedVersionInfo } from '@shared/models/iot-hub/iot-hub-installed-item.models';
import { ItemType, ItemTypeFilterInfo, WidgetCategory } from '@shared/models/iot-hub/iot-hub-item.models';
@ -82,19 +82,20 @@ export class IotHubApiService {
}
/**
* The sectioned answer: at most four rows of each item type, every row carrying the size of its
* section. Its own endpoint, which is why this is its own method - it takes no page, page size
* or sort direction, and sending them would be sending what the server does not read.
* The sectioned answer: one section per item type, in the server's order, each holding at most
* four rows and the total behind them. Its own endpoint, which is why this is its own method -
* it takes no page, page size or sort direction, and sending them would be sending what the
* server does not read.
*/
public getPublishedVersionsGrouped(query: MpItemVersionQuery,
config?: IotHubRequestConfig): Observable<PageData<MpItemVersionGroupedView>> {
config?: IotHubRequestConfig): Observable<MpItemVersionSection[]> {
if (query.options.tbVersion == null) {
query.options.tbVersion = tbVersionToInt(env.tbVersion);
}
if (query.options.peOnly == null) {
query.options.peOnly = false;
}
return this.http.get<PageData<MpItemVersionGroupedView>>(
return this.http.get<MpItemVersionSection[]>(
`${this.baseUrl}/api/versions/published/grouped${query.toGroupedQuery()}`,
{ params: this.buildParams(config) }
);

2
ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-home.component.html

@ -72,7 +72,7 @@
<mat-option [disabled]="true" class="tb-search-popup-loading">
<mat-spinner diameter="24"></mat-spinner>
</mat-option>
} @else if (searchResults.length === 0 && searchLoaded) {
} @else if (searchResultGroups.length === 0 && searchLoaded) {
<mat-option [disabled]="true" class="tb-search-popup-no-results">
<div class="tb-search-popup-no-results-content">
<mat-icon>search_off</mat-icon>

51
ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-home.component.ts

@ -7,10 +7,9 @@ import { BreakpointObserver } from '@angular/cdk/layout';
import { forkJoin, of, Subject, Subscription } from 'rxjs';
import { catchError, debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';
import { MediaBreakpoints } from '@shared/models/constants';
import { emptyPageData } from '@shared/models/page/page-data';
import { PageLink } from '@shared/models/page/page-link';
import { Direction, SortOrder } from '@shared/models/page/sort-order';
import { MpItemVersionGroupedView, MpItemVersionQuery, MpItemVersionView } from '@shared/models/iot-hub/iot-hub-version.models';
import { MpItemVersionQuery, MpItemVersionSection, MpItemVersionView } from '@shared/models/iot-hub/iot-hub-version.models';
import { getItemTypeIcon, ItemType, itemTypeTranslations } from '@shared/models/iot-hub/iot-hub-item.models';
import { IotHubInstalledItem } from '@shared/models/iot-hub/iot-hub-installed-item.models';
import { IotHubApiService } from '@core/http/iot-hub-api.service';
@ -35,14 +34,16 @@ interface HeroTypeConfig {
interface SearchResultGroup {
type: ItemType;
items: MpItemVersionGroupedView[];
/** Rows of this type behind the answer, from the response's typeTotal. */
items: MpItemVersionView[];
/** Rows of this type behind the answer, from the section's total. */
total: number;
/** total - items.length, floored at 0. Zero means the header shows no "+N more". */
remaining: number;
}
const SEARCH_GROUP_ORDER: ItemType[] = [
/** The types this popup lays out. A section of anything else is dropped rather than rendered
* without a label - the platform publishes no dashboards. Order comes from the server. */
const SEARCH_GROUP_TYPES: ItemType[] = [
ItemType.DEVICE, ItemType.SOLUTION_TEMPLATE, ItemType.WIDGET,
ItemType.CALCULATED_FIELD, ItemType.ALARM_RULE, ItemType.RULE_CHAIN
];
@ -63,7 +64,6 @@ export class TbIotHubHomeComponent implements OnInit, OnDestroy {
readonly ItemType = ItemType;
searchText = '';
searchResults: MpItemVersionGroupedView[] = [];
searchResultGroups: SearchResultGroup[] = [];
searchLoaded = false;
searchLoading = false;
@ -178,12 +178,11 @@ export class TbIotHubHomeComponent implements OnInit, OnDestroy {
// A failed request must not end the subscription: the interceptor reports it, and the
// panel goes back to an empty answer the next keystroke can replace.
return this.iotHubApiService.getPublishedVersionsGrouped(query, { ignoreLoading: true }).pipe(
catchError(() => of(emptyPageData<MpItemVersionGroupedView>()))
catchError(() => of([] as MpItemVersionSection[]))
);
})
).subscribe(result => {
this.searchResults = result.data;
this.searchResultGroups = this.groupSearchResults(result.data);
).subscribe(sections => {
this.searchResultGroups = this.toResultGroups(sections);
this.searchLoaded = true;
this.searchLoading = false;
});
@ -559,27 +558,15 @@ export class TbIotHubHomeComponent implements OnInit, OnDestroy {
* arrives already capped at four per type - so all this does is put the types in the panel's
* fixed order and carry each one's typeTotal onto the header.
*/
private groupSearchResults(items: MpItemVersionGroupedView[]): SearchResultGroup[] {
const groupMap = new Map<ItemType, MpItemVersionGroupedView[]>();
for (const item of items) {
if (!SEARCH_GROUP_ORDER.includes(item.type)) {
continue;
}
let list = groupMap.get(item.type);
if (!list) {
list = [];
groupMap.set(item.type, list);
}
list.push(item);
}
return SEARCH_GROUP_ORDER
.filter(type => groupMap.has(type))
.map(type => {
const groupItems = groupMap.get(type);
// Every row of a type carries the same typeTotal, so the first one answers for the
// section.
const total = groupItems[0].typeTotal;
return { type, items: groupItems, total, remaining: Math.max(0, total - groupItems.length) };
});
private toResultGroups(sections: MpItemVersionSection[]): SearchResultGroup[] {
return sections
.filter(section => SEARCH_GROUP_TYPES.includes(section.itemType))
.map(section => ({
type: section.itemType,
items: section.items,
total: section.total,
remaining: Math.max(0, section.total - section.items.length)
}));
}
}

15
ui-ngx/src/app/shared/models/iot-hub/iot-hub-version.models.ts

@ -109,15 +109,14 @@ export interface MpItemVersionView {
}
/**
* A row of a grouped search: the view plus the size of the section it belongs to. Its own type
* rather than a nullable field on the view, matching what the endpoint returns.
* One item type's share of a grouped search: the rows to show and how many exist behind them.
* A grouped answer is a list of these, in the server's section order, and a type nothing matched
* has no section — so a "+N more" header is `total - items.length`.
*/
export interface MpItemVersionGroupedView extends MpItemVersionView {
/**
* Rows of this row's item type behind the answer — what a "+N more" header subtracts the
* shown rows from. Always set: the grouped endpoint projects it for every row it returns.
*/
typeTotal: number;
export interface MpItemVersionSection {
itemType: ItemType;
total: number;
items: MpItemVersionView[];
}
// 404 body shapes returned by the public listing item-version endpoint

Loading…
Cancel
Save