-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Develop #1480
Open
RostyslavSharuiev
wants to merge
4
commits into
mate-academy:master
Choose a base branch
from
RostyslavSharuiev:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Develop #1480
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,110 @@ | ||
/* eslint-disable max-len */ | ||
/* eslint-disable jsx-a11y/label-has-associated-control */ | ||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||
import React from 'react'; | ||
import { UserWarning } from './UserWarning'; | ||
import { FC, useEffect, useMemo, useState } from 'react'; | ||
|
||
const USER_ID = 0; | ||
import { Todo } from './types/Todo'; | ||
import { Errors } from './types/Errors'; | ||
import { FilterBy } from './types/FilterBy'; | ||
|
||
export const App: React.FC = () => { | ||
if (!USER_ID) { | ||
return <UserWarning />; | ||
} | ||
import { getFilteredTodos } from './utils/getFilteredTodos'; | ||
import { handleFetchTodos } from './utils/handleFetchTodos'; | ||
import { handleDeleteTodos } from './utils/handleDeleteTodos'; | ||
|
||
import { Header, TodoList, Footer, ErrorMessage, TodoItem } from './components'; | ||
import { handleUpdateTodos } from './utils/handleUpdateTodos'; | ||
|
||
export const App: FC = () => { | ||
const [todos, setTodos] = useState<Todo[]>([]); | ||
const [tempTodo, setTempTodo] = useState<Todo | null>(null); | ||
const [newTodoData, setNewTodoData] = useState<Partial<Todo>>({}); | ||
|
||
const [idsForDelete, setIdsForDelete] = useState<number[]>([]); | ||
const [idsForUpdate, setIdsForUpdate] = useState<number[]>([]); | ||
|
||
const [error, setError] = useState<Errors>(Errors.DEFAULT); | ||
|
||
const [selectedFilter, setSelectedFilter] = useState<FilterBy>(FilterBy.ALL); | ||
|
||
const completedTodosId = useMemo(() => { | ||
return getFilteredTodos(todos, FilterBy.COMPLETED).map(todo => todo.id); | ||
}, [todos]); | ||
|
||
const numberOfActiveTodos = useMemo(() => { | ||
return getFilteredTodos(todos, FilterBy.ACTIVE).length; | ||
}, [todos]); | ||
|
||
const filteredTodos = useMemo(() => { | ||
return getFilteredTodos(todos, selectedFilter); | ||
}, [todos, selectedFilter]); | ||
|
||
useEffect(() => { | ||
if (idsForDelete.length) { | ||
handleDeleteTodos(idsForDelete, setTodos, setIdsForDelete, setError); | ||
} | ||
}, [idsForDelete]); | ||
|
||
useEffect(() => { | ||
if (idsForUpdate.length) { | ||
handleUpdateTodos( | ||
idsForUpdate, | ||
newTodoData, | ||
setTodos, | ||
setIdsForUpdate, | ||
setError, | ||
); | ||
} | ||
}, [idsForUpdate, newTodoData]); | ||
|
||
useEffect(() => { | ||
handleFetchTodos(setTodos, setError); | ||
}, []); | ||
|
||
return ( | ||
<section className="section container"> | ||
<p className="title is-4"> | ||
Copy all you need from the prev task: | ||
<br /> | ||
<a href="https://github.com/mate-academy/react_todo-app-add-and-delete#react-todo-app-add-and-delete"> | ||
React Todo App - Add and Delete | ||
</a> | ||
</p> | ||
|
||
<p className="subtitle">Styles are already copied</p> | ||
</section> | ||
<div className="todoapp"> | ||
<h1 className="todoapp__title">todos</h1> | ||
|
||
<div className="todoapp__content"> | ||
<Header | ||
todos={todos} | ||
tempTodo={tempTodo} | ||
numberOfActiveTodos={numberOfActiveTodos} | ||
setTodos={setTodos} | ||
setError={setError} | ||
setTempTodo={setTempTodo} | ||
setIdsForUpdate={setIdsForUpdate} | ||
setNewTodoData={setNewTodoData} | ||
/> | ||
|
||
<TodoList | ||
todos={filteredTodos} | ||
idsForDelete={idsForDelete} | ||
idsForUpdate={idsForUpdate} | ||
setIdsForDelete={setIdsForDelete} | ||
setIdsForUpdate={setIdsForUpdate} | ||
setNewTodoData={setNewTodoData} | ||
/> | ||
|
||
{tempTodo && ( | ||
<TodoItem | ||
todo={tempTodo} | ||
setIdsForDelete={setIdsForDelete} | ||
setIdsForUpdate={setIdsForUpdate} | ||
setNewTodoData={setNewTodoData} | ||
/> | ||
)} | ||
|
||
{!!todos.length && ( | ||
<Footer | ||
selectedFilter={selectedFilter} | ||
completedTodosId={completedTodosId} | ||
numberOfActiveTodos={numberOfActiveTodos} | ||
setSelectedFilter={setSelectedFilter} | ||
setIdsForDelete={setIdsForDelete} | ||
/> | ||
)} | ||
</div> | ||
|
||
<ErrorMessage error={error} setError={setError} /> | ||
</div> | ||
); | ||
}; |
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 { Todo } from '../types/Todo'; | ||
|
||
import { client } from '../utils/fetchClient'; | ||
|
||
import { USER_ID } from '../constants/constants'; | ||
|
||
export const getTodos = (): Promise<Todo[]> => { | ||
return client.get<Todo[]>(`/todos?userId=${USER_ID}`); | ||
}; | ||
|
||
export const addTodo = (newTodo: Omit<Todo, 'id'>): Promise<Todo> => { | ||
return client.post<Todo>('/todos', newTodo); | ||
}; | ||
|
||
export const deleteTodo = (id: number): Promise<unknown> => { | ||
return client.delete(`/todos/${id}`); | ||
}; | ||
|
||
export const updateTodo = ( | ||
id: number, | ||
updates: Partial<Todo>, | ||
): Promise<Todo> => { | ||
return client.patch<Todo>(`/todos/${id}`, updates); | ||
}; |
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,33 @@ | ||
import { FC } from 'react'; | ||
import cn from 'classnames'; | ||
|
||
import { Errors } from '../../types/Errors'; | ||
import { handleError } from '../../utils/handleError'; | ||
|
||
interface Props { | ||
error: Errors; | ||
setError: (error: Errors) => void; | ||
} | ||
|
||
export const ErrorMessage: FC<Props> = ({ error, setError }) => { | ||
const handleClose = () => { | ||
handleError(Errors.DEFAULT, setError); | ||
}; | ||
|
||
return ( | ||
<div | ||
data-cy="ErrorNotification" | ||
className={cn('notification is-danger is-light has-text-weight-normal', { | ||
hidden: !error, | ||
})} | ||
> | ||
<button | ||
data-cy="HideErrorButton" | ||
type="button" | ||
className="delete" | ||
onClick={handleClose} | ||
/> | ||
{error} | ||
</div> | ||
); | ||
}; |
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 './ErrorMessage'; |
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,58 @@ | ||
import { FC } from 'react'; | ||
import cn from 'classnames'; | ||
|
||
import { FilterBy } from '../../types/FilterBy'; | ||
|
||
interface Props { | ||
selectedFilter: FilterBy; | ||
completedTodosId: number[]; | ||
numberOfActiveTodos: number; | ||
setSelectedFilter: (filter: FilterBy) => void; | ||
setIdsForDelete: (ids: number[]) => void; | ||
} | ||
|
||
const Footer: FC<Props> = ({ | ||
selectedFilter, | ||
completedTodosId, | ||
numberOfActiveTodos, | ||
setSelectedFilter, | ||
setIdsForDelete, | ||
}) => { | ||
const filters = Object.values(FilterBy); | ||
|
||
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{numberOfActiveTodos} items left | ||
</span> | ||
|
||
<nav className="filter" data-cy="Filter"> | ||
{filters.map(filter => ( | ||
<a | ||
key={filter} | ||
href="#/" | ||
className={cn('filter__link', { | ||
selected: filter === selectedFilter, | ||
})} | ||
data-cy={`FilterLink${filter}`} | ||
onClick={() => setSelectedFilter(filter)} | ||
> | ||
{filter} | ||
</a> | ||
))} | ||
</nav> | ||
|
||
<button | ||
type="button" | ||
className="todoapp__clear-completed" | ||
data-cy="ClearCompletedButton" | ||
onClick={() => setIdsForDelete(completedTodosId)} | ||
disabled={!completedTodosId.length} | ||
> | ||
Clear completed | ||
</button> | ||
</footer> | ||
); | ||
}; | ||
|
||
export default 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 { default as Footer } 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,116 @@ | ||
import { ChangeEvent, FC, FormEvent, useEffect, useRef, useState } from 'react'; | ||
import cn from 'classnames'; | ||
|
||
import { Todo } from '../../types/Todo'; | ||
import { Errors } from '../../types/Errors'; | ||
|
||
import { addTodo } from '../../api/todos'; | ||
import { handleError } from '../../utils/handleError'; | ||
|
||
import { USER_ID } from '../../constants/constants'; | ||
|
||
interface Props { | ||
todos: Todo[]; | ||
tempTodo: Todo | null; | ||
numberOfActiveTodos: number; | ||
setTodos: (updateTodos: (todos: Todo[]) => Todo[]) => void; | ||
setError: (error: Errors) => void; | ||
setTempTodo: (todo: Todo | null) => void; | ||
setIdsForUpdate: (prevIds: (ids: number[]) => number[]) => void; | ||
setNewTodoData: (newData: Partial<Todo>) => void; | ||
} | ||
|
||
const Header: FC<Props> = ({ | ||
todos, | ||
tempTodo, | ||
numberOfActiveTodos, | ||
setTodos, | ||
setError, | ||
setTempTodo, | ||
setIdsForUpdate, | ||
setNewTodoData, | ||
}) => { | ||
const [title, setTitle] = useState(''); | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
const handleChangeTitle = (event: ChangeEvent<HTMLInputElement>) => { | ||
setTitle(event.target.value.trimStart()); | ||
}; | ||
|
||
const handleFormSubmit = async (event: FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
const formattedTitle = title.trim(); | ||
|
||
if (!formattedTitle) { | ||
handleError(Errors.TITLE_ERROR, setError); | ||
|
||
return; | ||
} | ||
|
||
const newTodo: Omit<Todo, 'id'> = { | ||
userId: USER_ID, | ||
title: formattedTitle, | ||
completed: false, | ||
}; | ||
|
||
const tmpTodo: Todo = { | ||
id: 0, | ||
...newTodo, | ||
}; | ||
|
||
setTempTodo(tmpTodo); | ||
|
||
try { | ||
const todo = await addTodo(newTodo); | ||
|
||
setTodos(currentTodos => [...currentTodos, todo]); | ||
setTitle(''); | ||
setTempTodo(null); | ||
} catch { | ||
setTempTodo(null); | ||
handleError(Errors.ADD_TODO, setError); | ||
} | ||
}; | ||
|
||
const handleToggleAll = () => { | ||
const isAllTodosCompleted = todos.every(todo => todo.completed); | ||
|
||
todos.map(todo => { | ||
setIdsForUpdate(currentIds => [...currentIds, todo.id]); | ||
setNewTodoData({ completed: isAllTodosCompleted ? false : true }); | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
inputRef.current?.focus(); | ||
}, [todos, tempTodo]); | ||
|
||
return ( | ||
<header className="todoapp__header"> | ||
<button | ||
type="button" | ||
className={cn('todoapp__toggle-all', { | ||
active: !numberOfActiveTodos, | ||
})} | ||
data-cy="ToggleAllButton" | ||
onClick={handleToggleAll} | ||
/> | ||
|
||
<form onSubmit={handleFormSubmit}> | ||
<input | ||
data-cy="NewTodoField" | ||
type="text" | ||
className="todoapp__new-todo" | ||
placeholder="What needs to be done?" | ||
ref={inputRef} | ||
value={title} | ||
onChange={handleChangeTitle} | ||
disabled={!!tempTodo} | ||
/> | ||
</form> | ||
</header> | ||
); | ||
}; | ||
|
||
export default 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 { default as Header } from './Header'; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it can be put inside TodoList component
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its makes according to part 2 of the todo app