I'm having some trouble getting my selector to work. My component doesn't update with books from the FakeApiService
though I can see the call is made. So the actions and effects are working.
I believe the problem is related to my use of @ngrx/Entity
. Specifically this
export const getBooksState = createFeatureSelector<DemoState>('demo');
and
export const { selectAll } = fromBook.adapter.getSelectors();
I've seen tutorials where they supply a selector function to getSelectors()
but that shouldn't be necessary.
I'm hoping someone can spot where I go wrong, and if someone have some suggestions about the structure/setup I'm all ears :)
Here's my setup.
import { Action } from '@ngrx/store';
import { Book } from '../../models/book';
export enum BookActionTypes {
Load = '[Book] Load',
LoadSuccess = '[Book] Load Success',
LoadFail = '[Book] Load Fail'
}
export class LoadBooksAction {
readonly type = BookActionTypes.Load;
}
export class LoadBooksSuccessAction implements Action {
readonly type = BookActionTypes.LoadSuccess;
constructor(public payload: Book[]) {}
}
export class LoadBooksFailAction {
readonly type = BookActionTypes.LoadFail;
}
export type BookActions
= LoadBooksAction
| LoadBooksSuccessAction
| LoadBooksFailAction;
import { Injectable } from '@angular/core';
import { Actions, Effect } from '@ngrx/effects';
import { of } from 'rxjs';
import { catchError, map, switchMap } from 'rxjs/operators';
import { FakeApiService } from '../../services/fake-api.service';
import { LoadBooksFailAction, LoadBooksSuccessAction, BookActionTypes } from '../actions/book.actions';
@Injectable()
export class BookEffects {
constructor(
private fakeService: FakeApiService,
private actions$: Actions,
) {}
@Effect()
loadBooks$ = this.actions$
.ofType(BookActionTypes.Load)
.pipe(
switchMap(() => this.fakeService.getBooks()),
map(books => (new LoadBooksSuccessAction(books))),
catchError(error => of(new LoadBooksFailAction()))
);
}
import { EntityState, createEntityAdapter } from '@ngrx/entity';
import { Book } from '../../models/book';
import { BookActionTypes, BookActions } from './../actions/book.actions';
export interface BooksState extends EntityState<Book> {}
export const adapter = createEntityAdapter<Book>();
const initialState: BooksState = adapter.getInitialState();
export function reducer(state = initialState, action: BookActions ): BooksState {
switch (action.type) {
case BookActionTypes.LoadSuccess: {
return adapter.addAll(action.payload, state);
}
default: {
return state;
}
}
}
import { ActionReducerMap, createFeatureSelector } from '@ngrx/store';
import * as fromOrder from './book.reducer';
export interface DemoState {
demo: fromBook.BooksState;
}
export const reducers: ActionReducerMap<DemoState> = {
demo: fromBook.reducer
};
export const getBooksState = createFeatureSelector<DemoState>('demo');
import * as fromBook from '../reducers/book.reducer';
export const {selectAll} = fromBook.adapter.getSelectors();
@NgModule({
imports: [
CommonModule,
StoreModule.forFeature('demo', reducer),
EffectsModule.forFeature([BookEffects])
],
providers: [FakeApiService],
declarations: []
})
export class DemoModule { }
@Component({
selector: 'app-book-view',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<book-list [books]="books$ | async"></book-list>`
})
export class BookViewComponent implements OnInit {
books$: Observable<Book[]>;
constructor(private store: Store<fromStore.BooksState>) {
this.books$ = this.store.pipe(select(fromStore.selectAll));
}
ngOnInit() {
this.store.dispatch(new bookActions.LoadBooksAction());
}
}
Problem is with your Selector; You have to pass book/demo state into the getSelectors functions
import * as fromBook from '../reducers/book.reducer';
import { getBooksState } from '../reducers';
export const selectBookState = createSelector(
getBooksState,
state => state.demo
);
export const {
selectAll
} = fromBook.adapter.getSelectors(selectBookState);
have a look at my repo and url