Skip to content
Permalink
Browse files

Made changes to the todo mvc example to use react-safe-actions to sho…

…w how the library can be used.
  • Loading branch information...
thaggie committed Jul 12, 2016
1 parent 994a79e commit 67e3e87762d254acffdaf7ac949253286074efc8
@@ -1,25 +1,23 @@
import * as types from '../constants/ActionTypes'
import rsa from 'react-safe-actions'

export function addTodo(text) {
return { type: types.ADD_TODO, text }
}
export const addTodo = rsa.create(types.ADD_TODO, {
text: rsa.types.string.isRequired
})

export function deleteTodo(id) {
return { type: types.DELETE_TODO, id }
}
export const deleteTodo = rsa.create(types.DELETE_TODO, {
id: rsa.types.number.isRequired
})

export function editTodo(id, text) {
return { type: types.EDIT_TODO, id, text }
}
export const editTodo = rsa.create(types.EDIT_TODO, {
id: rsa.types.number.isRequired,
text: rsa.types.string.isRequired
})

export function completeTodo(id) {
return { type: types.COMPLETE_TODO, id }
}
export const completeTodo = rsa.create(types.COMPLETE_TODO, {
id: rsa.types.number.isRequired
})

export function completeAll() {
return { type: types.COMPLETE_ALL }
}
export const completeAll = rsa.create(types.COMPLETE_ALL)

export function clearCompleted() {
return { type: types.CLEAR_COMPLETED }
}
export const clearCompleted = rsa.create(types.CLEAR_COMPLETED)
@@ -18,7 +18,7 @@ class TodoItem extends Component {
if (text.length === 0) {
this.props.deleteTodo(id)
} else {
this.props.editTodo(id, text)
this.props.editTodo({ id, text })
}
this.setState({ editing: false })
}
@@ -22,6 +22,7 @@
"react": "^0.14.7",
"react-dom": "^0.14.7",
"react-redux": "^4.2.1",
"react-safe-actions": "^0.1.0",
"redux": "^3.2.1"
},
"devDependencies": {
@@ -15,26 +15,26 @@ export default function todos(state = initialState, action) {
{
id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,
completed: false,
text: action.text
},
text: action.payload.text
},
...state
]

case DELETE_TODO:
return state.filter(todo =>
todo.id !== action.id
todo.id !== action.payload.id
)

case EDIT_TODO:
return state.map(todo =>
todo.id === action.id ?
Object.assign({}, todo, { text: action.text }) :
todo.id === action.payload.id ?
Object.assign({}, todo, { text: action.payload.text }) :
todo
)

case COMPLETE_TODO:
return state.map(todo =>
todo.id === action.id ?
todo.id === action.payload.id ?
Object.assign({}, todo, { completed: !todo.completed }) :
todo
)
@@ -6,29 +6,37 @@ describe('todo actions', () => {
it('addTodo should create ADD_TODO action', () => {
expect(actions.addTodo('Use Redux')).toEqual({
type: types.ADD_TODO,
text: 'Use Redux'
payload: {
text: 'Use Redux'
}
})
})

it('deleteTodo should create DELETE_TODO action', () => {
expect(actions.deleteTodo(1)).toEqual({
type: types.DELETE_TODO,
id: 1
payload: {
id: 1
}
})
})

it('editTodo should create EDIT_TODO action', () => {
expect(actions.editTodo(1, 'Use Redux everywhere')).toEqual({
expect(actions.editTodo({ id:1, text:'Use Redux everywhere' })).toEqual({
type: types.EDIT_TODO,
id: 1,
text: 'Use Redux everywhere'
payload: {
id: 1,
text: 'Use Redux everywhere'
}
})
})

it('completeTodo should create COMPLETE_TODO action', () => {
expect(actions.completeTodo(1)).toEqual({
type: types.COMPLETE_TODO,
id: 1
payload: {
id: 1
}
})
})

0 comments on commit 67e3e87

Please sign in to comment.
You can’t perform that action at this time.