-
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
11 changed files
with
433 additions
and
137 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,81 @@ | ||
import { useContext } from 'react'; | ||
import { TodosContext } from './TodosContext'; | ||
import { FilterMethods } from '../types/FilterMethods'; | ||
import classNames from 'classnames'; | ||
|
||
type Props = { | ||
onFilter: (v: FilterMethods) => void; | ||
filterList: FilterMethods | null; | ||
}; | ||
|
||
export const Footer: React.FC<Props> = ({ onFilter, filterList }) => { | ||
const context = useContext(TodosContext); | ||
|
||
if (!context) { | ||
throw new Error('useTodos must be used within a TodosProvider'); | ||
} | ||
|
||
const { todos, setTodos, inputFocus } = context; | ||
|
||
function clearCompleted() { | ||
const clearedTodos = todos.filter(todo => !todo.completed); | ||
|
||
setTodos(clearedTodos); | ||
inputFocus.current?.focus(); | ||
} | ||
|
||
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{`${todos.filter(el => !el.completed).length} items left`} | ||
</span> | ||
|
||
{/* Active link should have the 'selected' class */} | ||
<nav className="filter" data-cy="Filter"> | ||
<a | ||
onClick={() => { | ||
onFilter(FilterMethods.all); | ||
}} | ||
className={classNames('filter__link', { | ||
selected: filterList === FilterMethods.all, | ||
})} | ||
data-cy="FilterLinkAll" | ||
> | ||
All | ||
</a> | ||
|
||
<a | ||
onClick={() => onFilter(FilterMethods.active)} | ||
className={classNames('filter__link', { | ||
selected: filterList === FilterMethods.active, | ||
})} | ||
data-cy="FilterLinkActive" | ||
> | ||
Active | ||
</a> | ||
|
||
<a | ||
onClick={() => onFilter(FilterMethods.completed)} | ||
className={classNames('filter__link', { | ||
selected: filterList === FilterMethods.completed, | ||
})} | ||
data-cy="FilterLinkCompleted" | ||
> | ||
Completed | ||
</a> | ||
</nav> | ||
|
||
{/* this button should be disabled if there are no completed todos */} | ||
|
||
<button | ||
disabled={todos.every(el => !el.completed)} | ||
onClick={() => clearCompleted()} | ||
type="button" | ||
className="todoapp__clear-completed" | ||
data-cy="ClearCompletedButton" | ||
> | ||
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,43 @@ | ||
import { useContext, useState } from 'react'; | ||
import { TodosContext } from './TodosContext'; | ||
|
||
export const Form = () => { | ||
const [title, setTitle] = useState(''); | ||
const context = useContext(TodosContext); | ||
|
||
if (!context) { | ||
throw new Error('useTodos must be used within a TodosProvider'); | ||
} | ||
|
||
const { addTodo, inputFocus } = context; | ||
|
||
return ( | ||
<form | ||
onSubmit={e => { | ||
e.preventDefault(); | ||
if (!title.trim()) { | ||
return; | ||
} | ||
|
||
addTodo({ | ||
id: +new Date(), | ||
title: title.trim(), | ||
completed: false, | ||
}); | ||
|
||
setTitle(''); | ||
}} | ||
> | ||
<input | ||
autoFocus | ||
ref={inputFocus} | ||
data-cy="NewTodoField" | ||
type="text" | ||
className="todoapp__new-todo" | ||
placeholder="What needs to be done?" | ||
value={title} | ||
onChange={e => setTitle(e.target.value)} | ||
/> | ||
</form> | ||
); | ||
}; |
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,42 @@ | ||
import classNames from 'classnames'; | ||
import { useContext } from 'react'; | ||
import { TodosContext } from './TodosContext'; | ||
import { Form } from './Form'; | ||
|
||
export const Header: React.FC = () => { | ||
const context = useContext(TodosContext); | ||
|
||
if (!context) { | ||
throw new Error('useTodos must be used within a TodosProvider'); | ||
} | ||
|
||
const { todos, updateTodo } = context; | ||
|
||
const toggleAll = () => { | ||
const allCompleted = todos.every(todo => todo.completed); | ||
const newStatus = !allCompleted; | ||
|
||
todos.forEach(todo => { | ||
updateTodo(todo.id, { completed: newStatus }); | ||
}); | ||
}; | ||
|
||
return ( | ||
<header className="todoapp__header"> | ||
{/* this button should have `active` class only if all todos are completed */} | ||
{todos.length !== 0 && ( | ||
<button | ||
onClick={toggleAll} | ||
type="button" | ||
className={classNames('todoapp__toggle-all', { | ||
active: todos.every(el => el.completed), | ||
})} | ||
data-cy="ToggleAllButton" | ||
/> | ||
)} | ||
|
||
{/* Add a todo on form submit */} | ||
<Form /> | ||
</header> | ||
); | ||
}; |
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,24 @@ | ||
import { useState } from 'react'; | ||
import { Todo } from '../types/Todo'; | ||
import { TodoItem } from './TodoItem'; | ||
|
||
type Props = { | ||
visibleTodos: Todo[]; | ||
}; | ||
|
||
export const Main: React.FC<Props> = ({ visibleTodos }) => { | ||
const [editingTodo, setEditingTodo] = useState<Date | number | null>(null); | ||
|
||
return ( | ||
<section className="todoapp__main" data-cy="TodoList"> | ||
{visibleTodos?.map(todo => ( | ||
<TodoItem | ||
key={`${todo.id}-${todo.completed}`} | ||
todo={todo} | ||
editing={editingTodo} | ||
onEdit={setEditingTodo} | ||
/> | ||
))} | ||
</section> | ||
); | ||
}; |
Oops, something went wrong.