-
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 #799
Open
Sancz0pansa
wants to merge
15
commits into
mate-academy:master
Choose a base branch
from
Sancz0pansa: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 #799
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4121bbb
Solution
Sancz0pansa 2700953
test
Sancz0pansa f6e2c74
*
Sancz0pansa d28bbd1
t
Sancz0pansa 8ef0517
*
Sancz0pansa 7cde92a
*
Sancz0pansa 538b8e1
q
Sancz0pansa 8f26092
*
Sancz0pansa ea8aa71
*
Sancz0pansa 4faaae3
*
Sancz0pansa faff100
*
Sancz0pansa 8cc2c12
*
Sancz0pansa 6cd3e5b
solution
Sancz0pansa d15ef3d
fix
Sancz0pansa da12c37
*
Sancz0pansa 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
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 |
---|---|---|
@@ -1,24 +1,238 @@ | ||
/* eslint-disable max-len */ | ||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||
import React from 'react'; | ||
/* eslint-disable no-param-reassign */ | ||
|
||
import React, { | ||
FormEventHandler, useEffect, useRef, useState, | ||
} from 'react'; | ||
import cn from 'classnames'; | ||
import { UserWarning } from './UserWarning'; | ||
import { | ||
addTodo, updateTodo, deleteTodo, getTodos, | ||
} from './api/todos'; | ||
import { Todo } from './types/Todo'; | ||
import { Filter } from './types/Filter'; | ||
import { ErrorMess } from './types/Error'; | ||
import { TodoList } from './components/TodoList'; | ||
import { Footer } from './components/Footer'; | ||
import { NewTodoInput } from './components/NewTodoInput'; | ||
|
||
const USER_ID = 0; | ||
const USER_ID = 10521; | ||
|
||
export const App: React.FC = () => { | ||
const [todos, setTodos] = useState<Todo[]>([]); | ||
const [error, setError] = useState<ErrorMess>(null); | ||
const [filter, setFilter] = useState<Filter>('All'); | ||
const [title, setTitle] = useState<string>(''); | ||
const [temporaryTodo, setTemporaryTodo] = useState<Todo | null>(null); | ||
const [isSubmiting, setIsSubmiting] = useState<boolean>(false); | ||
const [counter, setCounter] = useState<number>(0); | ||
const inputRef = useRef<HTMLInputElement | null>(null); | ||
|
||
const handleError = (mess: ErrorMess) => { | ||
setError(mess); | ||
setTimeout(() => setError(null), 3000); | ||
}; | ||
|
||
useEffect(() => { | ||
if (!isSubmiting) { | ||
inputRef.current?.focus(); | ||
} | ||
}, [isSubmiting]); | ||
|
||
const fetchData = async () => { | ||
try { | ||
const todoss = getTodos(USER_ID); | ||
const count = (await todoss).filter(todo => todo.completed === false) | ||
.length; | ||
|
||
setTodos(await todoss); | ||
setCounter(count); | ||
} catch (e) { | ||
handleError('Unable to load todos'); | ||
} | ||
}; | ||
|
||
const handleCount = () => { | ||
const count = todos.filter(toDo => toDo.completed === false) | ||
.length; | ||
|
||
setCounter(count); | ||
}; | ||
|
||
const handleComplete = (todo: Todo, callback: () => void) => { | ||
updateTodo(todo.id, { | ||
completed: !todo.completed, | ||
}).then(() => { | ||
todo.completed = !todo.completed; | ||
handleCount(); | ||
}).catch(() => { | ||
handleError('Unable to update a todo'); | ||
}).finally(() => callback()); | ||
}; | ||
|
||
const handleCompleteALL = (toDos: Todo[], data: boolean) => { | ||
toDos.forEach(todo => { | ||
if (data === true && todo.completed === false) { | ||
updateTodo(todo.id, { | ||
completed: data, | ||
}).catch(() => { | ||
handleError('Unable to update a todo'); | ||
}).finally(() => { | ||
fetchData(); | ||
handleCount(); | ||
}); | ||
} else if (data === false) { | ||
updateTodo(todo.id, { | ||
completed: data, | ||
}).catch(() => { | ||
handleError('Unable to update a todo'); | ||
}).finally(() => { | ||
fetchData(); | ||
handleCount(); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
const handleDelete = (todo: Todo, callback?: () => void) => { | ||
deleteTodo(todo.id).then(() => { | ||
setTodos(prevTodo => prevTodo.filter(toDo => toDo !== todo)); | ||
inputRef.current?.focus(); | ||
fetchData(); | ||
}).catch(() => { | ||
handleError('Unable to delete todo'); | ||
if (callback) { | ||
callback(); | ||
} | ||
}); | ||
}; | ||
|
||
const handleAdd = () => { | ||
setIsSubmiting(true); | ||
addTodo({ | ||
id: 0, | ||
userId: USER_ID, | ||
title: title.trim(), | ||
completed: false, | ||
}).then((response) => { | ||
setTitle(''); | ||
setCounter(oldCount => oldCount + 1); | ||
setTodos((prevTodos) => [...prevTodos, response] as Todo[]); | ||
}).catch(() => { | ||
handleError('Unable to add a todo'); | ||
}).finally(() => { | ||
setTemporaryTodo(null); | ||
setIsSubmiting(false); | ||
}); | ||
}; | ||
|
||
const handleSubmit: FormEventHandler = (e) => { | ||
e.preventDefault(); | ||
if (title.trim() === '') { | ||
handleError('Title should not be empty'); | ||
|
||
return; | ||
} | ||
|
||
handleAdd(); | ||
|
||
setTemporaryTodo({ | ||
id: 0, | ||
userId: USER_ID, | ||
title: title.trim(), | ||
completed: false, | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
fetchData(); | ||
}, []); | ||
|
||
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"> | ||
<header className="todoapp__header"> | ||
{/* this buttons is active only if there are some active todos */} | ||
{todos.length > 0 && ( | ||
<button | ||
type="button" | ||
className={cn('todoapp__toggle-all', | ||
{ active: todos.every(todo => todo.completed) })} | ||
data-cy="ToggleAllButton" | ||
onClick={() => { | ||
if (todos.every(todo => todo.completed === true)) { | ||
handleCompleteALL(todos, false); | ||
} else { | ||
handleCompleteALL(todos, true); | ||
} | ||
}} | ||
/> | ||
)} | ||
|
||
{/* Add a todo on form submit */} | ||
<NewTodoInput | ||
handleSubmit={handleSubmit} | ||
title={title} | ||
isSubmiting={isSubmiting} | ||
setTitle={(e) => setTitle(e)} | ||
inputRef={inputRef} | ||
/> | ||
</header> | ||
|
||
{(todos.length > 0 || temporaryTodo) && ( | ||
<TodoList | ||
todos={todos} | ||
filter={filter} | ||
temporaryTodo={temporaryTodo} | ||
handleDelete={handleDelete} | ||
handleComplete={handleComplete} | ||
handleError={handleError} | ||
/> | ||
)} | ||
|
||
{/* Hide the footer if there are no todos */} | ||
{(todos.length > 0 || temporaryTodo) | ||
&& ( | ||
<Footer | ||
counter={counter} | ||
filter={filter} | ||
setFilter={(f) => setFilter(f)} | ||
todos={todos} | ||
handleDelete={handleDelete} | ||
/> | ||
)} | ||
</div> | ||
|
||
{/* Notification is shown in case of any error */} | ||
{/* Add the 'hidden' class to hide the message smoothly */} | ||
<div | ||
data-cy="ErrorNotification" | ||
className={cn('notification is-danger is-light has-text-weight-normal', | ||
{ hidden: error === null })} | ||
> | ||
{error | ||
&& ( | ||
<div | ||
className="notification is-danger is-light has-text-weight-normal" | ||
> | ||
<button | ||
data-cy="HideErrorButton" | ||
type="button" | ||
className="delete" | ||
onClick={() => setError(null)} | ||
/> | ||
</div> | ||
|
||
)} | ||
{/* show only one message at a time */} | ||
{error} | ||
</div> | ||
</div> | ||
); | ||
}; |
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 think most of the code belongs to global state - this should not be in App.tsx file