It is the second part of the React Todo App with API.
Take your code implemented for Loading todos and implement the ability to add and remove todos.
Here is the working example
❗️❗️❗️
Please implement only adding and deleting todos in addition to what was already implemented.
All the other features from the working version will be implemented in the next task.
❗️❗️❗️
Check the API Documentation
Add a todo with the entered title on the form submit:
- text field should be focused by default;
- if the title is empty show the
Title should not be empty
notification at the bottom; - trim the title when checked or saved;
- use your
userId
for the new todo; - send a POST request to the API (check the API Documentation)
- disable the input until receiving a response from the API;
- immediately after sending a request create a todo with
id: 0
and save it to thetempTodo
variable in the state (NOT to thetodos
array); - show an independent
TodoItem
after the list iftempTodo
is notnull
; - temp TodoItem should have the loader (check the original markup);
- in case of success add the todo created by the API to the array (take it from the POST response);
- in case of an API error showing
Unable to add a todo
notification at the bottom; - set
tempTodo
tonull
to hide the extraTodoItem
; - focus the text field after receiving a response;
- clear the text in case of success;
- keep the text in case of error;
Don't try to implement animations for adding or removing Todos (at least until you finish everything else). If you really feel confident to try, there is a hint at the end of the description.
Remove a todo on the TodoDeleteButton
click:
- covered the todo with the loader while waiting for the API response;
- remove the todo from the list on success;
- in case of API error show
Unable to delete a todo
notification at the bottom (the todo must stay in the list);
Remove all the completed todos after the Clear completed
button click:
- the button should be enabled only if there is at least 1 completed todo;
- the deletion should work as several individual deletions running at the same time;
- in case of any error show error message but process success deletions;
- Implement a solution following the React task guideline.
- Use the React TypeScript cheat sheet.
- Replace
<your_account>
with your Github username in the DEMO LINK and add it to the PR description.
Click here to see the hint
<section className="todoapp__main" data-cy="TodoList">
<TransitionGroup>
{visibleTodos.map(todo => (
<CSSTransition
key={todo.id}
timeout={300}
classNames="item"
>
<TodoItem
todo={todo}
isProcessed={processings.includes(todo.id)}
onDelete={() => deleteTodo(todo.id)}
onUpdate={updateTodo}
/>
</CSSTransition>
))}
{creating && (
<CSSTransition
key={0}
timeout={300}
classNames="temp-item"
>
<TodoItem
todo={{
id: Math.random(),
title,
completed: false,
userId: user.id,
}}
isProcessed
/>
</CSSTransition>
)}
</TransitionGroup>
</section>
Here are the styles used in this example
.item-enter {
max-height: 0;
}
.item-enter-active {
overflow: hidden;
max-height: 58px;
transition: max-height 0.3s ease-in-out;
}
.item-exit {
max-height: 58px;
}
.item-exit-active {
overflow: hidden;
max-height: 0;
transition: max-height 0.3s ease-in-out;
}
.temp-item-enter {
max-height: 0;
}
.temp-item-enter-active {
overflow: hidden;
max-height: 58px;
transition: max-height 0.3s ease-in-out;
}
.temp-item-exit {
max-height: 58px;
}
.temp-item-exit-active {
transform: translateY(-58px);
max-height: 0;
opacity: 0;
transition: 0.3s ease-in-out;
transition-property: opacity, max-height, transform;
}
.has-error .temp-item-exit-active {
transform: translateY(0);
overflow: hidden;
}