-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75ec80c
commit cf456b2
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { useContext } from 'react'; | ||
import { TodoContext } from '../context/TodoContext'; | ||
import { TodosFilter } from './TodoFilter'; | ||
|
||
export const Footer: React.FC = () => { | ||
const { todos, setTodos } = useContext(TodoContext); | ||
|
||
const itemsLeft = todos.filter(todo => !todo.completed).length; | ||
|
||
const handleClearCompleted = () => { | ||
// Filter out completed todos and update the state | ||
const uncompletedTodos = todos.filter(todo => !todo.completed); | ||
|
||
setTodos(uncompletedTodos); | ||
}; | ||
|
||
return ( | ||
<footer className="footer"> | ||
<span className="todo-count" data-cy="todosCounter"> | ||
{`${itemsLeft} item${itemsLeft !== 1 ? 's' : ''} left`} | ||
</span> | ||
|
||
<TodosFilter /> | ||
|
||
<button | ||
type="button" | ||
className="clear-completed" | ||
onClick={handleClearCompleted} | ||
> | ||
Clear completed | ||
</button> | ||
</footer> | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// TodosFilter.tsx | ||
import React, { useState } from 'react'; | ||
|
||
enum Status { | ||
All = 'All', | ||
Active = 'Active', | ||
Completed = 'Completed', | ||
} | ||
|
||
export const TodosFilter: React.FC = () => { | ||
const [filter, setFilter] = useState<Status>(Status.All); | ||
|
||
const handleFilterChange = (status: Status) => { | ||
setFilter(status); | ||
}; | ||
|
||
return ( | ||
<ul className="filters"> | ||
<li> | ||
<a | ||
href="#/" | ||
className={filter === Status.All ? 'active' : ''} | ||
onClick={() => handleFilterChange(Status.All)} | ||
> | ||
All | ||
</a> | ||
</li> | ||
<li> | ||
<a | ||
href="#/active" | ||
onClick={() => handleFilterChange(Status.Active)} | ||
> | ||
Active | ||
</a> | ||
</li> | ||
<li> | ||
<a | ||
href="#/completed" | ||
onClick={() => handleFilterChange(Status.Completed)} | ||
> | ||
Completed | ||
</a> | ||
</li> | ||
</ul> | ||
); | ||
}; |