-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Showing
13 changed files
with
386 additions
and
151 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,87 @@ | ||
import { useContext } from 'react'; | ||
import { TodoContext } from './TodoContext'; | ||
import { TodoFilter } from '../types/TodoFilter'; | ||
import classNames from 'classnames'; | ||
|
||
export const Footer = () => { | ||
const { todos, filterBy, setFilterBy, setTodos } = useContext(TodoContext); | ||
|
||
const activeTodosLength = todos.filter(t => !t.completed).length; | ||
const isDisabledBtn = !(todos.length - activeTodosLength); | ||
const title = | ||
activeTodosLength === 1 ? '1 item left' : `${activeTodosLength} items left`; | ||
|
||
const filterTodos = (status: TodoFilter) => { | ||
switch (status) { | ||
case 'Active': | ||
setFilterBy('Active'); | ||
break; | ||
case 'Completed': | ||
setFilterBy('Completed'); | ||
break; | ||
default: | ||
setFilterBy('All'); | ||
} | ||
}; | ||
|
||
const deleteCompleted = () => { | ||
setTodos(todos.filter(t => !t.completed)); | ||
}; | ||
|
||
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{title} | ||
</span> | ||
|
||
{/* Active link should have the 'selected' class */} | ||
<nav className="filter" data-cy="Filter"> | ||
<a | ||
href="#/" | ||
className={classNames('filter__link', { | ||
selected: filterBy === 'All', | ||
})} | ||
data-cy="FilterLinkAll" | ||
onClick={() => filterTodos('All')} | ||
> | ||
All | ||
</a> | ||
|
||
<a | ||
href="#/active" | ||
className={classNames('filter__link', { | ||
selected: filterBy === 'Active', | ||
})} | ||
data-cy="FilterLinkActive" | ||
onClick={() => filterTodos('Active')} | ||
> | ||
Active | ||
</a> | ||
|
||
<a | ||
href="#/completed" | ||
className={classNames('filter__link', { | ||
selected: filterBy === 'Completed', | ||
})} | ||
data-cy="FilterLinkCompleted" | ||
onClick={() => filterTodos('Completed')} | ||
> | ||
Completed | ||
</a> | ||
</nav> | ||
|
||
{/* this button should be disabled if there are no completed todos */} | ||
|
||
<button | ||
type="button" | ||
className="todoapp__clear-completed" | ||
style={{ visibility: `${isDisabledBtn ? 'hidden' : 'visible'}` }} | ||
data-cy="ClearCompletedButton" | ||
onClick={deleteCompleted} | ||
disabled={isDisabledBtn} | ||
> | ||
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,61 @@ | ||
import React, { useContext, useEffect, useRef, useState } from 'react'; | ||
import { TodoContext } from './TodoContext'; | ||
import classNames from 'classnames'; | ||
|
||
export const Header = () => { | ||
const { todos, setTodos } = useContext(TodoContext); | ||
const [value, setValue] = useState(''); | ||
const mainInput = useRef<HTMLInputElement>(null); | ||
const isSomeActive = todos.some(t => !t.completed) || !todos.length; | ||
|
||
useEffect(() => { | ||
if (mainInput.current) { | ||
mainInput.current.focus(); | ||
} | ||
}, [todos]); | ||
|
||
const submitHandler: React.FormEventHandler<HTMLFormElement> = e => { | ||
e.preventDefault(); | ||
|
||
setTodos([ | ||
...todos, | ||
{ id: Date.now(), title: value.trim(), completed: false }, | ||
]); | ||
setValue(''); | ||
}; | ||
|
||
const toggleAll = () => { | ||
if (isSomeActive) { | ||
setTodos(todos.map(t => ({ ...t, completed: true }))); | ||
} else { | ||
setTodos(todos.map(t => ({ ...t, completed: false }))); | ||
} | ||
}; | ||
|
||
return ( | ||
<header className="todoapp__header"> | ||
{!!todos.length && ( | ||
<button | ||
type="button" | ||
className={classNames('todoapp__toggle-all', { | ||
active: !isSomeActive, | ||
})} | ||
data-cy="ToggleAllButton" | ||
onClick={toggleAll} | ||
/> | ||
)} | ||
|
||
<form onSubmit={submitHandler}> | ||
<input | ||
data-cy="NewTodoField" | ||
type="text" | ||
className="todoapp__new-todo" | ||
placeholder="What needs to be done?" | ||
value={value} | ||
ref={mainInput} | ||
onChange={e => setValue(e.target.value)} | ||
/> | ||
</form> | ||
</header> | ||
); | ||
}; |
Oops, something went wrong.