Created
December 5, 2019 23:00
-
-
Save shanebdavis/ce02b4495f1bc0afa830796f58124604 to your computer and use it in GitHub Desktop.
hooks-for-redux core todo-with-filters-and-competes code
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
// filtersSlice.js | |
import { useRedux } from 'hooks-for-redux' | |
export const filters = { | |
all: () => true, | |
completed: t => t.completed, | |
active: t => !t.completed | |
} | |
export const [useFilters, { setVisibilityFilter }] = useRedux( | |
'visibilityFilters', | |
filters.all, | |
{ | |
setVisibilityFilter: (__, filter) => { | |
console.log("setVis: " + filter) | |
return filters[filter] || filters.all | |
} | |
} | |
) | |
// todosSlice.js | |
import { useRedux } from 'hooks-for-redux' | |
export const [useTodos, { addTodo, toggleTodo, resetTodos }, todos] = | |
useRedux('todos', [], { | |
resetTodos: () => { nextTodoId = 0; return [] }, | |
addTodo: (todos, text) => | |
[...todos, { text, completed: false, id: nextTodoId++ }], | |
toggleTodo: (todos, id) => | |
todos.map(todo => | |
todo.id === id ? { ...todo, completed: !todo.completed } : todo | |
) | |
}) | |
// VisibleTodoList.js | |
import React from 'react' | |
import Todo from './Todo' | |
import { useFilters } from '../filters/filtersSlice' | |
import { useTodos } from './todosSlice' | |
const VisibleTodoList = () => | |
<ul> | |
{useTodos() | |
.filter(useFilters()) | |
.map(todo => ( | |
<Todo key={todo.id} {...todo} /> | |
))} | |
</ul> | |
export default VisibleTodoList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment