This repository has been archived by the owner on Jul 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
assemble redux stuffs, merge actions and reducers in a single file.
- Loading branch information
Showing
8 changed files
with
84 additions
and
68 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import {createAction, handleActions } from 'redux-actions' | ||
|
||
const ADD_TODO = 'add todo' | ||
const DELETE_TODO = 'delete todo' | ||
const EDIT_TODO = 'edit todo' | ||
const COMPLETE_TODO = 'complete todo' | ||
const COMPLETE_ALL = 'complete all' | ||
const CLEAR_COMPLETE = 'clear complete' | ||
|
||
// Actions | ||
const addTodo = createAction(ADD_TODO) | ||
const deleteTodo = createAction(DELETE_TODO) | ||
const editTodo = createAction(EDIT_TODO) | ||
const completeTodo = createAction(COMPLETE_TODO) | ||
const completeAll = createAction(COMPLETE_ALL) | ||
const clearCompleted = createAction(CLEAR_COMPLETE) | ||
|
||
// Exposed Actions | ||
export const actions = { | ||
addTodo, | ||
deleteTodo, | ||
editTodo, | ||
completeTodo, | ||
completeAll, | ||
clearCompleted, | ||
} | ||
|
||
// Reducers | ||
const initialState = [{ | ||
text: 'Use Redux', | ||
completed: false, | ||
id: 0 | ||
}] | ||
|
||
export default handleActions({ | ||
[ADD_TODO] (state, action) { | ||
return [{ | ||
id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1, | ||
completed: false, | ||
text: action.payload | ||
}, ...state] | ||
}, | ||
|
||
[DELETE_TODO] (state, action) { | ||
return state.filter(todo => todo.id !== action.payload ) | ||
}, | ||
|
||
[EDIT_TODO] (state, action) { | ||
return state.map(todo => { | ||
return todo.id === action.payload.id | ||
? { ...todo, text: action.payload.text } | ||
: todo | ||
}) | ||
}, | ||
|
||
[COMPLETE_TODO] (state, action) { | ||
return state.map(todo => { | ||
return todo.id === action.payload | ||
? { ...todo, completed: !todo.completed } | ||
: todo | ||
}) | ||
}, | ||
|
||
[COMPLETE_ALL] (state, action) { | ||
const areAllMarked = state.every(todo => todo.completed) | ||
return state.map(todo => { | ||
return { | ||
...todo, | ||
completed: !areAllMarked | ||
} | ||
}) | ||
}, | ||
|
||
[CLEAR_COMPLETE] (state, action) { | ||
return state.filter(todo => todo.completed === false) | ||
} | ||
}, initialState) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters