Skip to content

Instantly share code, notes, and snippets.

@Gethyl
Last active December 20, 2016 06:25
Show Gist options
  • Save Gethyl/548e3ad1580a817beaf2fc3b6b07a2bb to your computer and use it in GitHub Desktop.
Save Gethyl/548e3ad1580a817beaf2fc3b6b07a2bb to your computer and use it in GitHub Desktop.
export const AddItem = (item) => ({
type: "ADD_ITEM",
item: item
})
export const DeleteItem = (id) => ({
type: "DELETE_ITEM",
itemId: id
})
export const EditIconClicked = (id) => ({
type: "EDIT_ICON_CLICKED",
itemId: id,
editItem: true
})
export const EditItem = (id,editedText) => ({
type: "EDIT_ITEM",
itemId: id,
editItem: false,
item:editedText
})
export const CompleteItem = (id, completedFlag) => ({
type: "COMPLETED_ITEM",
itemId: id,
completed:completedFlag
})
import update from 'immutability-helper';
const initialState = { items:[]}
//const windowState = window.__REDUX_STATE__
let id = 0;
const todoReducer = (state=initialState, action) => { //(state=initialState, action) => {
switch (action.type) {
case 'ADD_ITEM':
return {
...state,
items: state.items.concat({id:id++,text:action.item,editItem:false,completed:false})
}
case 'DELETE_ITEM':
return {
...state,
items:update(state.items, {$splice: [[action.itemId, 1]]})
}
case 'EDIT_ICON_CLICKED':
return {
...state,
items:update(state.items, {[action.itemId]: {editItem: {$set: true}}})
}
case 'EDIT_ITEM':
return {
...state,
items:update(state.items, {[action.itemId]: {editItem: {$set: false},text: {$set: action.item}}})
}
case 'COMPLETED_ITEM':
return {
...state,
items:update(state.items, {[action.itemId]: {completed: {$set: action.completed}}})
}
default:
return state
}
}
export default todoReducer;
import { combineReducers } from 'redux'
import todoReducer from './ToDoReducers'
const allReducers = combineReducers({
todoReducer
})
export default allReducers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment