-
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
5381a7d
commit b90c37f
Showing
9 changed files
with
461 additions
and
145 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
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,56 @@ | ||
import React from 'react'; | ||
import { FilterTypes } from '../types/FilterType'; | ||
import cn from 'classnames'; | ||
import { useTodoContext } from '../Context/TodoProvider'; | ||
|
||
type FooterProps = { | ||
selectedFilter: FilterTypes; | ||
setSelectedFilter: (filter: FilterTypes) => void; | ||
}; | ||
|
||
const Footer: React.FC<FooterProps> = ({ | ||
selectedFilter, | ||
setSelectedFilter, | ||
}) => { | ||
const { todoItems, clearCompletedTodos } = useTodoContext(); | ||
|
||
const countItemsLeft = todoItems.reduce((accumulator, value) => { | ||
return accumulator + (value.completed ? 0 : 1); | ||
}, 0); | ||
|
||
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{countItemsLeft} items left | ||
</span> | ||
|
||
<nav className="filter" data-cy="Filter"> | ||
{Object.values(FilterTypes).map(filter => ( | ||
<a | ||
key={filter} | ||
href={`#/${filter}`} | ||
className={cn('filter__link', { | ||
selected: selectedFilter === filter, | ||
})} | ||
data-cy={`FilterLink${filter.charAt(0).toUpperCase() + filter.slice(1)}`} | ||
onClick={() => setSelectedFilter(filter as FilterTypes)} | ||
> | ||
{filter.charAt(0).toUpperCase() + filter.slice(1)} | ||
</a> | ||
))} | ||
</nav> | ||
|
||
<button | ||
type="button" | ||
className="todoapp__clear-completed" | ||
data-cy="ClearCompletedButton" | ||
onClick={() => clearCompletedTodos()} | ||
disabled={!todoItems.some(todo => todo.completed)} | ||
> | ||
Clear completed | ||
</button> | ||
</footer> | ||
); | ||
}; | ||
|
||
export default 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,66 @@ | ||
import React, { useState } from 'react'; | ||
import cn from 'classnames'; | ||
import { useTodoContext } from '../Context/TodoProvider'; | ||
|
||
const Header: React.FC = () => { | ||
const { todoItems, addTodo, setTodoItems } = useTodoContext(); | ||
const [newTodoTitle, setNewTodoTitle] = useState<string>(''); | ||
|
||
const addTodoFunc = (e: React.FormEvent<HTMLFormElement>, title: string) => { | ||
e.preventDefault(); | ||
addTodo(title); | ||
setNewTodoTitle(''); | ||
}; | ||
|
||
const handleToggleAll = async () => { | ||
if ( | ||
!todoItems.some(todo => todo.completed) || | ||
!todoItems.some(todo => !todo.completed) | ||
) { | ||
setTodoItems( | ||
todoItems.map(todo => ({ | ||
...todo, | ||
completed: !todo.completed, | ||
})), | ||
); | ||
|
||
return; | ||
} else { | ||
setTodoItems( | ||
todoItems.map(todo => ({ | ||
...todo, | ||
completed: true, | ||
})), | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<header className="todoapp__header"> | ||
{todoItems.length > 0 && ( | ||
<button | ||
type="button" | ||
className={cn('todoapp__toggle-all', { | ||
active: todoItems.every(todo => todo.completed), | ||
})} | ||
data-cy="ToggleAllButton" | ||
onClick={handleToggleAll} | ||
/> | ||
)} | ||
|
||
<form onSubmit={e => addTodoFunc(e, newTodoTitle)}> | ||
<input | ||
data-cy="NewTodoField" | ||
type="text" | ||
className="todoapp__new-todo" | ||
placeholder="What needs to be done?" | ||
value={newTodoTitle} | ||
onChange={e => setNewTodoTitle(e.target.value)} | ||
autoFocus | ||
/> | ||
</form> | ||
</header> | ||
); | ||
}; | ||
|
||
export default Header; |
Oops, something went wrong.