Skip to content

Commit

Permalink
feat(ui): implement list controls - items left and clear completed fu…
Browse files Browse the repository at this point in the history
…nctionality
  • Loading branch information
bsbodden committed Feb 23, 2021
1 parent 0352de4 commit b7c9c37
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/webapp/src/components/todo_list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useContext, useMemo } from 'react';
import { TodosContext } from "../context/todos_context";
import Todo from './todo';
import TodoListControls from './todo_list_controls';

const TodoList = function () {
const { todos, toggleAll } = useContext(TodosContext);
Expand All @@ -21,6 +22,7 @@ const TodoList = function () {
<Todo key={todo.id} id={todo.id} />
))}
</ul>
<TodoListControls />
</section >
);
}
Expand Down
39 changes: 39 additions & 0 deletions src/main/webapp/src/components/todo_list_controls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, { useMemo, useContext } from 'react';
import { TodosContext } from "../context/todos_context";

const TodoListControls = function (props) {
const { todos, clearCompleted } = useContext(TodosContext);

const itemsLeft = useMemo(() => todos.reduce((p, c) => p + (c.completed ? 0 : 1), 0), [
todos
]);

const itemsCompleted = useMemo(() => todos.length - itemsLeft, [
todos, itemsLeft
]);

return (
<footer className="footer">
{/* This should be `0 items left` by default */}
<span className="todo-count"><strong>{itemsLeft}</strong> {itemsLeft != 1 ? "items" : "item"} left</span>
{/* Remove this if you don't implement routing */}
<ul className="filters">
<li>
<a className="selected" href="#/">All</a>
</li>
<li>
<a href="#/active">Active</a>
</li>
<li>
<a href="#/completed">Completed</a>
</li>
</ul>
{/* Hidden if no completed items are left ↓ */}
{itemsCompleted > 0 &&
<button className="clear-completed" onClick={clearCompleted}>Clear completed</button>
}
</footer>
);
}

export default TodoListControls;

0 comments on commit b7c9c37

Please sign in to comment.