-
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
Showing
12 changed files
with
454 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
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,51 @@ | ||
import { useContext } from 'react'; | ||
import cn from 'classnames'; | ||
import { DispatchContext, StateContext } from './Store'; | ||
import { Filter } from '../types/Filter'; | ||
import { activeTodos, completedTodos } from '../services'; | ||
|
||
export const Footer = () => { | ||
const { todos, filter } = useContext(StateContext); | ||
const dispatch = useContext(DispatchContext); | ||
|
||
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{activeTodos(todos).length} items left | ||
</span> | ||
|
||
<nav className="filter" data-cy="Filter"> | ||
{Object.values(Filter).map(status => ( | ||
<a | ||
key={status} | ||
href={`#/${status}`} | ||
className={cn('filter__link', { | ||
selected: filter === status, | ||
})} | ||
data-cy={`FilterLink${status}`} | ||
onClick={() => | ||
dispatch({ | ||
type: 'setFilterByStatus', | ||
payload: status, | ||
}) | ||
} | ||
> | ||
{status} | ||
</a> | ||
))} | ||
</nav> | ||
|
||
<button | ||
type="button" | ||
className="todoapp__clear-completed" | ||
data-cy="ClearCompletedButton" | ||
disabled={completedTodos(todos).length === 0} | ||
onClick={() => { | ||
dispatch({ type: 'clearAllCompleted' }); | ||
}} | ||
> | ||
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,72 @@ | ||
import React, { FormEvent, useContext, useEffect, useRef } from 'react'; | ||
import { DispatchContext, StateContext } from './Store'; | ||
import { Todo } from '../types/Todo'; | ||
import classNames from 'classnames'; | ||
import { completedTodos } from '../services'; | ||
|
||
export const Header = () => { | ||
const { todos, newTitle } = useContext(StateContext); | ||
const dispatch = useContext(DispatchContext); | ||
|
||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
useEffect(() => { | ||
if (inputRef.current) { | ||
inputRef.current.focus(); | ||
} | ||
}); | ||
|
||
const setNewTitle = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
dispatch({ type: 'setNewTitle', payload: e.target.value }); | ||
}; | ||
|
||
const addTodo = (e: FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
if (!newTitle.trim()) { | ||
return; | ||
} | ||
|
||
const newTodo: Todo = { | ||
id: +new Date(), | ||
title: newTitle.trim(), | ||
completed: false, | ||
}; | ||
|
||
dispatch({ type: 'addTodo', payload: newTodo }); | ||
dispatch({ type: 'setNewTitle', payload: '' }); | ||
}; | ||
|
||
const validation = todos.every(todo => todo.completed === true); | ||
|
||
const toggleAll = () => { | ||
dispatch({ type: 'toggleAll', payload: !validation }); | ||
}; | ||
|
||
return ( | ||
<header className="todoapp__header"> | ||
{todos.length > 0 && ( | ||
<button | ||
type="button" | ||
className={classNames('todoapp__toggle-all', { | ||
active: todos.length === completedTodos(todos).length, | ||
})} | ||
data-cy="ToggleAllButton" | ||
onClick={toggleAll} | ||
/> | ||
)} | ||
|
||
<form onSubmit={addTodo}> | ||
<input | ||
ref={inputRef} | ||
data-cy="NewTodoField" | ||
type="text" | ||
className="todoapp__new-todo" | ||
placeholder="What needs to be done?" | ||
value={newTitle} | ||
onChange={setNewTitle} | ||
/> | ||
</form> | ||
</header> | ||
); | ||
}; |
Oops, something went wrong.