From 2f7e69e1a27f2dc1732e4a55ef98d752b6f86f20 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Fri, 27 Mar 2020 15:06:07 +0300 Subject: [PATCH] refactor: reduce codes and increase readability --- .../components/dynamic-layout.component.ts | 48 +++++++------------ 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/components/dynamic-layout.component.ts b/npm/ng-packs/packages/core/src/lib/components/dynamic-layout.component.ts index fc3926a1ea..e835dd777d 100644 --- a/npm/ng-packs/packages/core/src/lib/components/dynamic-layout.component.ts +++ b/npm/ng-packs/packages/core/src/lib/components/dynamic-layout.component.ts @@ -1,15 +1,13 @@ -import { Component, Input, OnDestroy, Type, Injector } from '@angular/core'; +import { Component, OnDestroy, Type } from '@angular/core'; import { ActivatedRoute, NavigationEnd, Router, UrlSegment } from '@angular/router'; -import { Select, Store } from '@ngxs/store'; -import { Observable, combineLatest } from 'rxjs'; +import { Store } from '@ngxs/store'; import snq from 'snq'; import { eLayoutType } from '../enums/common'; -import { Config } from '../models/config'; import { ABP } from '../models/common'; +import { ReplaceableComponents } from '../models/replaceable-components'; import { ConfigState } from '../states/config.state'; -import { takeUntilDestroy } from '../utils/rxjs-utils'; import { ReplaceableComponentsState } from '../states/replaceable-components.state'; -import { ReplaceableComponents } from '../models/replaceable-components'; +import { takeUntilDestroy } from '../utils/rxjs-utils'; @Component({ selector: 'abp-dynamic-layout', @@ -22,23 +20,9 @@ import { ReplaceableComponents } from '../models/replaceable-components'; `, }) export class DynamicLayoutComponent implements OnDestroy { - @Select(ReplaceableComponentsState.getComponent('Theme.ApplicationLayoutComponent')) - applicationLayout$: Observable; - - @Select(ReplaceableComponentsState.getComponent('Theme.AccountLayoutComponent')) - accountLayout$: Observable; - - @Select(ReplaceableComponentsState.getComponent('Theme.EmptyLayoutComponent')) - emptyLayout$: Observable; - layout: Type; - layouts = {} as { [key in eLayoutType]: Type }; - - expectedLayout: eLayoutType = eLayoutType.empty; - constructor(private router: Router, private route: ActivatedRoute, private store: Store) { - this.listenToLayouts(); const { routes } = this.store.selectSnapshot(ConfigState.getAll); router.events.pipe(takeUntilDestroy(this)).subscribe(event => { @@ -47,25 +31,25 @@ export class DynamicLayoutComponent implements OnDestroy { { path: router.url.replace('/', '') }, ] as any); - this.expectedLayout = + const layouts = { + application: this.getComponent('Theme.ApplicationLayoutComponent'), + account: this.getComponent('Theme.AccountLayoutComponent'), + empty: this.getComponent('Theme.EmptyApplicationLayoutComponent'), + }; + + const expectedLayout = (this.route.snapshot.data || {}).layout || findLayout(segments, routes); - this.layout = this.layouts[this.expectedLayout]; + this.layout = layouts[expectedLayout].component; } }); } - ngOnDestroy() {} - - listenToLayouts() { - combineLatest(this.applicationLayout$, this.accountLayout$, this.emptyLayout$) - .pipe(takeUntilDestroy(this)) - .subscribe(([application, account, empty]) => { - this.layouts.application = application.component; - this.layouts.account = account.component; - this.layouts.empty = empty.component; - }); + getComponent(key: string): ReplaceableComponents.ReplaceableComponent { + return this.store.selectSnapshot(ReplaceableComponentsState.getComponent(key)); } + + ngOnDestroy() {} } function findLayout(segments: UrlSegment[], routes: ABP.FullRoute[]): eLayoutType {