-
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
1 parent
f1536f7
commit a05c0ba
Showing
13 changed files
with
439 additions
and
147 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,66 @@ | ||
// #region imports | ||
import cn from 'classnames'; | ||
import { useContext } from 'react'; | ||
import { FilterStatus } from '../types/FilterStatus'; | ||
import { Todo } from '../types/Todo'; | ||
import { TodosContext } from './TodosContext'; | ||
// #endregion | ||
|
||
type Props = { | ||
sortedTodos: { | ||
active: Todo[]; | ||
completed: Todo[]; | ||
}; | ||
filterStatus: FilterStatus; | ||
onStatusChange: (status: FilterStatus) => void; | ||
}; | ||
|
||
export const Footer: React.FC<Props> = ({ | ||
sortedTodos, | ||
filterStatus, | ||
onStatusChange, | ||
}) => { | ||
const { changeTodos } = useContext(TodosContext); | ||
const { active, completed } = sortedTodos; | ||
const filterLinks: { | ||
[key: string]: string; | ||
} = { | ||
All: '#/', | ||
Active: '#/active', | ||
Completed: '#/completed', | ||
}; | ||
|
||
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{`${active.length} items left`} | ||
</span> | ||
|
||
<nav className="filter" data-cy="Filter"> | ||
{Object.values(FilterStatus).map(status => ( | ||
<a | ||
key={status} | ||
href={filterLinks[status]} | ||
className={cn('filter__link', { | ||
selected: status === filterStatus, | ||
})} | ||
data-cy={`FilterLink${status}`} | ||
onClick={() => onStatusChange(status)} | ||
> | ||
{status} | ||
</a> | ||
))} | ||
</nav> | ||
|
||
<button | ||
type="button" | ||
className="todoapp__clear-completed" | ||
data-cy="ClearCompletedButton" | ||
disabled={!completed.length} | ||
onClick={() => changeTodos(active)} | ||
> | ||
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,84 @@ | ||
// #region imports | ||
import classNames from 'classnames'; | ||
import { | ||
FormEvent, | ||
memo, | ||
useContext, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from 'react'; | ||
import { TodosContext } from './TodosContext'; | ||
// #endregion | ||
|
||
export const Header = memo(function Header() { | ||
// #region hooks | ||
const { todos, changeTodos } = useContext(TodosContext); | ||
const [newTitle, setNewTitle] = useState(''); | ||
const titleInput = useRef<HTMLInputElement>(null); | ||
|
||
useEffect(() => { | ||
titleInput.current?.focus(); | ||
}); | ||
// #endregion | ||
|
||
const areTodosCompleted = todos.every(todo => todo.completed); | ||
|
||
// #region handlings | ||
const handleTodosToggle = () => { | ||
changeTodos( | ||
todos.map(todo => ({ | ||
...todo, | ||
completed: !areTodosCompleted, | ||
})), | ||
); | ||
}; | ||
|
||
const handleSubmit = (e: FormEvent) => { | ||
e.preventDefault(); | ||
|
||
const trimmedTitle = newTitle.trim(); | ||
|
||
if (!trimmedTitle) { | ||
return; | ||
} | ||
|
||
changeTodos([ | ||
...todos, | ||
{ | ||
id: +new Date(), | ||
title: trimmedTitle, | ||
completed: false, | ||
}, | ||
]); | ||
setNewTitle(''); | ||
}; | ||
// #endregion | ||
|
||
return ( | ||
<header className="todoapp__header"> | ||
{todos.length > 0 && ( | ||
<button | ||
type="button" | ||
className={classNames('todoapp__toggle-all', { | ||
active: areTodosCompleted, | ||
})} | ||
onClick={handleTodosToggle} | ||
data-cy="ToggleAllButton" | ||
/> | ||
)} | ||
|
||
<form onSubmit={handleSubmit}> | ||
<input | ||
ref={titleInput} | ||
data-cy="NewTodoField" | ||
type="text" | ||
className="todoapp__new-todo" | ||
placeholder="What needs to be done?" | ||
value={newTitle} | ||
onChange={e => setNewTitle(e.target.value)} | ||
/> | ||
</form> | ||
</header> | ||
); | ||
}); |
Oops, something went wrong.