-
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
14 changed files
with
475 additions
and
150 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,52 @@ | ||
import { useContext } from 'react'; | ||
import cn from 'classnames'; | ||
import { DispatchContext, StateContext } from './Store'; | ||
import { getActiveTodosArray, getCompletedTodosArray } from '../services'; | ||
import { Filter } from '../types/Filter'; | ||
import React from 'react'; | ||
|
||
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"> | ||
{getActiveTodosArray(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={getCompletedTodosArray(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,75 @@ | ||
import { FormEvent, useContext, useEffect, useRef } from 'react'; | ||
import { DispatchContext, StateContext } from './Store'; | ||
import { Todo } from '../types/Todo'; | ||
import classNames from 'classnames'; | ||
import { getCompletedTodosArray } from '../services'; | ||
import React from 'react'; | ||
|
||
export const Header = () => { | ||
const { todos, newTodoTitle } = useContext(StateContext); | ||
const dispatch = useContext(DispatchContext); | ||
|
||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
useEffect(() => { | ||
if (inputRef.current) { | ||
inputRef.current.focus(); | ||
} | ||
}); | ||
|
||
const handleSetNewTodoTitle = ( | ||
event: React.ChangeEvent<HTMLInputElement>, | ||
) => { | ||
dispatch({ type: 'setNewTodoTitle', payload: event.target.value }); | ||
}; | ||
|
||
const handleAddTodo = (event: FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
if (!newTodoTitle.trim()) { | ||
return; | ||
} | ||
|
||
const newTodo: Todo = { | ||
id: +new Date(), | ||
title: newTodoTitle.trim(), | ||
completed: false, | ||
}; | ||
|
||
dispatch({ type: 'addTodo', payload: newTodo }); | ||
dispatch({ type: 'setNewTodoTitle', payload: '' }); | ||
}; | ||
|
||
const validation = todos.every(todo => todo.completed === true); | ||
|
||
const handleToggleAll = () => { | ||
dispatch({ type: 'setAllCompleted', payload: !validation }); | ||
}; | ||
|
||
return ( | ||
<header className="todoapp__header"> | ||
{!!todos.length && ( | ||
<button | ||
type="button" | ||
className={classNames('todoapp__toggle-all', { | ||
active: todos.length === getCompletedTodosArray(todos).length, | ||
})} | ||
data-cy="ToggleAllButton" | ||
onClick={handleToggleAll} | ||
/> | ||
)} | ||
|
||
<form onSubmit={handleAddTodo}> | ||
<input | ||
ref={inputRef} | ||
data-cy="NewTodoField" | ||
type="text" | ||
className="todoapp__new-todo" | ||
placeholder="What needs to be done?" | ||
value={newTodoTitle} | ||
onChange={handleSetNewTodoTitle} | ||
/> | ||
</form> | ||
</header> | ||
); | ||
}; |
Oops, something went wrong.