All Projects → LMFinney → ngrx-enums

LMFinney / ngrx-enums

Licence: MIT license
A small library that provides the basis for using ts-enums for @ngrx actions and reducers.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to ngrx-enums

Keyist-Ecommerce
🔑 A simple ecommerce site powered with Spring Boot + Angular 10 + Ngrx + OAuth2
Stars: ✭ 220 (+1733.33%)
Mutual labels:  ngrx, ngrx-store
todo-app-ngrx
TodoMV* - Angular + Redux (ngrx)
Stars: ✭ 42 (+250%)
Mutual labels:  ngrx, ngrx-store
ionic3-firebase-ngrx
Sample Ionic 3 application using ngrx with firebase (auth, crud and camera plugin)
Stars: ✭ 48 (+300%)
Mutual labels:  ngrx, ngrx-store
ngx-redux-ui-management-recipes
Recipes for managing the UI layout of an application using Redux in Angular
Stars: ✭ 39 (+225%)
Mutual labels:  ngrx, ngrx-store
Deox
Functional Type-safe Flux Standard Utilities
Stars: ✭ 200 (+1566.67%)
Mutual labels:  ngrx
Aspnetcore Angular Ngrx
🚀 An ASP.NET Core WebAPI Demo with an Angular Client using Ngrx store and effects and Signalr
Stars: ✭ 141 (+1075%)
Mutual labels:  ngrx
Xsm
State Management made eXtraordinarily simple and effective for Angular, React, and Vue
Stars: ✭ 138 (+1050%)
Mutual labels:  ngrx
Ngrx Generator
ngrx templates generator
Stars: ✭ 132 (+1000%)
Mutual labels:  ngrx
ts-action-operators
TypeScript action operators for NgRx and redux-observable
Stars: ✭ 34 (+183.33%)
Mutual labels:  ngrx
Angular Checklist
🔥 Curated list of common mistakes made when developing Angular applications
Stars: ✭ 245 (+1941.67%)
Mutual labels:  ngrx
Router Store
Bindings to connect the Angular Router to @ngrx/store
Stars: ✭ 187 (+1458.33%)
Mutual labels:  ngrx
Ngrx Course
NgRx (with NgRx Data) -The Complete Guide
Stars: ✭ 157 (+1208.33%)
Mutual labels:  ngrx
Angular Seed Advanced
Advanced Angular seed project with support for ngrx/store, ngrx/effects, ngx-translate, angulartics2, lodash, NativeScript (*native* mobile), Electron (Mac, Windows and Linux desktop) and more.
Stars: ✭ 2,279 (+18891.67%)
Mutual labels:  ngrx
Angular Ngrx Chuck Norris
Chuck Norris Joke Generator w/ NgRx Store
Stars: ✭ 141 (+1075%)
Mutual labels:  ngrx
Rwa Trivia
Trivia App - Real World Angular series
Stars: ✭ 251 (+1991.67%)
Mutual labels:  ngrx
Ngrx Course V7
Angular Ngrx Course - includes Store, Effects, Entity, Router Store, Schematics
Stars: ✭ 133 (+1008.33%)
Mutual labels:  ngrx
Angular Ngrx Socket Frontend
Angular Ngrx Socket.IO Example
Stars: ✭ 177 (+1375%)
Mutual labels:  ngrx
Angular Ngrx Material Starter
Angular, NgRx, Angular CLI & Angular Material Starter Project
Stars: ✭ 2,623 (+21758.33%)
Mutual labels:  ngrx
Aspnetcoreangularsignalrsecurity
Security with ASP.NET Core, SignalR and Angular
Stars: ✭ 171 (+1325%)
Mutual labels:  ngrx
Aspnetcoreangularsignalr
SignalR ASP.NET Core Angular
Stars: ✭ 163 (+1258.33%)
Mutual labels:  ngrx

ngrx-enums

A small library that provides the base classes for for implementing @ngrx actions and reducers with ts-enums

Motivation

@ngrx/store is a very powerful utility for managing the state of Angular apps, but some developers have criticized the example app for containing too much boilerplate (particularly in the action classes) and for having large switch statements in the reducers. ngrx-example-app-enums is a fork of the example app that uses ts-enums to encapsulate the actions and reducers, thereby reducing boilerplate and hiding the switch statement from view.

This library builds on ts-enums to provide just the base files that ngrx-example-app-enums uses so that they can be used separately in your apps.

The basics

Install:

npm install ngrx-enums

Example

These examples are included in a test.

Actions Defined via Enums

import {ActionEnum, ActionEnumValue} from 'ngrx-enums';

class LayoutAction<T> extends ActionEnumValue<T> {
  constructor(name: string) {
    super(name);
  }
}

class LayoutActionEnumType extends ActionEnum<LayoutAction<any>> {
  OPEN_SIDENAV = new LayoutAction<void>('[Layout] Open Sidenav');
  OPEN_SIDENAV_ALSO = new LayoutAction<void>('[Layout] Open Sidenav Also');
  SET_SIDENAV = new LayoutAction<boolean>('[Layout] Set Sidenav');

  constructor() {
    super();
    this.initEnum('layoutActions');
  }
}

export const LayoutActionEnum: LayoutActionEnumType = new LayoutActionEnumType();

Reducer Defined via matches() Methods

import {simplePropertyReducer, TypedAction} from 'ngrx-enums';

interface State {
  showSidenav: boolean;
}

const initialState: State = {
  showSidenav: false
};

function layoutReducer(state = initialState, action: TypedAction<any>): State {
  if (LayoutActionEnum.matches(action,
      LayoutActionEnum.OPEN_SIDENAV, LayoutActionEnum.OPEN_SIDENAV_ALSO)) {
    // a simple reducer that always sets the same value
    // this shows how to respond to multiple actions
    // (fall-through in switch)
    return {...state, showSidenav: true};
  } else if (LayoutActionEnum.SET_SIDENAV.matches(action)) {
    // a reducer that accepts a value and copies it as a property into state
    return simplePropertyReducer<State, boolean>('showSidenav')(state, action);
  } else {
    return state;
  }
}

Reducer Defined via Enums

import {
  ActionEnumValue,
  ReducerEnum,
  ReducerEnumValue,
  ReducerFunction,
  simplePropertyReducer
} from 'ngrx-enums';

interface State {
  showSidenav: boolean;
}

const initialState: State = {
  showSidenav: false
};

class LayoutReducer<T> extends ReducerEnumValue<State, T> {
  constructor(action: ActionEnumValue<T> | ActionEnumValue<T>[],
              reduce: ReducerFunction<State, T>) {
    super(action, reduce);
  }
}

class LayoutReducerEnumType extends ReducerEnum<LayoutReducer<any>, State> {
  // a simple reducer that always sets the same value
  // this shows how to respond to multiple actions
  // (fall-through in switch)
  OPEN_SIDENAV = new LayoutReducer<void>(
    [LayoutActionEnum.OPEN_SIDENAV, LayoutActionEnum.OPEN_SIDENAV_ALSO],
      (state: State) => ({showSidenav: true}));
  // a reducer that accepts a value and copies it as a property into state
  SET_SIDENAV = new LayoutReducer<boolean>(
    LayoutActionEnum.SET_SIDENAV,
    simplePropertyReducer<State, boolean>('showSidenav')
  );

  constructor() {
    super(initialState);
    this.initEnum('layoutReducers');
  }
}

export const LayoutReducerEnum: LayoutReducerEnumType = new LayoutReducerEnumType();

More information

  • Some users get an error that looks something like this when compiling:

    ERROR in Error encountered resolving symbol values statically. Calling function 'ɵmakeDecorator', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol Injectable...

    If you get this error, you might be able to fix the problem by adding a path of "@angular/*": ["../node_modules/@angular/*"] to your tsconfig.app.json file (more information).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].