Nodes Per Minute

    This package has been deprecated

    Author message:

    Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

    ngrx-action-mapper
    TypeScript icon, indicating that this package has built-in type declarations

    2.0.0 • Public • Published

    Ngrx Action Mapper

    Simple utility for building Ngrx reducer with less boilerplate

    npm version Build Status Commitizen friendly

    Install

    Install the npm package: ngrx-action-mapper

    # with npm 
    npm install ngrx-action-mapper
    # or with yarn 
    yarn add ngrx-action-mapper

    Usage

    // src/app/store/users.reducer.ts
     
    import { createEntityAdapter, EntityAdapter, EntityState } from '@ngrx/entity';
    import { Action } from '@ngrx/store';
    import { EntityActionMapper } from 'ngrx-action-mapper';
     
    export interface User {
      id: string;
      name: string;
    }
     
    export abstract class BaseAction<Payload = never> {
      public type: string = this.constructor.name;
     
      constructor(public payload?: Payload) {}
    }
     
    export class AddOneUserAction extends BaseAction<User> {}
    export class RemoveOneUserAction extends BaseAction<string> {}
    export class SelectUserAction extends BaseAction<User> {}
     
    export interface UsersState extends EntityState<User> {
      // additional entity state properties
      selectedUserId: string | null;
    }
     
    export interface UsersRootState extends EntityState<User> {
      users: UsersState;
    }
     
    export const usersAdapter: EntityAdapter<User> = createEntityAdapter<User>();
     
    export const initialUsersState: UsersState = usersAdapter.getInitialState({
      selectedUserId: null,
    });
     
    export const actionReducer = new EntityActionMapper(initialUsersState, usersAdapter)
      .mapEntityAction(AddOneUserAction, adapter => adapter.addOne)
      .mapEntityAction(RemoveOneUserAction, adapter => adapter.removeOne)
      .add(
        SelectUserAction,
        (state: UsersState, user: User): UsersState => {
          return { ...state, selectedUserId: user.id };
        },
      )
      .buildReducer();
     
    export function usersReducer(state: UsersState, action: Action): UsersState {
      return actionReducer(state, action);
    }

    Install

    npm i ngrx-action-mapper

    DownloadsWeekly Downloads

    3

    Version

    2.0.0

    License

    MIT

    Unpacked Size

    93.9 kB

    Total Files

    24

    Last publish

    Collaborators

    • zakhenry