Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ngrx store - how to select root state

I am loading config file from appsettings.json, where I have api url saved.

I've set up ngrx 7 store, with effects. I dispatch loadconfig action in app.component.ts onInit, and then I try to read data from store in console after. I get an error:

TypeError: Cannot read property 'getConfig' of undefined at new AppFacade (VM5613 main.js:2060)

I think problem is how I implemented selectors for root. As you can see I used createFeatureSelector, but root state is not feature, it's root. What am I doing wrong? Why I can't access state value as it says it's undefined but in redux chrome devtools state is clearly there?

State is stored as we can see here: enter image description here

This is how I implemented my store:
Actions:

import { Action } from '@ngrx/store';
import { AppConfig } from '../../app.config';

export enum AppActionTypes {
  LoadConfig = '[App] Load Config',
  LoadConfigError = '[App] Load Config Error',
  LoadConfigSuccess = '[App] Load Config Success'
}

export class LoadConfig implements Action {
  public readonly type = AppActionTypes.LoadConfig;
}

export class LoadConfigError implements Action {
  public readonly type = AppActionTypes.LoadConfigError;
  constructor(public payload: any) {}
}

export class LoadConfigSuccess implements Action {
  public readonly type = AppActionTypes.LoadConfigSuccess;
  constructor(public payload: AppConfig) {}
}

export type AppActions = LoadConfig | LoadConfigError | LoadConfigSuccess;

export const fromAppActions: any = {
  LoadConfig,
  LoadConfigError,
  LoadConfigSuccess
};

Effects:

import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { AppActionTypes } from '../actions/app.actions';
import { HttpClient } from '@angular/common/http';
import { mergeMap, map, catchError } from 'rxjs/operators';
import { AppConfig } from '../../app.config';
import { of } from 'rxjs';

@Injectable()
export class AppEffects {
  constructor(private actions$: Actions, private httpClient: HttpClient) {}

  @Effect()
  private loadConfig$: any = this.actions$.pipe(
    ofType(AppActionTypes.LoadConfig),
    mergeMap(() =>
      this.httpClient.get('assets/appsettings.json').pipe(
        map((response: AppConfig) => ({
          type: AppActionTypes.LoadConfigSuccess,
          payload: response
        })),
        catchError(() => of({ type: AppActionTypes.LoadConfigError }))
      )
    )
  );
}

Facade:

import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs';
import { AppState } from '../reducers/app.reducer';
import { appQuery } from '../selectors/app.selectors';
import { AppConfig } from '../../app.config';
import { LoadConfig } from '../actions/app.actions';

@Injectable({
  providedIn: 'root'
})
export class AppFacade {
  constructor(private store: Store<AppState>) {}

  public getConfig: Observable<AppConfig> = this.store.pipe(
    select(appQuery.getConfig)
  );

  public loadConfig(): void {
    this.store.dispatch(new LoadConfig());
  }
}

Reducer:

import { AppActions, AppActionTypes } from '../actions/app.actions';
import { AppConfig } from '../../app.config';

export const APP_KEY: string = 'app_key';

export interface AppState {
  appConfig: AppConfig | undefined;
}

export const initialState: AppState = {
  appConfig: {
    AppSettings: {
      AppUrl: undefined,
      ApiUrl: undefined,
      AuthUrl: undefined
    },
    ApplicationInsights: {
      InstrumentationKey: undefined
    }
  }
};

export function reducer(
  state: AppState = initialState,
  action: AppActions
): AppState {
  switch (action.type) {
    case AppActionTypes.LoadConfigSuccess: {
      return {
        ...state,
        appConfig: action.payload
      };
    }
  }

  return state;
}

Selectors:

import {
  createFeatureSelector,
  MemoizedSelector,
  createSelector
} from '@ngrx/store';
import { AppState, APP_KEY } from '../reducers/app.reducer';
import { AppConfig } from '../../app.config';

// Lookup the 'account bar' feature state managed by NgRx
const getAppState: MemoizedSelector<
  object,
  AppState
> = createFeatureSelector<AppState>(APP_KEY);

const getConfig: MemoizedSelector<object, AppConfig> = createSelector(
  getAppState,
  (state: AppState) => state.appConfig
);

export const appQuery = {
  getConfig
};

App module:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { CoreModule } from './core/core.module';

import { EffectsModule } from '@ngrx/effects';
import { StoreModule } from '@ngrx/store';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';

import { AppComponent } from './app.component';
import { AppEffects } from './store/effects/app.effects';
import { reducers } from './store/reducers';

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    CoreModule,
    StoreModule.forRoot(reducers),
    EffectsModule.forRoot([AppEffects]),
    StoreDevtoolsModule.instrument({
      maxAge: 100,
      logOnly: false
    })
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

App component:

import { Component, OnInit } from '@angular/core';
import { AppFacade } from './store/facades/app,facade';
import { AppConfig } from './app.config';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  constructor(private facade: AppFacade) {}

  public ngOnInit(): void {
    this.facade.loadConfig();

    this.facade.getConfig.subscribe((response: AppConfig) => {
      console.log(JSON.stringify(response, null, 4));
    });
  }
}
like image 453
sensei Avatar asked Feb 17 '19 00:02

sensei


Video Answer


1 Answers

Ok I found an issue. I won't delete my question since structure of store is world class, and might help someone else.

Problem was the APP_KEY in reducer was "app_key", instead of "app", as you can see from picture state name is "app", thus was accessing non-existing state.

like image 185
sensei Avatar answered Oct 08 '22 10:10

sensei