-
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
solution #1565
Open
Fridrif27
wants to merge
1
commit into
mate-academy:master
Choose a base branch
from
Fridrif27: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
solution #1565
Changes from all 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,146 @@ | ||
/* 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 React, { useEffect, useMemo, useState } from 'react'; | ||
import { UserWarning } from './UserWarning'; | ||
import { USER_ID } from './api/todos'; | ||
import * as servisesTodos from './api/todos'; | ||
import { Todo } from './types/Todo'; | ||
import { TodoInput } from './components/TodoInput'; | ||
import { TodoList } from './components/TodoList'; | ||
import { ErrorNotification } from './components/Errors'; | ||
import { Footer } from './components/Footer'; | ||
import { Filter } from './types/Filter'; | ||
|
||
const USER_ID = 0; | ||
export enum FilterParam { | ||
All = 'All', | ||
Active = 'Active', | ||
Completed = 'Completed', | ||
} | ||
|
||
export const App: React.FC = () => { | ||
const [todos, setTodos] = useState<Todo[]>([]); | ||
const [errorMessage, setErrorMessage] = useState(''); | ||
const [filter, setFilter] = useState<Filter>(FilterParam.All); | ||
const [tempTodo, setTempTodo] = useState<Todo | null>(null); | ||
const [loadingIds, setLoadingIds] = useState<number[]>([]); | ||
|
||
useEffect(() => { | ||
servisesTodos | ||
.getTodos() | ||
.then(setTodos) | ||
.catch(() => { | ||
setErrorMessage('Unable to load todos'); | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
setTimeout(() => { | ||
setErrorMessage(''); | ||
}, 3000); | ||
}, [errorMessage]); | ||
|
||
const filteredTodos = useMemo(() => { | ||
if (filter === FilterParam.Active) { | ||
return todos.filter(todo => !todo.completed); | ||
} | ||
|
||
if (filter === FilterParam.Completed) { | ||
return todos.filter(todo => todo.completed); | ||
} | ||
|
||
return todos; | ||
}, [filter, todos]); | ||
|
||
const handleDeleteTodo = (todoId: number) => { | ||
setLoadingIds(current => [...current, todoId]); | ||
|
||
return servisesTodos | ||
.deleteTodo(todoId) | ||
.then(() => { | ||
setTodos(currentTodo => currentTodo.filter(todo => todo.id !== todoId)); | ||
}) | ||
.catch(error => { | ||
setErrorMessage('Unable to delete a todo'); | ||
throw error; | ||
}) | ||
.finally(() => { | ||
setLoadingIds(current => current.filter(id => id !== todoId)); | ||
}); | ||
}; | ||
|
||
const handleUpdateTodo = async ( | ||
todoId: number, | ||
newTitle: string, | ||
completed: boolean, | ||
): Promise<void> => { | ||
setLoadingIds(current => [...current, todoId]); | ||
const todoToUpdate = todos.find(todo => todo.id === todoId); | ||
|
||
if (!todoToUpdate) { | ||
setLoadingIds(current => current.filter(id => id !== todoId)); | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
const updatedTodo = { | ||
...todoToUpdate, | ||
title: newTitle.trim(), | ||
completed, | ||
}; | ||
|
||
try { | ||
await servisesTodos.updateTodo(todoId, updatedTodo); | ||
setTodos(currentTodo => | ||
currentTodo.map(todo => (todo.id === todoId ? updatedTodo : todo)), | ||
); | ||
} catch (error) { | ||
setErrorMessage('Unable to update a todo'); | ||
throw error; | ||
} finally { | ||
setLoadingIds(current => current.filter(id => id !== todoId)); | ||
} | ||
}; | ||
|
||
if (!USER_ID) { | ||
return <UserWarning />; | ||
} | ||
|
||
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"> | ||
<TodoInput | ||
setErrorMessage={setErrorMessage} | ||
todos={todos} | ||
setTodos={setTodos} | ||
setTempTodo={setTempTodo} | ||
tempTodo={tempTodo} | ||
handleUpdateTodo={handleUpdateTodo} | ||
loadingIds={loadingIds} | ||
/> | ||
|
||
<TodoList | ||
filteredTodos={filteredTodos} | ||
tempTodo={tempTodo} | ||
handleDeleteTodo={handleDeleteTodo} | ||
handleUpdateTodo={handleUpdateTodo} | ||
loadingIds={loadingIds} | ||
/> | ||
{todos.length > 0 && ( | ||
<Footer | ||
filter={filter} | ||
setFilter={setFilter} | ||
todos={todos} | ||
handleDeleteTodo={handleDeleteTodo} | ||
/> | ||
)} | ||
</div> | ||
<ErrorNotification | ||
errorMessage={errorMessage} | ||
setErrorMessage={setErrorMessage} | ||
/> | ||
</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,21 @@ | ||
import { Todo } from '../types/Todo'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const USER_ID = 1913; | ||
|
||
export const getTodos = () => { | ||
return client.get<Todo[]>(`/todos?userId=${USER_ID}`); | ||
}; | ||
|
||
export const postTodos = ({ title, userId, completed }: Omit<Todo, 'id'>) => { | ||
return client.post<Todo>(`/todos/`, { title, userId, completed }); | ||
}; | ||
|
||
export const deleteTodo = (todoId: number) => { | ||
return client.delete(`/todos/${todoId}`); | ||
}; | ||
|
||
export const updateTodo = (todoId: number, todo: Todo) => { | ||
return client.patch(`/todos/${todoId}`, todo); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's move |
||
}; | ||
// Add more methods here | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove extra comments |
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,35 @@ | ||
import classNames from 'classnames'; | ||
import React from 'react'; | ||
|
||
type Props = { | ||
errorMessage: string; | ||
setErrorMessage: (a: string) => void; | ||
}; | ||
|
||
export const ErrorNotification: React.FC<Props> = ({ | ||
errorMessage, | ||
setErrorMessage, | ||
}) => { | ||
const handleCloseNotification = () => { | ||
setErrorMessage(''); | ||
}; | ||
|
||
return ( | ||
<div | ||
data-cy="ErrorNotification" | ||
className={classNames( | ||
'notification is-danger is-light has-text-weight-normal', | ||
// eslint-disable-next-line prettier/prettier | ||
{ hidden: !errorMessage }, | ||
)} | ||
> | ||
<button | ||
onClick={handleCloseNotification} | ||
data-cy="HideErrorButton" | ||
type="button" | ||
className="delete" | ||
/> | ||
{errorMessage} | ||
</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,69 @@ | ||
import classNames from 'classnames'; | ||
import { Todo } from '../types/Todo'; | ||
import { Filter } from '../types/Filter'; | ||
import { FilterParam } from '../App'; | ||
import React from 'react'; | ||
|
||
type Props = { | ||
filter: string; | ||
setFilter: (a: Filter) => void; | ||
todos: Todo[]; | ||
handleDeleteTodo: (a: number) => void; | ||
}; | ||
|
||
export const Footer: React.FC<Props> = ({ | ||
filter, | ||
setFilter, | ||
todos, | ||
handleDeleteTodo, | ||
}) => { | ||
const handleFilter = (event: React.MouseEvent<HTMLElement>) => { | ||
const filterValue = event.currentTarget.textContent as Filter; | ||
|
||
setFilter(filterValue); | ||
}; | ||
|
||
const handleClearComplete = () => { | ||
const completedTodos = todos.filter(todo => todo.completed); | ||
const deletePromises = completedTodos.map(todo => | ||
handleDeleteTodo(todo.id), | ||
); | ||
|
||
Promise.allSettled(deletePromises); | ||
}; | ||
|
||
const amountActiveTodos = todos.filter(todo => !todo.completed).length; | ||
|
||
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{amountActiveTodos} items left | ||
</span> | ||
|
||
<nav className="filter" data-cy="Filter"> | ||
{Object.values(FilterParam).map(param => ( | ||
<a | ||
key={param} | ||
href="#/" | ||
className={classNames('filter__link', { | ||
selected: filter === param, | ||
})} | ||
data-cy={`FilterLink${param}`} | ||
onClick={handleFilter} | ||
> | ||
{param} | ||
</a> | ||
))} | ||
</nav> | ||
<button | ||
type="button" | ||
className="todoapp__clear-completed" | ||
data-cy="ClearCompletedButton" | ||
disabled={todos.every(todo => !todo.completed)} | ||
onClick={handleClearComplete} | ||
> | ||
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,103 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import * as servisesTodos from '../api/todos'; | ||
import { Todo } from '../types/Todo'; | ||
import classNames from 'classnames'; | ||
import React from 'react'; | ||
|
||
const userId = servisesTodos.USER_ID; | ||
|
||
type Props = { | ||
setErrorMessage: (a: string) => void; | ||
todos: Todo[]; | ||
setTodos: (a: Todo[]) => void; | ||
setTempTodo: (a: Todo | null) => void; | ||
tempTodo: Todo | null; | ||
handleUpdateTodo: (a: number, b: string, c: boolean) => void; | ||
loadingIds: number[]; | ||
}; | ||
|
||
export const TodoInput: React.FC<Props> = ({ | ||
setErrorMessage, | ||
todos, | ||
setTodos, | ||
setTempTodo, | ||
tempTodo, | ||
handleUpdateTodo, | ||
loadingIds, | ||
}) => { | ||
const [title, setTitle] = useState(''); | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
const AllCompletedTodo = todos.every(todo => todo.completed); | ||
|
||
useEffect(() => { | ||
if (inputRef.current) { | ||
inputRef.current.focus(); | ||
} | ||
}, [todos, tempTodo]); | ||
|
||
const handleInputValue = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setTitle(event.target.value); | ||
}; | ||
|
||
const handleAddTodo = (event: React.KeyboardEvent<HTMLFormElement>) => { | ||
if (event.key === 'Enter') { | ||
event.preventDefault(); | ||
setTempTodo({ id: 0, title: title.trim(), userId, completed: false }); | ||
if (title.trim().length > 0) { | ||
servisesTodos | ||
.postTodos({ title: title.trim(), userId, completed: false }) | ||
.then(newTodo => { | ||
setTodos([...todos, newTodo]); | ||
setTitle(''); | ||
setTempTodo(null); | ||
}) | ||
.catch(() => { | ||
setTempTodo(null); | ||
setErrorMessage('Unable to add a todo'); | ||
}); | ||
} else { | ||
setTempTodo(null); | ||
setErrorMessage('Title should not be empty'); | ||
} | ||
} | ||
}; | ||
|
||
const handleChangleAllStatus = () => { | ||
const newCompletedStatus = !AllCompletedTodo; | ||
const todosToUpdate = todos.filter( | ||
todo => todo.completed !== newCompletedStatus, | ||
); | ||
|
||
todosToUpdate.map(todo => | ||
handleUpdateTodo(todo.id, todo.title, newCompletedStatus), | ||
); | ||
}; | ||
|
||
return ( | ||
<header className="todoapp__header"> | ||
{todos.length > 0 && ( | ||
<button | ||
type="button" | ||
className={classNames('todoapp__toggle-all', { | ||
active: AllCompletedTodo, | ||
})} | ||
data-cy="ToggleAllButton" | ||
onClick={handleChangleAllStatus} | ||
disabled={loadingIds.length > 0} | ||
/> | ||
)} | ||
<form onKeyDown={handleAddTodo}> | ||
<input | ||
ref={inputRef} | ||
data-cy="NewTodoField" | ||
value={title} | ||
type="text" | ||
className="todoapp__new-todo" | ||
placeholder="What needs to be done?" | ||
onChange={handleInputValue} | ||
disabled={!!tempTodo} | ||
/> | ||
</form> | ||
</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.
You can remove FilterParam into types folder and use it instead type Filter