All files / src/app/extensions/store-locator/store/stores stores.reducer.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 4/4
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 385x 5x     5x       5x               5x   5x           5x   2x     1x 1x     3x 3x      
import { EntityState, createEntityAdapter } from '@ngrx/entity';
import { createReducer, on } from '@ngrx/store';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { setErrorOn } from 'ish-core/utils/ngrx-creators';
 
import { StoreLocation } from '../../models/store-location/store-location.model';
 
import { highlightStore, loadStores, loadStoresFail, loadStoresSuccess } from './stores.actions';
 
export interface StoresState extends EntityState<StoreLocation> {
  loading: boolean;
  highlighted: string;
  error: HttpError;
}
 
export const storesAdapter = createEntityAdapter<StoreLocation>({ selectId: store => store.id });
 
export const initialState: StoresState = storesAdapter.getInitialState({
  loading: false,
  highlighted: undefined,
  error: undefined,
});
 
export const storesReducer = createReducer(
  initialState,
  on(loadStores, (state): StoresState => ({ ...state, loading: true })),
  setErrorOn(loadStoresFail),
  on(highlightStore, (state, action): StoresState => {
    const { storeId } = action.payload;
    return { ...state, highlighted: storeId };
  }),
  on(loadStoresSuccess, (state, action) => {
    const { stores } = action.payload;
    return storesAdapter.setAll(stores, { ...state, loading: false, error: undefined });
  })
);