Created
May 26, 2019 06:44
-
-
Save rafaelrozon/98cbdac7aeabd2ddb22d9fd72d77a019 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { makeActionCreator, makeReducer, composeReducers } from 'redux-toolbelt'; | |
import { clone } from 'ramda'; | |
import cuid from 'cuid'; | |
import { DEFAULT_STATE } from './constants'; | |
const makeAction = makeActionCreator.withDefaults({prefix: "@todo/"}); | |
export const actions = { | |
add: makeAction('ADD'), | |
toggle: makeAction('TOGGLE'), | |
changeFilter: makeAction('CHANGE_FILTER') | |
}; | |
// Reducers / | |
export const handleAdd = makeReducer(actions.add, (state = DEFAULT_STATE, action) => { | |
const todo = { | |
id: cuid(), | |
title: action.payload.title, | |
completed: false | |
}; | |
const newTodos = state.todos.concat(todo); | |
return { ...state, todos: newTodos }; | |
}); | |
export const handleToggle = makeReducer(actions.toggle, (state = DEFAULT_STATE, action) => { | |
return { | |
...state, | |
todos: state.todos.map(td => { | |
if (td.id === action.payload.id) { | |
const completedTodo = clone(td); | |
completedTodo.completed = !td.completed; | |
return completedTodo; | |
} | |
return td; | |
}) | |
} | |
}); | |
export const handleChangeFitler = makeReducer(actions.changeFilter, (state = DEFAULT_STATE, action) => { | |
return { | |
...state, | |
todosFilter: action.payload | |
}; | |
}); | |
export const reducer = composeReducers(handleAdd, handleToggle, handleChangeFitler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment