From c14a216dcf2cb1a635f4ee16625f94ba42b8999a Mon Sep 17 00:00:00 2001 From: Jane Sandberg Date: Thu, 25 Jun 2020 13:55:41 -0700 Subject: [PATCH] LP 1857351: refactor basic admin component Refactors the component to be more consistent in how it gets data from ActivatedRoute. Signed-off-by: Jane Sandberg Signed-off-by: Ruth Frasur Signed-off-by: Michele Morgan --- .../staff/admin/basic-admin-page.component.ts | 121 ++++++++++-------- 1 file changed, 70 insertions(+), 51 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.ts b/Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.ts index 6a43a628a1..a983a7272d 100644 --- a/Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.ts @@ -1,6 +1,8 @@ import {Component, OnInit} from '@angular/core'; -import {ActivatedRoute} from '@angular/router'; +import {ActivatedRoute, ParamMap} from '@angular/router'; import {IdlService} from '@eg/core/idl.service'; +import {Observable} from 'rxjs'; +import {tap, switchMap} from 'rxjs/operators'; /** * Generic IDL class editor page. @@ -8,6 +10,7 @@ import {IdlService} from '@eg/core/idl.service'; @Component({ template: ` + @@ -16,6 +19,7 @@ import {IdlService} from '@eg/core/idl.service'; configLinkBasePath="{{configLinkBasePath}}" readonlyFields="{{readonlyFields}}" [disableOrgFilter]="disableOrgFilter"> + ` }) @@ -30,6 +34,13 @@ export class BasicAdminPageComponent implements OnInit { // Tell the admin page to disable and hide the automagic org unit filter disableOrgFilter: boolean; + private getParams$: Observable; + private getRouteData$: Observable; + private getParentUrl$: Observable; + + private schema: string; + private table: string; + constructor( private route: ActivatedRoute, private idl: IdlService @@ -37,60 +48,68 @@ export class BasicAdminPageComponent implements OnInit { } ngOnInit() { - let schema = this.route.snapshot.paramMap.get('schema'); - if (!schema) { - // Allow callers to pass the schema via static route data - const data = this.route.snapshot.data[0]; - if (data) { schema = data.schema; } - } - let table = this.route.snapshot.paramMap.get('table'); - if (!table) { - const data = this.route.snapshot.data[0]; - if (data) { table = data.table; } - } - const fullTable = schema + '.' + table; - - // Set the prefix to "server", "local", "workstation", - // extracted from the URL path. - // For admin pages that use none of these, avoid setting - // the prefix because that will cause it to double-up. - // e.g. eg.grid.acq.acq.cancel_reason - this.persistKeyPfx = this.route.snapshot.parent.url[0].path; - const selfPrefixers = ['acq', 'action_trigger', 'booking']; - if (selfPrefixers.indexOf(this.persistKeyPfx) > -1) { - // selfPrefixers, unlike 'server', 'local', and - // 'workstation', are the root of the path. - this.persistKeyPfx = ''; - } else { - this.configLinkBasePath += '/' + this.persistKeyPfx; - } - - // Pass the readonlyFields param if available - if (this.route.snapshot.data && this.route.snapshot.data[0]) { - // snapshot.data is a HASH. - const data = this.route.snapshot.data[0]; - - if (data.readonlyFields) { - this.readonlyFields = data.readonlyFields; - } + this.getParams$ = this.route.paramMap + .pipe(tap(params => { + this.schema = params.get('schema'); + this.table = params.get('table'); + })); - if (data.disableOrgFilter) { - this.disableOrgFilter = true; - } - } + this.getRouteData$ = this.route.data + .pipe(tap(routeData => { + const data = routeData[0]; + + if (data) { + // Schema and table can both be passed + // by either param or data + if (!this.schema) { + this.schema = data['schema']; + } + if (!this.table) { + this.table = data['table']; + } + this.disableOrgFilter = data['disableOrgFilter']; + this.readonlyFields = data['readonlyFields']; + } + + })); - Object.keys(this.idl.classes).forEach(class_ => { - const classDef = this.idl.classes[class_]; - if (classDef.table === fullTable) { - this.idlClass = class_; - this.classLabel = classDef.label; + this.getParentUrl$ = this.route.parent.url + .pipe(tap(parentUrl => { + // Set the prefix to "server", "local", "workstation", + // extracted from the URL path. + // For admin pages that use none of these, avoid setting + // the prefix because that will cause it to double-up. + // e.g. eg.grid.acq.acq.cancel_reason + this.persistKeyPfx = this.route.snapshot.parent.url[0].path; + const selfPrefixers = ['acq', 'action_trigger', 'booking']; + if (selfPrefixers.indexOf(this.persistKeyPfx) > -1) { + // selfPrefixers, unlike 'server', 'local', and + // 'workstation', are the root of the path. + this.persistKeyPfx = ''; + } else { + this.configLinkBasePath += '/' + this.persistKeyPfx; + } + })); + + this.getParentUrl$.subscribe(); + this.getParams$.pipe( + switchMap(() => this.getRouteData$) + ).subscribe(() => { + const fullTable = this.schema + '.' + this.table; + + Object.keys(this.idl.classes).forEach(class_ => { + const classDef = this.idl.classes[class_]; + if (classDef.table === fullTable) { + this.idlClass = class_; + this.classLabel = classDef.label; + } + }); + + if (!this.idlClass) { + throw new Error('Unable to find IDL class for table ' + fullTable); } }); - - if (!this.idlClass) { - throw new Error('Unable to find IDL class for table ' + fullTable); - } } -} +} -- 2.43.2