-
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
17 changed files
with
491 additions
and
149 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,20 @@ | ||
import { Todo } from '../types/Todo'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const USER_ID = 968; | ||
|
||
export const getTodos = () => { | ||
return client.get<Todo[]>(`/todos?userId=${USER_ID}`); | ||
}; | ||
|
||
export const postTodo = (todo: Omit<Todo, 'id'>) => { | ||
return client.post<Todo>(`/todos`, todo); | ||
}; | ||
|
||
export const deleteTodo = (todoId: number) => { | ||
return client.delete<Todo>(`/todos/${todoId}`); | ||
}; | ||
|
||
export const patchTodo = (id: number, todoData: Partial<Todo>) => { | ||
return client.patch<Todo>(`/todos/${id}`, todoData); | ||
}; |
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 from 'react'; | ||
import { FilterStatus } from '../../types/FilterStatus'; | ||
import cn from 'classnames'; | ||
import { useDispatch, useGlobalState } from '../../context/Store'; | ||
|
||
type Props = { | ||
filter: FilterStatus; | ||
onFilter: (filter: FilterStatus) => void; | ||
}; | ||
|
||
export const Footer: React.FC<Props> = ({ filter, onFilter }) => { | ||
const todos = useGlobalState(); | ||
const dispatch = useDispatch(); | ||
|
||
const activeTodos = todos.filter(todo => !todo.completed).length; | ||
|
||
const haveCompletedTodos = todos.some(todo => todo.completed); | ||
|
||
const handleClearCompleted = () => | ||
todos | ||
.filter(todo => todo.completed) | ||
.forEach(todo => dispatch({ type: 'delete', payload: todo.id })); | ||
|
||
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{`${activeTodos} items left`} | ||
</span> | ||
|
||
<nav className="filter" data-cy="Filter"> | ||
{Object.values(FilterStatus).map(filterStatus => ( | ||
<a | ||
key={filterStatus} | ||
href={ | ||
filterStatus === FilterStatus.All | ||
? '#/' | ||
: `#/${filterStatus.toLowerCase()}` | ||
} | ||
className={cn('filter__link', { | ||
selected: filter === filterStatus, | ||
})} | ||
data-cy={`FilterLink${filterStatus}`} | ||
onClick={() => onFilter(filterStatus)} | ||
> | ||
{filterStatus} | ||
</a> | ||
))} | ||
</nav> | ||
|
||
<button | ||
type="button" | ||
className="todoapp__clear-completed" | ||
data-cy="ClearCompletedButton" | ||
disabled={!haveCompletedTodos} | ||
onClick={handleClearCompleted} | ||
> | ||
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 @@ | ||
export * from './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,77 @@ | ||
import React, { FC, useEffect, useRef, useState } from 'react'; | ||
import cn from 'classnames'; | ||
|
||
import { useDispatch, useGlobalState } from '../../context/Store'; | ||
|
||
export const Header: FC = () => { | ||
const titleField = useRef<HTMLInputElement>(null); | ||
const [title, setTitle] = useState(''); | ||
const [isSubmitting, setIsSubmiting] = useState(false); | ||
|
||
const todos = useGlobalState(); | ||
const dispatch = useDispatch(); | ||
|
||
const areAllTodosCompleted = todos.every(todo => todo.completed); | ||
|
||
const handleToggleAll = () => { | ||
const haveActive = todos.some(todo => !todo.completed); | ||
const todosToUpdate = haveActive | ||
? todos.filter(todo => !todo.completed) | ||
: todos; | ||
|
||
todosToUpdate.forEach(todo => | ||
dispatch({ type: 'update', payload: { ...todo, completed: haveActive } }), | ||
); | ||
}; | ||
|
||
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
setIsSubmiting(true); | ||
|
||
const newTitle = title.trim(); | ||
|
||
if (newTitle) { | ||
try { | ||
dispatch({ type: 'add', payload: newTitle }); | ||
setTitle(''); | ||
} catch (error) { | ||
throw error; | ||
} finally { | ||
setIsSubmiting(false); | ||
} | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
titleField.current?.focus(); | ||
}, [todos, isSubmitting]); | ||
|
||
return ( | ||
<header className="todoapp__header"> | ||
{!!todos.length && ( | ||
<button | ||
type="button" | ||
className={cn('todoapp__toggle-all', { | ||
active: areAllTodosCompleted, | ||
})} | ||
data-cy="ToggleAllButton" | ||
onClick={handleToggleAll} | ||
/> | ||
)} | ||
|
||
<form onSubmit={handleSubmit}> | ||
<input | ||
ref={titleField} | ||
data-cy="NewTodoField" | ||
type="text" | ||
className="todoapp__new-todo" | ||
placeholder="What needs to be done?" | ||
value={title} | ||
onChange={event => setTitle(event.target.value)} | ||
disabled={isSubmitting} | ||
/> | ||
</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 @@ | ||
export * from './Header'; |
Oops, something went wrong.