Socket
Book a DemoInstallSign in
Socket

ngrx-action-mapper

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ngrx-action-mapper

Simple utility for building Ngrx reducer with less boilerplate

2.0.0
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

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);
}

Keywords

ngrx

FAQs

Package last updated on 20 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.