Created
April 9, 2018 18:56
-
-
Save Kavignon/70f761f5aabf00039426e1f85deb8e6e to your computer and use it in GitHub Desktop.
Built Todo list application with fundamental concepts in React & Redux
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
import createStore from "redux"; | |
import combineReducers from "redux" | |
const todoItemCategoryAction = { | |
Add: "ADD_TODO", | |
Remove: "REMOVE_TODO", | |
Toggle: "TOGGLE_TODO", | |
ShowAll: "SHOW_ALL", | |
ShowCompleted: "SHOW_COMPLETED", | |
SetVisibilityFilter: "SET_VISIBILITY_FILTER" | |
} | |
const handleTodoItem = (state, action) => { | |
switch(action.type) { | |
case todoItemCategoryAction.Add: | |
return { | |
id: action.id, | |
text: action.text, | |
isDone: false | |
}; | |
case todoItemCategoryAction.Toggle: | |
if (state.id !== action.id){ | |
return state | |
} | |
return Object.assign({}, state, { | |
isDone: !state.isDone | |
}) | |
case todoItemCategoryAction.ShowAll: | |
return state; | |
case todoItemCategoryAction.ShowCompleted: | |
if(state.isDone) | |
return state; | |
break; | |
default: | |
return state; | |
} | |
} | |
const handleTodoItems = (state = [], action) => { | |
switch(action.type) { | |
case todoItemCategoryAction.Add: | |
return [ | |
...state, | |
handleTodoItem(undefined, action) | |
]; | |
case todoItemCategoryAction.Toggle: | |
return state.map(t => handleTodoItem(t, action)); | |
default: | |
return state; | |
} | |
} | |
const applyVisibilityFilter = ( | |
state = todoItemCategoryAction.ShowAll, | |
action | |
) => { | |
switch(action.type){ | |
case todoItemCategoryAction.SetVisibilityFilter: | |
return action.filter; | |
default: | |
return state; | |
} | |
}; | |
const todoListApp = (state = {}, action) => { | |
return { | |
todoItems: handleTodoItems( | |
state.todoItems, | |
action | |
), | |
visibilityFilter: applyVisibilityFilter( | |
state.visibilityFilter, | |
action | |
) | |
} | |
} | |
const todoListApplication = combineReducers({ | |
handleTodoItems, | |
applyVisibilityFilter | |
}) | |
const store = createStore(todoListApplication) | |
const loggingStoreActiveState = (action) => { | |
console.log("Current state: "); | |
console.log(store.getState()); | |
console.log("--------------------"); | |
} | |
loggingStoreActiveState() | |
console.log("DISPATCHING ADD TODO") | |
store.dispatch({ | |
type: todoItemCategoryAction.Add, | |
id: 0, | |
text: "My first action dispatch" | |
}); | |
loggingStoreActiveState() | |
console.log | |
store.dispatch({ | |
type: todoItemCategoryAction.Add, | |
id: 0, | |
text: "Coding this dispatcher pretty well now" | |
}); | |
loggingStoreActiveState() | |
console.log("DISPATCHING TOGGLE TODO") | |
store.dispatch({ | |
type: todoItemCategoryAction.Toggle, | |
id: 0 | |
}) | |
loggingStoreActiveState() | |
console.log("DISPATCHING TOGGLE TODO") | |
store.dispatch({ | |
type: todoItemCategoryAction.Toggle, | |
id: 1 | |
}) | |
loggingStoreActiveState() | |
console.log("DISPATCHING SET VISISBILITY FILTER") | |
store.dispatch({ | |
type: todoItemCategoryAction.SetVisibilityFilter, | |
filter: todoItemCategoryAction.ShowCompleted | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment