mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-12 04:23:04 +00:00
rewrote header reaction to route change as ngrx event
This commit is contained in:
@@ -64,6 +64,7 @@
|
|||||||
"@ng-bootstrap/ng-bootstrap": "1.0.0-alpha.14",
|
"@ng-bootstrap/ng-bootstrap": "1.0.0-alpha.14",
|
||||||
"@ngrx/core": "^1.2.0",
|
"@ngrx/core": "^1.2.0",
|
||||||
"@ngrx/effects": "^2.0.0",
|
"@ngrx/effects": "^2.0.0",
|
||||||
|
"@ngrx/router-store": "^1.2.5",
|
||||||
"@ngrx/store": "^2.2.1",
|
"@ngrx/store": "^2.2.1",
|
||||||
"@ngrx/store-devtools": "^3.2.2",
|
"@ngrx/store-devtools": "^3.2.2",
|
||||||
"angular2-express-engine": "2.1.0-rc.1",
|
"angular2-express-engine": "2.1.0-rc.1",
|
||||||
|
@@ -8,6 +8,7 @@ import { AppComponent } from './app.component';
|
|||||||
import { HeaderComponent } from './header/header.component';
|
import { HeaderComponent } from './header/header.component';
|
||||||
|
|
||||||
import { StoreModule } from "@ngrx/store";
|
import { StoreModule } from "@ngrx/store";
|
||||||
|
import { RouterStoreModule } from "@ngrx/router-store";
|
||||||
import { StoreDevtoolsModule } from "@ngrx/store-devtools";
|
import { StoreDevtoolsModule } from "@ngrx/store-devtools";
|
||||||
|
|
||||||
import reducers from './app.reducers';
|
import reducers from './app.reducers';
|
||||||
@@ -31,6 +32,12 @@ import effects from './app.effects';
|
|||||||
*/
|
*/
|
||||||
StoreModule.provideStore(reducers),
|
StoreModule.provideStore(reducers),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngrx/router-store keeps router state up-to-date in the store and uses
|
||||||
|
* the store as the single source of truth for the router's state.
|
||||||
|
*/
|
||||||
|
RouterStoreModule.connectRouter(),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store devtools instrument the store retaining past versions of state
|
* Store devtools instrument the store retaining past versions of state
|
||||||
* and recalculating new states. This enables powerful time-travel
|
* and recalculating new states. This enables powerful time-travel
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
import { Component, OnInit, OnDestroy, HostListener } from "@angular/core";
|
import { Component, OnInit } from "@angular/core";
|
||||||
import { Router, NavigationEnd, Event } from "@angular/router";
|
|
||||||
import { Store } from "@ngrx/store";
|
import { Store } from "@ngrx/store";
|
||||||
import { HeaderState } from "./header.reducer";
|
import { HeaderState } from "./header.reducer";
|
||||||
import { HeaderActions } from "./header.actions";
|
import { HeaderActions } from "./header.actions";
|
||||||
@@ -10,32 +9,19 @@ import { Observable } from "rxjs";
|
|||||||
styleUrls: ['header.component.css'],
|
styleUrls: ['header.component.css'],
|
||||||
templateUrl: 'header.component.html'
|
templateUrl: 'header.component.html'
|
||||||
})
|
})
|
||||||
export class HeaderComponent implements OnDestroy, OnInit {
|
export class HeaderComponent implements OnInit {
|
||||||
private routerSubscription: any;
|
|
||||||
public isNavBarCollapsed: Observable<boolean>;
|
public isNavBarCollapsed: Observable<boolean>;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private router: Router,
|
|
||||||
private store: Store<HeaderState>
|
private store: Store<HeaderState>
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.routerSubscription = this.router.events.subscribe((event: Event) => {
|
|
||||||
if (event instanceof NavigationEnd) {
|
|
||||||
this.collapse();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
this.isNavBarCollapsed = this.store.select('headerReducer')
|
this.isNavBarCollapsed = this.store.select('headerReducer')
|
||||||
.map(({ navCollapsed }: HeaderState) => navCollapsed);
|
.map(({ navCollapsed }: HeaderState) => navCollapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnDestroy(): void {
|
|
||||||
if (this.routerSubscription) {
|
|
||||||
this.routerSubscription.unsubscribe();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private collapse(): void {
|
private collapse(): void {
|
||||||
this.store.dispatch(HeaderActions.collapse());
|
this.store.dispatch(HeaderActions.collapse());
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,7 @@ import { Injectable } from "@angular/core";
|
|||||||
import { Effect, Actions } from '@ngrx/effects'
|
import { Effect, Actions } from '@ngrx/effects'
|
||||||
import { HeaderActions } from "./header.actions";
|
import { HeaderActions } from "./header.actions";
|
||||||
import { HostWindowActions } from "../shared/host-window.actions";
|
import { HostWindowActions } from "../shared/host-window.actions";
|
||||||
|
import { routerActions } from "@ngrx/router-store";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class HeaderEffects {
|
export class HeaderEffects {
|
||||||
@@ -13,4 +14,8 @@ export class HeaderEffects {
|
|||||||
@Effect() resize$ = this.actions$
|
@Effect() resize$ = this.actions$
|
||||||
.ofType(HostWindowActions.RESIZE)
|
.ofType(HostWindowActions.RESIZE)
|
||||||
.map(() => HeaderActions.collapse());
|
.map(() => HeaderActions.collapse());
|
||||||
|
|
||||||
|
@Effect() routeChange$ = this.actions$
|
||||||
|
.ofType(routerActions.UPDATE_LOCATION)
|
||||||
|
.map(() => HeaderActions.collapse());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user