Skip to content

Commit

Permalink
feat(codestyle): add state interfaces and use object spread instead o…
Browse files Browse the repository at this point in the history
…f object assign to type check new state creation in reducers
  • Loading branch information
tomastrajan committed Jan 17, 2018
1 parent 002942d commit 2adb6e9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 37 deletions.
19 changes: 11 additions & 8 deletions src/app/core/auth/auth.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,28 @@ export class ActionAuthLogout implements Action {

export type AuthActions = ActionAuthLogin | ActionAuthLogout;

export const initialState = {
export const initialState: AuthState = {
isAuthenticated: false
};

export const selectorAuth = state => state.auth;

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

case AuthActionTypes.LOGOUT:
return Object.assign({}, state, {
isAuthenticated: false
});
return { ...state, isAuthenticated: false };

default:
return state;
}
}

export interface AuthState {
isAuthenticated: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ export function initStateFromLocalStorage(
return function(state, action) {
const newState = reducer(state, action);
if ([INIT.toString(), UPDATE.toString()].includes(action.type)) {
return Object.assign(
{},
newState,
LocalStorageService.loadInitialState()
);
return { ...newState, ...LocalStorageService.loadInitialState() };
}
return newState;
};
Expand Down
31 changes: 21 additions & 10 deletions src/app/examples/stock-market/stock-market.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,42 @@ export type StockMarketActions =
| ActionStockMarketRetrieveSuccess
| ActionStockMarketRetrieveError;

export const initialState = {
symbol: 'GOOGL'
export const initialState: StockMarketState = {
symbol: 'GOOGL',
loading: false
};

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

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

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

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

default:
return state;
Expand All @@ -74,3 +78,10 @@ export interface Stock {
ccy: string;
change: string;
}

export interface StockMarketState {
symbol: string;
loading: boolean;
stock?: Stock;
error?: HttpErrorResponse;
}
33 changes: 22 additions & 11 deletions src/app/examples/todos/todos.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export type TodosActions =
| ActionTodosFilter
| ActionTodosPersist;

export type TodosFilter = 'ALL' | 'DONE' | 'ACTIVE';

export const initialState = {
export const initialState: TodosState = {
items: [
{ id: uuid(), name: 'Open Todo list example', done: true },
{ id: uuid(), name: 'Check the other examples', done: false },
Expand All @@ -59,32 +57,38 @@ export const initialState = {

export const selectorTodos = state => state.examples.todos;

export function todosReducer(state = initialState, action: TodosActions) {
export function todosReducer(
state: TodosState = initialState,
action: TodosActions
): TodosState {
switch (action.type) {
case TodosActionTypes.ADD:
return Object.assign({}, state, {
return {
...state,
items: state.items.concat({
id: uuid(),
name: action.payload.name,
done: false
})
});
};

case TodosActionTypes.TOGGLE:
return Object.assign({}, state, {
return {
...state,
items: state.items.map(
(item: Todo) =>
item.id === action.payload.id ? { ...item, done: !item.done } : item
)
});
};

case TodosActionTypes.REMOVE_DONE:
return Object.assign({}, state, {
return {
...state,
items: state.items.filter((item: Todo) => !item.done)
});
};

case TodosActionTypes.FILTER:
return Object.assign({}, state, { filter: action.payload.filter });
return { ...state, filter: action.payload.filter };

default:
return state;
Expand All @@ -96,3 +100,10 @@ export interface Todo {
name: string;
done: boolean;
}

export type TodosFilter = 'ALL' | 'DONE' | 'ACTIVE';

export interface TodosState {
items: Todo[];
filter: TodosFilter;
}
13 changes: 10 additions & 3 deletions src/app/settings/settings.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@ export class ActionSettingsChangeTheme implements Action {

export type SettingsActions = ActionSettingsChangeTheme;

export const initialState = {
export const initialState: SettingsState = {
theme: 'DEFAULT-THEME'
};

export const selectorSettings = state => state.settings || { theme: '' };

export function settingsReducer(state = initialState, action: SettingsActions) {
export function settingsReducer(
state: SettingsState = initialState,
action: SettingsActions
): SettingsState {
switch (action.type) {
case SettingsActionTypes.CHANGE_THEME:
return { theme: action.payload.theme };
return { ...state, theme: action.payload.theme };

default:
return state;
}
}

export interface SettingsState {
theme: string;
}

0 comments on commit 2adb6e9

Please sign in to comment.