Skip to content

Commit 62c3763

Browse files
committed
[NAE-2125] Remove URI service usage from admin and menu items
- Improved breadcrumbs redirection logic by adding null checks and appropriate error logging for missing targets. - Simplified and optimized left and right navigation item loading with reusable `processItems` and `getChildCasesFromPage` logic. - Enhanced error handling and loading state management to ensure robustness in edge cases.
1 parent 241e3f9 commit 62c3763

File tree

2 files changed

+73
-81
lines changed

2 files changed

+73
-81
lines changed

projects/netgrif-components-core/src/lib/navigation/breadcrumbs/abstract-breadcrumbs.component.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ export abstract class AbstractBreadcrumbsComponent implements OnDestroy, AfterVi
114114
if (!this.redirectOnClick) {
115115
return;
116116
}
117-
this._router.navigate(this.redirectUrls.get(this._pathService.activePath)).then(r => {})
117+
const target = this.redirectUrls.get(this._pathService.activePath);
118+
if (target) {
119+
this._router.navigate(target).then(r => {});
120+
} else {
121+
this._log.error("Missing required data for redirecting breadcrumbs.")
122+
}
118123
}
119124

120125
public reset(): void {

projects/netgrif-components-core/src/lib/navigation/navigation-double-drawer/abstract-navigation-double-drawer.ts

Lines changed: 67 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Component, Input, OnDestroy, OnInit, TemplateRef} from '@angular/core';
33
import {ActivatedRoute, Router} from '@angular/router';
44
import {ResizeEvent} from 'angular-resizable-element';
55
import {Observable, of, Subscription} from 'rxjs';
6-
import {map} from 'rxjs/operators';
6+
import {map, switchMap, catchError, finalize} from 'rxjs/operators';
77
import {RoleAccess, View} from '../../../commons/schema';
88
import {AccessService} from '../../authorization/permission/access.service';
99
import {ConfigurationService} from '../../configuration/configuration.service';
@@ -43,7 +43,6 @@ import {
4343
SETTINGS_TRANSITION_ID
4444
} from '../model/navigation-configs';
4545

46-
4746
@Component({
4847
selector: 'ncc-abstract-navigation-double-drawer',
4948
template: '',
@@ -215,7 +214,7 @@ export abstract class AbstractNavigationDoubleDrawerComponent implements OnInit,
215214
getItemCaseByPath(path?: string): Observable<Page<Case>> {
216215
const searchBody: CaseSearchRequestBody = {
217216
data: {
218-
[GroupNavigationConstants.ITEM_FIELD_ID_NODE_PATH] : path
217+
[GroupNavigationConstants.ITEM_FIELD_ID_NODE_PATH]: path
219218
},
220219
process: {identifier: "preference_item"}
221220
};
@@ -333,7 +332,7 @@ export abstract class AbstractNavigationDoubleDrawerComponent implements OnInit,
333332
const path = item.resource.immediateData.find(f => f.stringId === GroupNavigationConstants.ITEM_FIELD_ID_NODE_PATH)?.value
334333
if (this.hasItemChildren(item) && !this.leftLoading$.isActive && !this.rightLoading$.isActive) {
335334
this._pathService.activePath = path;
336-
} else if (!path.includes(this.currentPath)){
335+
} else if (!path.includes(this.currentPath)) {
337336
this._pathService.activePath = this.extractParent(this.currentPath);
338337
} else {
339338
this._pathService.activePath = this.currentPath;
@@ -355,98 +354,87 @@ export abstract class AbstractNavigationDoubleDrawerComponent implements OnInit,
355354
return;
356355
}
357356
this.leftLoading$.on();
358-
this.getItemCaseByPath(this.extractParent(this.currentPath)).subscribe({
359-
next: (page) => {
360-
let childCases$: Observable<Case[]>, targetItem: Case, orderedChildCaseIds: string[];
361-
362-
if (page?.pagination?.totalElements === 0) {
363-
childCases$ = of([])
364-
} else {
365-
targetItem = page.content[0]
366-
orderedChildCaseIds = this.extractChildCaseIds(targetItem)
367-
childCases$ = this.getItemCasesByIdsInOnePage(orderedChildCaseIds).pipe(
368-
map(p => p.content),
369-
)
370-
}
371-
372-
childCases$.subscribe({
373-
next: (v) => {
374-
const result = v.map(folder => this.resolveItemCaseToNavigationItem(folder)).filter(i => !!i);
375-
this.leftItems = result.sort((a, b) => orderedChildCaseIds.indexOf(a.resource.stringId) - orderedChildCaseIds.indexOf(b.resource.stringId));
376-
this.resolveCustomViewsInLeftSide()
377-
this.leftLoading$.off();
378-
},
379-
error: (error) => {
380-
this._log.error(error);
381-
this.leftItems = [];
382-
this.resolveCustomViewsInLeftSide()
383-
this.leftLoading$.off();
384-
}
385-
});
386-
},
387-
error: (error) => {
357+
358+
this.getItemCaseByPath(this.extractParent(this.currentPath)).pipe(
359+
switchMap(page => this.getChildCasesFromPage(page)),
360+
map(({cases, orderedIds}) => this.processLeftItems(cases, orderedIds)),
361+
catchError((error) => {
388362
this._log.error(error);
389-
this.leftItems = [];
390-
this.resolveCustomViewsInLeftSide()
363+
return of([]);
364+
}),
365+
finalize(() => {
366+
this.resolveCustomViewsInLeftSide();
391367
this.leftLoading$.off();
392-
}
368+
})
369+
).subscribe(items => {
370+
this.leftItems = items;
393371
});
394372
}
395373

374+
private processLeftItems(cases: Case[], orderedChildCaseIds: string[]): NavigationItem[] {
375+
const result = cases
376+
.map(folder => this.resolveItemCaseToNavigationItem(folder))
377+
.filter(i => !!i);
378+
return result.sort((a, b) =>
379+
orderedChildCaseIds.indexOf(a.resource.stringId) - orderedChildCaseIds.indexOf(b.resource.stringId)
380+
);
381+
}
382+
396383
protected loadRightSide() {
397384
this.rightLoading$.on();
398385
this.moreItems = [];
399-
this.getItemCaseByPath(this.currentPath).subscribe({
400-
next: (page) => {
401-
let childCases$: Observable<Case[]>, targetItem: Case, orderedChildCaseIds: string[];
402-
403-
if (page?.pagination?.totalElements === 0) {
404-
childCases$ = of([])
405-
} else {
406-
targetItem = page.content[0]
407-
orderedChildCaseIds = this.extractChildCaseIds(targetItem)
408-
childCases$ = this.getItemCasesByIdsInOnePage(orderedChildCaseIds).pipe(
409-
map(p => p.content),
410-
)
411-
}
412-
413-
childCases$.subscribe({
414-
next: (result) => {
415-
result = (result as Case[]).sort((a, b) => orderedChildCaseIds.indexOf(a.stringId) - orderedChildCaseIds.indexOf(b.stringId));
416-
if (result.length > RIGHT_SIDE_INIT_PAGE_SIZE) {
417-
const rawRightItems: Case[] = result.splice(0, RIGHT_SIDE_INIT_PAGE_SIZE);
418-
this.rightItems = rawRightItems.map(folder => this.resolveItemCaseToNavigationItem(folder)).filter(i => !!i);
419-
this.moreItems = result.map(folder => this.resolveItemCaseToNavigationItem(folder)).filter(i => !!i);
420-
} else {
421-
this.rightItems = result.map(folder => this.resolveItemCaseToNavigationItem(folder)).filter(i => !!i);
422-
}
423-
this.resolveCustomViewsInRightSide()
424-
this.rightLoading$.off();
425-
},
426-
error: (error) => {
427-
this._log.error(error);
428-
this.rightItems = [];
429-
this.moreItems = [];
430-
this.resolveCustomViewsInRightSide()
431-
this.rightLoading$.off();
432-
}
433-
});
434-
},
435-
error: (error) => {
386+
387+
this.getItemCaseByPath(this.currentPath).pipe(
388+
switchMap(page => this.getChildCasesFromPage(page)),
389+
map(({cases, orderedIds}) => this.processRightItems(cases, orderedIds)),
390+
catchError((error) => {
436391
this._log.error(error);
437-
this.rightItems = [];
438-
this.moreItems = [];
439-
this.resolveCustomViewsInRightSide()
392+
return of({right: [], more: []});
393+
}),
394+
finalize(() => {
395+
this.resolveCustomViewsInRightSide();
440396
this.rightLoading$.off();
441-
}
397+
})
398+
).subscribe(({right, more}) => {
399+
this.rightItems = right;
400+
this.moreItems = more;
442401
});
443402
}
444403

404+
private processRightItems(cases: Case[], orderedChildCaseIds: string[]): {
405+
right: NavigationItem[];
406+
more: NavigationItem[]
407+
} {
408+
const result = cases
409+
.sort((a, b) => orderedChildCaseIds.indexOf(a.stringId) - orderedChildCaseIds.indexOf(b.stringId));
410+
let right: NavigationItem[], more: NavigationItem[];
411+
if (result.length > RIGHT_SIDE_INIT_PAGE_SIZE) {
412+
const rawRightItems: Case[] = result.splice(0, RIGHT_SIDE_INIT_PAGE_SIZE);
413+
right = rawRightItems.map(folder => this.resolveItemCaseToNavigationItem(folder)).filter(i => !!i);
414+
more = result.map(folder => this.resolveItemCaseToNavigationItem(folder)).filter(i => !!i);
415+
} else {
416+
right = result.map(folder => this.resolveItemCaseToNavigationItem(folder)).filter(i => !!i);
417+
more = [];
418+
}
419+
return {right, more};
420+
}
421+
422+
private getChildCasesFromPage(page: Page<Case>): Observable<{ cases: Case[]; orderedIds: string[] }> {
423+
if (!page?.pagination?.totalElements) {
424+
return of({cases: [], orderedIds: []});
425+
}
426+
const targetItem = page.content[0];
427+
const orderedChildCaseIds = this.extractChildCaseIds(targetItem) ?? [];
428+
return this.getItemCasesByIdsInOnePage(orderedChildCaseIds).pipe(
429+
map(p => ({cases: p.content, orderedIds: orderedChildCaseIds}))
430+
);
431+
}
432+
445433
protected extractChildCaseIds(item: Case): string[] {
446434
return item.immediateData.find(f => f.stringId === GroupNavigationConstants.ITEM_FIELD_ID_CHILD_ITEM_IDS)?.value
447435
}
448436

449-
protected getItemCasesByIdsInOnePage(caseIds: string[]): Observable<Page<Case>> {
437+
protected getItemCasesByIdsInOnePage(caseIds: string[]): Observable<Page<Case>> {
450438
return this.getItemCasesByIds(caseIds, 0, caseIds.length)
451439
}
452440

@@ -620,5 +608,4 @@ export abstract class AbstractNavigationDoubleDrawerComponent implements OnInit,
620608
}
621609
return path.substring(0, path.lastIndexOf('/'));
622610
}
623-
624611
}

0 commit comments

Comments
 (0)