-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
38 additions
and
89 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,42 +1,8 @@ | ||
import * as types from '../constants/ActionTypes'; | ||
|
||
export function addTodo(text) { | ||
return { | ||
type: types.ADD_TODO, | ||
text | ||
}; | ||
} | ||
|
||
export function deleteTodo(id) { | ||
return { | ||
type: types.DELETE_TODO, | ||
id | ||
}; | ||
} | ||
|
||
export function editTodo(id, text) { | ||
return { | ||
type: types.EDIT_TODO, | ||
id, | ||
text | ||
}; | ||
} | ||
|
||
export function markTodo(id) { | ||
return { | ||
type: types.MARK_TODO, | ||
id | ||
}; | ||
} | ||
|
||
export function markAll() { | ||
return { | ||
type: types.MARK_ALL | ||
}; | ||
} | ||
|
||
export function clearMarked() { | ||
return { | ||
type: types.CLEAR_MARKED | ||
}; | ||
} | ||
import { createAction } from 'redux-act'; | ||
|
||
export const addTodo = createAction('Add new todo'); | ||
export const deleteTodo = createAction('Delete todo'); | ||
export const editTodo = createAction('Edit todo', (id, text)=> ({id, text})); | ||
export const markTodo = createAction('Mark todo'); | ||
export const markAll = createAction('Mark all todos'); | ||
export const clearMarked = createAction('Clear all marked todos'); |
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 |
---|---|---|
@@ -1,6 +1,2 @@ | ||
export const ADD_TODO = 'ADD_TODO'; | ||
export const DELETE_TODO = 'DELETE_TODO'; | ||
export const EDIT_TODO = 'EDIT_TODO'; | ||
export const MARK_TODO = 'MARK_TODO'; | ||
export const MARK_ALL = 'MARK_ALL'; | ||
export const CLEAR_MARKED = 'CLEAR_MARKED'; | ||
// Remove all action constants | ||
// but keeping the file so it appears on the git diff |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// You can still keep constant for other purposes of course | ||
export const SHOW_ALL = 'show_all'; | ||
export const SHOW_MARKED = 'show_marked'; | ||
export const SHOW_UNMARKED = 'show_unmarked'; |
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 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 |
---|---|---|
@@ -1,50 +1,32 @@ | ||
import { ADD_TODO, DELETE_TODO, EDIT_TODO, MARK_TODO, MARK_ALL, CLEAR_MARKED } from '../constants/ActionTypes'; | ||
import { createReducer } from 'redux-act'; | ||
import * as actions from '../actions/TodoActions'; | ||
|
||
const initialState = [{ | ||
text: 'Use Redux', | ||
marked: false, | ||
id: 0 | ||
}]; | ||
|
||
export default function todos(state = initialState, action) { | ||
switch (action.type) { | ||
case ADD_TODO: | ||
return [{ | ||
id: (state.length === 0) ? 0 : state[0].id + 1, | ||
marked: false, | ||
text: action.text | ||
}, ...state]; | ||
export default createReducer({ | ||
[actions.addTodo]: (state, text)=> [{ | ||
id: (state.length === 0) ? 0 : state[0].id + 1, | ||
marked: false, | ||
text: text | ||
}, ...state], | ||
|
||
case DELETE_TODO: | ||
return state.filter(todo => | ||
todo.id !== action.id | ||
); | ||
[actions.deleteTodo]: (state, id)=> state.filter(todo=> todo.id !== id), | ||
|
||
case EDIT_TODO: | ||
return state.map(todo => | ||
todo.id === action.id ? | ||
{ ...todo, text: action.text } : | ||
todo | ||
); | ||
[actions.editTodo]: (state, todo)=> state.map(t=> todo.id === t.id ? {...t, text: todo.text} : t), | ||
|
||
case MARK_TODO: | ||
return state.map(todo => | ||
todo.id === action.id ? | ||
{ ...todo, marked: !todo.marked } : | ||
todo | ||
); | ||
[actions.markTodo]: (state, id)=> state.map(todo=> todo.id === id ? {...todo, marked: !todo.marked} : todo), | ||
|
||
case MARK_ALL: | ||
[actions.markAll]: (state)=> { | ||
const areAllMarked = state.every(todo => todo.marked); | ||
return state.map(todo => ({ | ||
...todo, | ||
marked: !areAllMarked | ||
})); | ||
}, | ||
|
||
case CLEAR_MARKED: | ||
return state.filter(todo => todo.marked === false); | ||
|
||
default: | ||
return state; | ||
} | ||
} | ||
[actions.clearMarked]: (state)=> state.filter(todo => todo.marked === false) | ||
}, initialState); |
66a0791
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are you talking about @thinkpadder1 ?
addTodo
andmarkAll
are not one liner at all. And the main point isn't about the number of lines anyway, it's about removing the bigswitch
and constant string types and replacing it with references to the actions themselves. As stated in the README, the main goal is not to remove boilerplate, it's just a nice side effect.66a0791
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would this work with thunks?
66a0791
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure think, no problem using thunks.