Created
December 5, 2019 23:02
-
-
Save shanebdavis/9e67be8a0874a4c295001ba6e91f79e2 to your computer and use it in GitHub Desktop.
redux toolkit core todos-with-toggls-and-completes
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 { createSlice } from '@reduxjs/toolkit' | |
export const VisibilityFilters = { | |
SHOW_ALL: 'SHOW_ALL', | |
SHOW_COMPLETED: 'SHOW_COMPLETED', | |
SHOW_ACTIVE: 'SHOW_ACTIVE' | |
} | |
const filtersSlice = createSlice({ | |
name: 'visibilityFilters', | |
initialState: VisibilityFilters.SHOW_ALL, | |
reducers: { | |
setVisibilityFilter(state, action) { | |
return action.payload | |
} | |
} | |
}) | |
export const { setVisibilityFilter } = filtersSlice.actions | |
export default filtersSlice.reducer | |
// todosSlice.js | |
import { createSlice } from '@reduxjs/toolkit' | |
let nextTodoId = 0 | |
const todosSlice = createSlice({ | |
name: 'todos', | |
initialState: [], | |
reducers: { | |
addTodo: { | |
reducer(state, action) { | |
const { id, text } = action.payload | |
state.push({ id, text, completed: false }) | |
}, | |
prepare(text) { | |
return { payload: { text, id: nextTodoId++ } } | |
} | |
}, | |
toggleTodo(state, action) { | |
const todo = state.find(todo => todo.id === action.payload) | |
if (todo) { | |
todo.completed = !todo.completed | |
} | |
} | |
} | |
}) | |
export const { addTodo, toggleTodo } = todosSlice.actions | |
export default todosSlice.reducer | |
// VisibleTodoList.js | |
import { connect } from 'react-redux' | |
import { createSelector } from '@reduxjs/toolkit' | |
import { toggleTodo } from 'features/todos/todosSlice' | |
import TodoList from './TodoList' | |
import { VisibilityFilters } from 'features/filters/filtersSlice' | |
const selectTodos = state => state.todos | |
const selectFilter = state => state.visibilityFilter | |
const selectVisibleTodos = createSelector( | |
[selectTodos, selectFilter], | |
(todos, filter) => { | |
switch (filter) { | |
case VisibilityFilters.SHOW_ALL: | |
return todos | |
case VisibilityFilters.SHOW_COMPLETED: | |
return todos.filter(t => t.completed) | |
case VisibilityFilters.SHOW_ACTIVE: | |
return todos.filter(t => !t.completed) | |
default: | |
throw new Error('Unknown filter: ' + filter) | |
} | |
} | |
) | |
const mapStateToProps = state => ({ | |
todos: selectVisibleTodos(state) | |
}) | |
const mapDispatchToProps = { toggleTodo } | |
export default connect( | |
mapStateToProps, | |
mapDispatchToProps | |
)(TodoList) | |
// TodoList | |
import React from 'react' | |
import Todo from './Todo' | |
const TodoList = ({ todos, toggleTodo }) => ( | |
<ul> | |
{todos.map(todo => ( | |
<Todo key={todo.id} {...todo} onClick={() => toggleTodo(todo.id)} /> | |
))} | |
</ul> | |
) | |
export default TodoList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment