Skip to content

Commit

Permalink
improve code according to mentor's suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
4xmplme committed Nov 27, 2023
1 parent 98c6b48 commit 3522f3c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/Footer/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const Footer: React.FC = () => {

return (
<>
{!!todos.length && (
{todos.length > 0 && (
<footer className="footer">
<span className="todo-count" data-cy="todosCounter">
{`${todosToComplete} items left`}
Expand Down
15 changes: 8 additions & 7 deletions src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@ type Props = {

export const TodoList: React.FC<Props> = React.memo(({ todos }) => {
const { filter } = useContext(StateContext);

const isDisplayed = (todo: Todo) => {
const filterTodos = (todosToFilter: Todo[]) => {
switch (filter.title) {
case 'All':
default:
return true;
return todosToFilter;

case 'Active':
return !todo.completed;
return todosToFilter.filter(todo => !todo.completed);

case 'Completed':
return todo.completed;
return todosToFilter.filter(todo => todo.completed);
}
};

const filteredTodos = filterTodos(todos);

return (
<>
{!!todos.length && (
{todos.length > 0 && (
<ul className="todo-list" data-cy="todosList">
{todos.map(todo => isDisplayed(todo) && (
{filteredTodos.map(todo => (
<TodoItem key={todo.id} todo={todo} />
))}
</ul>
Expand Down

0 comments on commit 3522f3c

Please sign in to comment.