1- import { StoreModule , ActionReducerMap , MetaReducer } from '@ngrx/store' ;
1+ import {
2+ StoreModule ,
3+ ActionReducerMap ,
4+ MetaReducer ,
5+ createFeatureSelector ,
6+ createSelector
7+ } from '@ngrx/store' ;
28import { Params , RouterStateSnapshot } from '@angular/router' ;
39import { compose } from '@ngrx/store' ;
410import { ActionReducer , combineReducers } from '@ngrx/store' ;
511import { storeFreeze } from 'ngrx-store-freeze' ;
612import { storeLogger } from 'ngrx-store-logger' ;
713import { routerReducer , RouterReducerState , RouterStateSerializer } from '@ngrx/router-store' ;
14+ import * as fromRouter from '@ngrx/router-store' ;
815
916import * as fromUser from '../user/user.reducer' ;
1017
@@ -20,7 +27,7 @@ const modules = {
2027} ;
2128
2229export interface AppState {
23- router : RouterReducerState < RouterStateUrl > ;
30+ router : fromRouter . RouterReducerState < RouterStateUrl > ;
2431 user : fromUser . UserState ;
2532}
2633
@@ -29,16 +36,22 @@ export const syncReducers = {
2936 user : fromUser . userReducer
3037} ;
3138
39+ export const getUserState = createFeatureSelector < fromUser . UserState > ( 'user' ) ;
40+
41+ export const getUserLoaded = createSelector (
42+ getUserState ,
43+ fromUser . getLoaded
44+ ) ;
45+
3246export class CustomSerializer implements RouterStateSerializer < RouterStateUrl > {
3347 serialize ( routerState : RouterStateSnapshot ) : RouterStateUrl {
3448 let route = routerState . root ;
3549 while ( route . firstChild ) {
3650 route = route . firstChild ;
3751 }
3852
39- const { url } = routerState ;
40- const queryParams = routerState . root . queryParams ;
41- const params = route . params ;
53+ const { url, root : { queryParams } } = routerState ;
54+ const { params } = route ;
4255
4356 // Only return an object including the URL, params and query params
4457 // instead of the entire snapshot
@@ -65,7 +78,7 @@ const createReducer = (asyncReducers = {}) => {
6578
6679// Generate a reducer to set the root state in dev mode for HMR
6780function stateSetter ( reducer : ActionReducer < any > ) : ActionReducer < any > {
68- return function ( state : any , action : any ) {
81+ return function ( state : any , action : any ) {
6982 if ( action . type === 'SET_ROOT_STATE' ) {
7083 return action . payload ;
7184 }
@@ -74,15 +87,15 @@ function stateSetter(reducer: ActionReducer<any>): ActionReducer<any> {
7487}
7588
7689function logout ( reducer : ActionReducer < AppState > ) : ActionReducer < AppState > {
77- return function ( state : AppState , action : any ) : AppState {
90+ return function ( state : AppState , action : any ) : AppState {
7891 if ( action . type === '[User] Logout Success' ) {
7992 state = undefined ;
8093 }
8194 return reducer ( state , action ) ;
8295 } ;
8396}
8497
85- export function resetOnLogout ( reducer : ActionReducer < AppState > ) : ActionReducer < AppState > {
98+ export function resetOnLogout ( reducer : ActionReducer < AppState > ) : ActionReducer < AppState > {
8699 return function ( state , action ) {
87100 let newState ;
88101 if ( action . type === '[User] Logout Success' ) {
0 commit comments