Skip to content

Commit

Permalink
feat(codestyle): use action types for ngrx actions instead of factory…
Browse files Browse the repository at this point in the history
… functions

- standardize payloads (always use payload object with typed properties)

Closes #25
  • Loading branch information
tomastrajan committed Jan 12, 2018
1 parent 06a51a5 commit 98435ed
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 133 deletions.
11 changes: 8 additions & 3 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import { takeUntil } from 'rxjs/operators/takeUntil';
import { map } from 'rxjs/operators/map';
import { filter } from 'rxjs/operators/filter';

import { login, logout, selectorAuth, routerTransition } from '@app/core';
import {
ActionAuthLogin,
ActionAuthLogout,
selectorAuth,
routerTransition
} from '@app/core';
import { environment as env } from '@env/environment';

import { selectorSettings } from './settings';
Expand Down Expand Up @@ -82,10 +87,10 @@ export class AppComponent implements OnInit, OnDestroy {
}

onLoginClick() {
this.store.dispatch(login());
this.store.dispatch(new ActionAuthLogin());
}

onLogoutClick() {
this.store.dispatch(logout());
this.store.dispatch(new ActionAuthLogout());
}
}
8 changes: 4 additions & 4 deletions src/app/core/auth/auth.effects.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Injectable } from '@angular/core';
import { Action } from '@ngrx/store';
import { Actions, Effect } from '@ngrx/effects';
import { Observable } from 'rxjs/Observable';
import { tap } from 'rxjs/operators/tap';

import { LocalStorageService } from '../local-storage/local-storage.service';
import { Action } from '../core.interfaces';

import { AUTH_KEY, AUTH_LOGIN, AUTH_LOGOUT } from './auth.reducer';
import { AUTH_KEY, AuthActionTypes } from './auth.reducer';

@Injectable()
export class AuthEffects {
Expand All @@ -18,7 +18,7 @@ export class AuthEffects {
@Effect({ dispatch: false })
login(): Observable<Action> {
return this.actions$
.ofType(AUTH_LOGIN)
.ofType(AuthActionTypes.LOGIN)
.pipe(
tap(action =>
this.localStorageService.setItem(AUTH_KEY, { isAuthenticated: true })
Expand All @@ -29,7 +29,7 @@ export class AuthEffects {
@Effect({ dispatch: false })
logout(): Observable<Action> {
return this.actions$
.ofType(AUTH_LOGOUT)
.ofType(AuthActionTypes.LOGOUT)
.pipe(
tap(action =>
this.localStorageService.setItem(AUTH_KEY, { isAuthenticated: false })
Expand Down
32 changes: 21 additions & 11 deletions src/app/core/auth/auth.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import { Action } from '../core.interfaces';
import { Action } from '@ngrx/store';

export const AUTH_KEY = 'AUTH';

export enum AuthActionTypes {
LOGIN = '[Auth] Login',
LOGOUT = '[Auth] Logout'
}

export class ActionAuthLogin implements Action {
readonly type = AuthActionTypes.LOGIN;
}

export class ActionAuthLogout implements Action {
readonly type = AuthActionTypes.LOGOUT;
}

export type AuthActions = ActionAuthLogin | ActionAuthLogout;

export const initialState = {
isAuthenticated: false
};

export const AUTH_KEY = 'AUTH';
export const AUTH_LOGIN = 'AUTH_LOGIN';
export const AUTH_LOGOUT = 'AUTH_LOGOUT';

export const login = () => ({ type: AUTH_LOGIN });
export const logout = () => ({ type: AUTH_LOGOUT });

export const selectorAuth = state => state.auth;

export function authReducer(state = initialState, action: Action) {
export function authReducer(state = initialState, action: AuthActions) {
switch (action.type) {
case AUTH_LOGIN:
case AuthActionTypes.LOGIN:
return Object.assign({}, state, {
isAuthenticated: true
});

case AUTH_LOGOUT:
case AuthActionTypes.LOGOUT:
return Object.assign({}, state, {
isAuthenticated: false
});
Expand Down
4 changes: 0 additions & 4 deletions src/app/core/core.interfaces.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/app/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './local-storage/local-storage.service';
export * from './animations/router.transition';
export * from './auth/auth.reducer';
export * from './core.interfaces';
export * from './core.module';
11 changes: 8 additions & 3 deletions src/app/examples/stock-market/stock-market.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { Store } from '@ngrx/store';
import { Subject } from 'rxjs/Subject';
import { takeUntil } from 'rxjs/operators/takeUntil';

import { actionRetrieveStock, selectorStocks } from './stock-market.reducer';
import {
ActionStockMarketRetrieve,
selectorStocks
} from './stock-market.reducer';

@Component({
selector: 'anms-stock-market',
Expand All @@ -28,7 +31,9 @@ export class StockMarketComponent implements OnInit, OnDestroy {

if (!this.initialized) {
this.initialized = true;
this.store.dispatch(actionRetrieveStock(stocks.symbol));
this.store.dispatch(
new ActionStockMarketRetrieve({ symbol: stocks.symbol })
);
}
});
}
Expand All @@ -39,6 +44,6 @@ export class StockMarketComponent implements OnInit, OnDestroy {
}

onSymbolChange(symbol: string) {
this.store.dispatch(actionRetrieveStock(symbol));
this.store.dispatch(new ActionStockMarketRetrieve({ symbol }));
}
}
33 changes: 17 additions & 16 deletions src/app/examples/stock-market/stock-market.effects.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { Action } from '@ngrx/store';
import { Actions, Effect } from '@ngrx/effects';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
Expand All @@ -9,13 +10,14 @@ import { distinctUntilChanged } from 'rxjs/operators/distinctUntilChanged';
import { switchMap } from 'rxjs/operators/switchMap';
import { catchError } from 'rxjs/operators/catchError';

import { LocalStorageService, Action } from '@app/core';
import { LocalStorageService } from '@app/core';

import {
ActionStockMarketRetrieve,
ActionStockMarketRetrieveError,
ActionStockMarketRetrieveSuccess,
STOCK_MARKET_KEY,
STOCK_MARKET_RETRIEVE,
STOCK_MARKET_RETRIEVE_SUCCESS,
STOCK_MARKET_RETRIEVE_ERROR
StockMarketActionTypes
} from './stock-market.reducer';
import { StockMarketService } from './stock-market.service';

Expand All @@ -29,24 +31,23 @@ export class StockMarketEffects {

@Effect()
retrieveStock(): Observable<Action> {
return this.actions$.ofType(STOCK_MARKET_RETRIEVE).pipe(
tap(action =>
return this.actions$.ofType(StockMarketActionTypes.RETRIEVE).pipe(
tap((action: ActionStockMarketRetrieve) =>
this.localStorageService.setItem(STOCK_MARKET_KEY, {
symbol: action.payload
symbol: action.payload.symbol
})
),
distinctUntilChanged(),
debounceTime(500),
switchMap(action =>
this.service.retrieveStock(action.payload).pipe(
map(stock => ({
type: STOCK_MARKET_RETRIEVE_SUCCESS,
payload: stock
})),
catchError(err =>
of({ type: STOCK_MARKET_RETRIEVE_ERROR, payload: err })
switchMap((action: ActionStockMarketRetrieve) =>
this.service
.retrieveStock(action.payload.symbol)
.pipe(
map(stock => new ActionStockMarketRetrieveSuccess({ stock })),
catchError(error =>
of(new ActionStockMarketRetrieveError({ error }))
)
)
)
)
);
}
Expand Down
58 changes: 40 additions & 18 deletions src/app/examples/stock-market/stock-market.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,65 @@
import { Action } from '@app/core';
import { Action } from '@ngrx/store';
import { HttpErrorResponse } from '@angular/common/http';

export const STOCK_MARKET_KEY = 'EXAMPLES.STOCKS';

export enum StockMarketActionTypes {
RETRIEVE = '[Todos] Retrieve',
RETRIEVE_SUCCESS = '[Todos] Retrieve Success',
RETRIEVE_ERROR = '[Todos] Retrieve Error'
}

export class ActionStockMarketRetrieve implements Action {
readonly type = StockMarketActionTypes.RETRIEVE;
constructor(public payload: { symbol: string }) {}
}

export class ActionStockMarketRetrieveSuccess implements Action {
readonly type = StockMarketActionTypes.RETRIEVE_SUCCESS;
constructor(public payload: { stock: Stock }) {}
}

export class ActionStockMarketRetrieveError implements Action {
readonly type = StockMarketActionTypes.RETRIEVE_ERROR;
constructor(public payload: { error: HttpErrorResponse }) {}
}

export type StockMarketActions =
| ActionStockMarketRetrieve
| ActionStockMarketRetrieveSuccess
| ActionStockMarketRetrieveError;

export const initialState = {
symbol: 'GOOGL'
};

export const STOCK_MARKET_KEY = 'EXAMPLES.STOCKS';
export const STOCK_MARKET_RETRIEVE = 'STOCK_MARKET_RETRIEVE';
export const STOCK_MARKET_RETRIEVE_SUCCESS = 'STOCK_MARKET_RETRIEVE_SUCCESS';
export const STOCK_MARKET_RETRIEVE_ERROR = 'STOCK_MARKET_RETRIEVE_ERROR';

export const actionRetrieveStock = (symbol: string) => ({
type: STOCK_MARKET_RETRIEVE,
payload: symbol
});

export const selectorStocks = state => state.examples.stocks;

export function stockMarketReducer(state = initialState, action: Action) {
export function stockMarketReducer(
state = initialState,
action: StockMarketActions
) {
switch (action.type) {
case STOCK_MARKET_RETRIEVE:
case StockMarketActionTypes.RETRIEVE:
return Object.assign({}, state, {
loading: true,
stock: null,
error: null,
symbol: action.payload
symbol: action.payload.symbol
});

case STOCK_MARKET_RETRIEVE_SUCCESS:
case StockMarketActionTypes.RETRIEVE_SUCCESS:
return Object.assign({}, state, {
loading: false,
stock: action.payload,
stock: action.payload.stock,
error: null
});

case STOCK_MARKET_RETRIEVE_ERROR:
case StockMarketActionTypes.RETRIEVE_ERROR:
return Object.assign({}, state, {
loading: false,
stock: null,
error: action.payload
error: action.payload.error
});

default:
Expand Down
24 changes: 12 additions & 12 deletions src/app/examples/todos/todos.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { takeUntil } from 'rxjs/operators/takeUntil';
import { ANIMATE_ON_ROUTE_ENTER } from '@app/core';

import {
actionAddTodo,
actionPersistTodos,
actionToggleTodo,
actionRemoveDoneTodos,
actionFilterTodos,
ActionTodosAdd,
ActionTodosPersist,
ActionTodosFilter,
ActionTodosRemoveDone,
ActionTodosToggle,
selectorTodos,
Todo,
TodoFilter
TodosFilter
} from './todos.reducer';

@Component({
Expand All @@ -36,7 +36,7 @@ export class TodosComponent implements OnInit, OnDestroy {
.pipe(takeUntil(this.unsubscribe$))
.subscribe(todos => {
this.todos = todos;
this.store.dispatch(actionPersistTodos(todos));
this.store.dispatch(new ActionTodosPersist({ todos }));
});
}

Expand Down Expand Up @@ -72,19 +72,19 @@ export class TodosComponent implements OnInit, OnDestroy {
}

onAddTodo() {
this.store.dispatch(actionAddTodo(this.newTodo));
this.store.dispatch(new ActionTodosAdd({ name: this.newTodo }));
this.newTodo = '';
}

onToggleTodo(todo: Todo) {
this.store.dispatch(actionToggleTodo(todo.id));
this.store.dispatch(new ActionTodosToggle({ id: todo.id }));
}

onRemoveDoneTodos() {
this.store.dispatch(actionRemoveDoneTodos());
this.store.dispatch(new ActionTodosRemoveDone());
}

onFilterTodos(filter: TodoFilter) {
this.store.dispatch(actionFilterTodos(filter));
onFilterTodos(filter: TodosFilter) {
this.store.dispatch(new ActionTodosFilter({ filter }));
}
}
15 changes: 10 additions & 5 deletions src/app/examples/todos/todos.effects.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Injectable } from '@angular/core';
import { Action } from '@ngrx/store';
import { Actions, Effect } from '@ngrx/effects';
import { Observable } from 'rxjs/Observable';
import { tap } from 'rxjs/operators/tap';

import { LocalStorageService, Action } from '@app/core';
import { LocalStorageService } from '@app/core';

import { TODOS_KEY, TODOS_PERSIST } from './todos.reducer';
import {
ActionTodosPersist,
TODOS_KEY,
TodosActionTypes
} from './todos.reducer';

@Injectable()
export class TodosEffects {
Expand All @@ -17,10 +22,10 @@ export class TodosEffects {
@Effect({ dispatch: false })
persistTodos(): Observable<Action> {
return this.actions$
.ofType(TODOS_PERSIST)
.ofType(TodosActionTypes.PERSIST)
.pipe(
tap(action =>
this.localStorageService.setItem(TODOS_KEY, action.payload)
tap((action: ActionTodosPersist) =>
this.localStorageService.setItem(TODOS_KEY, action.payload.todos)
)
);
}
Expand Down

0 comments on commit 98435ed

Please sign in to comment.