/react_todo-app-add-and-delete

Primary LanguageSCSSGNU General Public License v3.0GPL-3.0

React Todo App Add and Delete

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

Adding a todo

Add a todo with the entered title on the form submit:

  • if the title is empty show the Title can't be empty notification at the bottom;
  • 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 the tempTodo variable in the state (NOT to the todos array);
  • show an independent TodoItem after the list if tempTodo is not null;
  • 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 show Unable to add a todo notification at the bottom;
  • set tempTodo to null to hide the extra TodoItem;

Don't try to implement animations for adding or removing Todos (at least until you finish everything else). If you really fill confident to try, there is a hint at the end of the description.

Deleting todos

Remove a todo on TodoDeleteButton click:

  • covered the todo with the loader while wating 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 visible only if there is at least 1 completed todo;
  • the deletion should work as a several individual deletions running at the same time;

Instructions

IF you want to implement smooth animations

Click here to see the hint

Use React Transition Group

<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;
}