Skip to content

Instantly share code, notes, and snippets.

@codebucks27
Created April 22, 2021 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebucks27/b617d56ed6bc9717c77c4ff6bbeda268 to your computer and use it in GitHub Desktop.
Save codebucks27/b617d56ed6bc9717c77c4ff6bbeda268 to your computer and use it in GitHub Desktop.
import React, { useState } from "react";
import { connect } from "react-redux";
import {
addTodos,
completeTodos,
removeTodos,
updateTodos,
} from "../redux/reducer";
import TodoItem from "./TodoItem";
const mapStateToProps = (state) => {
return {
todos: state,
};
};
const mapDispatchToProps = (dispatch) => {
return {
addTodo: (obj) => dispatch(addTodos(obj)),
removeTodo: (id) => dispatch(removeTodos(id)),
updateTodo: (obj) => dispatch(updateTodos(obj)),
completeTodo: (id) => dispatch(completeTodos(id)),
};
};
const DisplayTodos = (props) => {
const [sort, setSort] = useState("active");
return (
<div className="displaytodos">
<div className="buttons">
<button
onClick={() => setSort("active")}
>
Active
</button>
<button
onClick={() => setSort("completed")}
>
Completed
</button>
<button
onClick={() => setSort("all")}
>
All
</button>
</div>
<ul>
{props.todos.length > 0 && sort === "active"
? props.todos.map((item) => {
return (
item.completed === false && (
<TodoItem
key={item.id}
item={item}
removeTodo={props.removeTodo}
updateTodo={props.updateTodo}
completeTodo={props.completeTodo}
/>
)
);
})
: null}
{/* for completed items */}
{props.todos.length > 0 && sort === "completed"
? props.todos.map((item) => {
return (
item.completed === true && (
<TodoItem
key={item.id}
item={item}
removeTodo={props.removeTodo}
updateTodo={props.updateTodo}
completeTodo={props.completeTodo}
/>
)
);
})
: null}
{/* for all items */}
{props.todos.length > 0 && sort === "all"
? props.todos.map((item) => {
return (
<TodoItem
key={item.id}
item={item}
removeTodo={props.removeTodo}
updateTodo={props.updateTodo}
completeTodo={props.completeTodo}
/>
);
})
: null}
</ul>
</div>
);
};
export default connect(mapStateToProps, mapDispatchToProps)(DisplayTodos);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment