-
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 #708
Open
JOWISSA
wants to merge
12
commits into
mate-academy:master
Choose a base branch
from
JOWISSA: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 #708
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
67075bd
solution
JOWISSA a04699d
upd
JOWISSA 0f96ab1
upd
JOWISSA cd36639
upd-2
JOWISSA 24654eb
upd-3
JOWISSA 18bc34d
upd-4
JOWISSA 4c14365
upd-5
JOWISSA a52d504
upd-5
JOWISSA 66c5340
upd-6
JOWISSA 999790f
6
JOWISSA 4a270f1
solution
JOWISSA 00a1b2d
upd 4.5
JOWISSA 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,24 +1,199 @@ | ||
/* eslint-disable max-len */ | ||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||
import React from 'react'; | ||
import React, { useEffect, useMemo, useState } from 'react'; | ||
import { UserWarning } from './UserWarning'; | ||
import { TodoList } from './Components/ToDoList'; | ||
import { Notification } from './Components/errorNotification'; | ||
import { client } from './utils/fetchClient'; | ||
import { Error } from './types/Error'; | ||
|
||
const USER_ID = 0; | ||
import { FilterType, Todo } from './types/Todo'; | ||
|
||
import { getTodos, deleteTodo } from './api/todos'; | ||
|
||
import { Footer } from './Components/Footer'; | ||
|
||
const USER_ID = 6340; | ||
|
||
export const App: React.FC = () => { | ||
const [todos, setTodos] = useState<Todo[]>([]); | ||
const [filterBy, setFilterBy] = useState<FilterType>(FilterType.ALL); | ||
const [error, setError] = useState<Error>(Error.NONE); | ||
const [inputQuery, setInputQuery] = useState(''); | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const loadTodosData = async () => { | ||
try { | ||
setError(Error.NONE); | ||
const todosFromServer = await getTodos(USER_ID); | ||
|
||
setTodos(todosFromServer); | ||
} catch { | ||
setError(Error.DOWNLOADING); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
loadTodosData(); | ||
}, []); | ||
|
||
const addNewTodo = async () => { | ||
if (!inputQuery.trim()) { | ||
setError(Error.NONE); | ||
|
||
return; | ||
} | ||
|
||
try { | ||
setError(Error.NONE); | ||
setIsLoading(true); | ||
|
||
const newTodo = { | ||
userId: USER_ID, | ||
title: inputQuery.trim(), | ||
completed: false, | ||
}; | ||
|
||
const response = await client.post<Todo>('/todos', newTodo); | ||
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.
better to keep such functions in separated |
||
|
||
setTodos([...todos, response]); | ||
setInputQuery(''); | ||
} catch { | ||
setError(Error.ADD); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
const deleteTodoItem = async (todoId: number) => { | ||
try { | ||
setError(Error.NONE); | ||
await deleteTodo(todoId); | ||
|
||
setTodos(todos.filter(todo => todo.id !== todoId)); | ||
} catch { | ||
setError(Error.DELETE); | ||
} | ||
}; | ||
|
||
const handleFormSubmit = (e: React.FormEvent) => { | ||
e.preventDefault(); | ||
addNewTodo(); | ||
}; | ||
|
||
const handleTodoDelete = (todoId: number) => { | ||
deleteTodoItem(todoId); | ||
}; | ||
|
||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setInputQuery(e.target.value); | ||
}; | ||
|
||
const handleToggleAll = () => { | ||
const areAllCompleted = todos.every(todo => todo.completed); | ||
|
||
const updatedTodos = todos.map(todo => ({ | ||
...todo, | ||
completed: !areAllCompleted, | ||
})); | ||
|
||
setTodos(updatedTodos); | ||
}; | ||
|
||
const visibleTodos = useMemo(() => { | ||
switch (filterBy) { | ||
case FilterType.ACTIVE: | ||
return todos.filter((todo) => !todo.completed); | ||
case FilterType.COMPLETED: | ||
return todos.filter((todo) => todo.completed); | ||
default: | ||
return todos; | ||
} | ||
}, [filterBy, todos]); | ||
|
||
if (!USER_ID) { | ||
return <UserWarning />; | ||
} | ||
|
||
const updateTodoItem = async (todoId: number, updatedTodo: Todo) => { | ||
try { | ||
setError(Error.NONE); | ||
await client.patch<Todo>(`/todos/${todoId}`, updatedTodo); | ||
|
||
setTodos(prevTodos => prevTodos.map( | ||
todo => (todo.id === todoId ? updatedTodo : todo), | ||
)); | ||
} catch { | ||
setError(Error.UPDATE); | ||
} | ||
}; | ||
|
||
const deleteAllCompletedTodos = () => { | ||
const completedTodos = todos.filter((todo) => todo.completed); | ||
|
||
const deletePromises = completedTodos.map((todo) => deleteTodo(todo.id)); | ||
|
||
Promise.all(deletePromises) | ||
.then(() => { | ||
setTodos((prevTodos) => prevTodos.filter((todo) => !todo.completed)); | ||
}) | ||
.catch(() => { | ||
setError(Error.DELETE); | ||
}); | ||
}; | ||
|
||
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 className="todoapp__header"> | ||
<button | ||
type="button" | ||
className="todoapp__toggle-all active" | ||
onClick={handleToggleAll} | ||
/> | ||
<form onSubmit={handleFormSubmit}> | ||
<input | ||
type="text" | ||
className="todoapp__new-todo" | ||
placeholder="What needs to be done?" | ||
value={inputQuery} | ||
onChange={handleInputChange} | ||
/> | ||
</form> | ||
</header> | ||
|
||
{todos.length > 0 && ( | ||
<> | ||
<TodoList | ||
todos={visibleTodos} | ||
onDelete={handleTodoDelete} | ||
onUpdate={updateTodoItem} | ||
setError={setError} | ||
/> | ||
|
||
<Footer | ||
filterBy={filterBy} | ||
setFilterBy={setFilterBy} | ||
todos={visibleTodos} | ||
onDelete={deleteAllCompletedTodos} | ||
/> | ||
</> | ||
)} | ||
</div> | ||
|
||
{isLoading && ( | ||
<div className="loader-overlay"> | ||
<div className="loader" /> | ||
</div> | ||
)} | ||
|
||
{error && ( | ||
<Notification | ||
error={error} | ||
onErrorChange={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,62 @@ | ||
import classNames from 'classnames'; | ||
import React from 'react'; | ||
import { Todo, FilterType } from '../types/Todo'; | ||
|
||
type Props = { | ||
filterBy: FilterType, | ||
setFilterBy: (value: FilterType) => void, | ||
todos: Todo[], | ||
onDelete: (id: number) => void, | ||
}; | ||
|
||
const filterOptions = Object.values(FilterType); | ||
|
||
export const Footer: React.FC<Props> = React.memo(({ | ||
filterBy, | ||
setFilterBy, | ||
todos, | ||
onDelete, | ||
}) => { | ||
const itemsLeftLength = todos.filter((todo) => !todo.completed).length; | ||
|
||
const deleteAllCompletedTodos = async () => { | ||
const completedTodoIds = todos.filter(todo => todo.completed); | ||
const idCompletesTodoId = completedTodoIds.map(todo => todo.id); | ||
|
||
await Promise.all(idCompletesTodoId.map(id => onDelete(id))); | ||
}; | ||
|
||
return ( | ||
<footer className="todoapp__footer"> | ||
<span className="todo-count"> | ||
{`${itemsLeftLength} items left`} | ||
</span> | ||
|
||
<nav className="filter"> | ||
{filterOptions.map((option) => { | ||
return ( | ||
<a | ||
key={option} | ||
href={`#/${option}`} | ||
className={classNames( | ||
'filter__link', | ||
{ selected: filterBy === option }, | ||
)} | ||
onClick={() => setFilterBy(option)} | ||
> | ||
{option} | ||
</a> | ||
); | ||
})} | ||
</nav> | ||
|
||
<button | ||
type="button" | ||
className="todoapp__clear-completed" | ||
onClick={deleteAllCompletedTodos} | ||
> | ||
Clear completed | ||
</button> | ||
</footer> | ||
); | ||
}); |
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.
Commit all files and push them