amin mohammadi
amin mohammadi

Reputation: 1053

NGRX; How to apply filter on adapter data

I have this piece of code which I want to apply a filter on. The state uses EntityState and Adapter. getAllObjects needs to get categories with parentId of 13 and the getAllTextures gets the remaining.

import { Category } from './models/category';
import { createFeatureSelector, createSelector } from '@ngrx/store';
import { AppState } from './app-state';
import { EntityState } from '@ngrx/entity';
import * as fromCategory from './services/reducers/category.reducer';

export interface CategoriesState extends EntityState<Category> {
    allCategoriesLoaded: boolean;
}

const getCategories = createFeatureSelector<AppState, CategoriesState>('categories');

export const getAllObjects = createSelector(getCategories, fromCategory.selectAll); // filter(x => x.parentId === 13)
export const getAllTextures = createSelector(getCategories, fromCategory.selectAll); // filter(x => x.parentId !== 13)
export const getAllCategoriesLoaded = createSelector(getCategories, state => state.allCategoriesLoaded);

and this is the reducer:

import {createReducer, on, State} from '@ngrx/store';
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { Category } from 'src/app/models/category';
import { RefreshCategoryDone, CategoryActionTypes, CategoryActions } from '../actions/category.actions';
import { CategoriesState } from 'src/app/categories-state';


export const categoryAdapter: EntityAdapter<Category> = createEntityAdapter<Category>();
export const initialState: CategoriesState = categoryAdapter.getInitialState({allCategoriesLoaded : false});

export function categoryReducer(state = initialState, action: CategoryActions) {
  switch (action.type) {
      case CategoryActionTypes.LoadCategories:
      categoryAdapter.addMany(action.payload, state);
      break;
    default:
      return state;
  }
}

export const {
  selectAll,
  selectEntities,
  selectIds,
  selectTotal
} = categoryAdapter.getSelectors();

Is it possible to select data from adapter by condition? If so, how?

Upvotes: 2

Views: 2181

Answers (2)

James D
James D

Reputation: 2311

Here's a few simple steps to set this up:

1) add a 'selectedId' property to your State and set the initial state to null

   export interface State extends EntityState<Category> {
       selectedCategoryId: string | null;
   }

   export const adapter: EntityAdapter<Category> = createEntityAdapter<Category>({
      selectId: (category: Category) => category.id,
      sortComparer: false,
   });

   export const initialState: State = adapter.getInitialState({
      selectedCategoryId: null,
   });

2) Add actions to set the selected ID

   // in the reducer, the action does following:
   on(ViewReportPageActions.selectCategory, (state, { id }) => ({
      ...state,
      selectedCategoryId: id,
   })),

3) add a selector for the selectedId in the reducer

    export const getSelectedId = (state: State) => state.selectedReportId;

4) add a selector at your FeatureSelector file, this is where you add the filter

// you've implemented another name for you Feature Selector here
export const getDataState = createFeatureSelector<State, DataState>('data');

export const getCategoryEntitiesState = createSelector(
  getDataState,
  state => state.categories
);

export const getSelectedCategoryId = createSelector(
  getCategoryEntitiesState,
  fromCategorys.getSelectedId
);

export const getSelectedCategory = createSelector(
  getCategoryEntities,
  getSelectedCategoryId,
  (entities, selectedId) => {
    if (selectedId != "0"){
      return selectedId && entities[selectedId];
    } else {
      //this is what I use, when I want to create a new category. 
      //I set the selectedId to 0 which indicitates I'm creating a new one
      return selectedId && generateMockCategory(); 
    }
  }
);

export const getNotSelectedCategory = createSelector(
  getCategoryEntities,
  getSelectedCategoryId,
  (entities, selectedId) => {
    // You'll have to test this...
    return entities.filter(e => e.id !== selectedID);
  }
);

example pages:

Upvotes: 1

amin mohammadi
amin mohammadi

Reputation: 1053

Found the answer:

import { Category } from './models/category';
import { createFeatureSelector, createSelector } from '@ngrx/store';
import { AppState } from './app-state';
import { EntityState } from '@ngrx/entity';
import * as fromCategory from './services/reducers/category.reducer';
import { map } from 'rxjs/operators';

export interface CategoriesState extends EntityState<Category> {
    allCategoriesLoaded: boolean;
}

const getCategories = createFeatureSelector<AppState, CategoriesState>('categories');

export const getAllCategories = createSelector(getCategories, fromCategory.selectAll);
export const getAllObjects = createSelector(getAllCategories, (categories) => categories.filter(x => x.categoryId !== 13));
export const getAllTextures = createSelector(getAllCategories, (categories) => categories.filter(x => x.categoryId === 13));
export const getAllCategoriesLoaded = createSelector(getCategories, state => state.allCategoriesLoaded);

Upvotes: 0

Related Questions