Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ Implement the ability to edit a todo title on double click:

- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_todo-app-with-api/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://Lazarin123.github.io/react_todo-app-with-api/) and add it to the PR description.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.8.5",
"@mate-academy/scripts": "^2.1.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
267 changes: 254 additions & 13 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,267 @@
/* 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 { UserWarning } from './UserWarning';

const USER_ID = 0;
import { USER_ID } from './api/todos';
import { TodoContext } from './context/TodoContext';
import { ERROR_TYPE, FILTER_TYPE } from './consts/constants';

export const App: React.FC = () => {
const {
todos,
todoTitle,
filteredTodos,
filterBy,
setFilterBy,
loadingIds,
error,
editingId,
setEditingId,
unfinishedTodos,
editTodoTitle,
setEditTodoTitle,
inputRef,
editTodoRef,
tempTodo,
handleEditingTodo,
handleTitleChange,
handleToggleAll,
handleEditFormSubmission,
handleDeleteTodo,
handleCloseError,
handleTodoToggle,
handleClearCompleted,
handleSubmitNewTodo,
} = React.useContext(TodoContext);

const hasCompletedTodo = todos.some(todo => todo.completed);

const allTodosCompleted = todos.every(todo => todo.completed);

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>
<div className="todoapp">
<h1 className="todoapp__title">todos</h1>

<div className="todoapp__content">
<header className="todoapp__header">
{/* this button should have `active` class only if all todos are completed */}
{todos.length > 0 && (
<button
type="button"
onClick={() => handleToggleAll(todos)}
className={`todoapp__toggle-all ${allTodosCompleted && 'active'}`}
data-cy="ToggleAllButton"
/>
)}

<form onSubmit={handleSubmitNewTodo}>
<input
data-cy="NewTodoField"
ref={inputRef}
type="text"
value={todoTitle}
onChange={handleTitleChange}
className="todoapp__new-todo"
disabled={loadingIds.length > 0}
placeholder="What needs to be done?"
/>
</form>
</header>

<section className="todoapp__main" data-cy="TodoList">
{/* This is a completed todo */}
{filteredTodos.map(todo => (
<div
key={todo.id}
data-cy="Todo"
onDoubleClick={() => {
setEditingId(todo.id);
setEditTodoTitle(todo.title);
}}
className={`todo ${todo.completed ? 'completed' : ''}`}
>
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
onChange={() => handleTodoToggle(todo)}
checked={todo.completed}
/>
</label>

{editingId === todo.id ? (
<form
onSubmit={event => {
event.preventDefault();
handleEditFormSubmission(todo);
}}
>
<input
data-cy="TodoTitleField"
ref={editTodoRef}
type="text"
onBlur={() => handleEditFormSubmission(todo)}
className="todo__title-field"
onKeyUp={event => {
if (event.key === 'Escape') {
setEditingId(null);
setEditTodoTitle(todo.title);
}
}}
onChange={event => handleEditingTodo(event)}
value={editTodoTitle}
placeholder="Empty todo will be deleted"
/>
</form>
) : (
<>
<span data-cy="TodoTitle" className="todo__title">
{todo.title}
</span>
<button
type="button"
className="todo__remove"
onClick={() => handleDeleteTodo(todo.id)}
data-cy="TodoDelete"
>
×
</button>
</>
)}

{/* overlay will cover the todo while it is being deleted or updated */}
{/* {fix is loading logic rn it sets the loader on all todos, even already loaded} */}
<div
data-cy="TodoLoader"
className={`modal overlay ${loadingIds.includes(todo.id) ? 'is-active' : ''}`}
>
<div className="modal-background has-background-white-ter" />
<div className="loader" />
</div>
</div>
))}

<p className="subtitle">Styles are already copied</p>
</section>
{tempTodo && (
<div
key={tempTodo.id}
data-cy="Todo"
className={`todo ${tempTodo.completed ? 'completed' : ''}`}
>
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
checked={tempTodo.completed}
/>
</label>

<span data-cy="TodoTitle" className="todo__title">
{tempTodo.title}
</span>
<button
type="button"
className="todo__remove"
data-cy="TodoDelete"
>
×
</button>

{/* overlay will cover the todo while it is being deleted or updated */}
{/* {fix is loading logic rn it sets the loader on all todos, even already loaded} */}
<div
data-cy="TodoLoader"
className={`modal overlay ${loadingIds.includes(tempTodo.id) ? 'is-active' : ''}`}
>
<div className="modal-background has-background-white-ter" />
<div className="loader" />
</div>
</div>
)}
</section>

{/* Hide the footer if there are no todos */}
{todos.length > 0 && (
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
{`${unfinishedTodos.length} items left`}
</span>

{/* Active link should have the 'selected' class */}
<nav className="filter" data-cy="Filter">
<a
href="#/"
onClick={() => {
setFilterBy(FILTER_TYPE.ALL);
}}
className={`filter__link ${filterBy === FILTER_TYPE.ALL ? 'selected' : ''}`}
data-cy="FilterLinkAll"
>
All
</a>

<a
href="#/active"
className={`filter__link ${filterBy === FILTER_TYPE.ACTIVE ? 'selected' : ''}`}
onClick={() => {
setFilterBy(FILTER_TYPE.ACTIVE);
}}
data-cy="FilterLinkActive"
>
Active
</a>

<a
href="#/completed"
className={`filter__link ${filterBy === FILTER_TYPE.COMPLETED ? 'selected' : ''}`}
onClick={() => {
setFilterBy(FILTER_TYPE.COMPLETED);
}}
data-cy="FilterLinkCompleted"
>
Completed
</a>
</nav>

{/* this button should be disabled if there are no completed todos */}
<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
onClick={() => handleClearCompleted(todos)}
disabled={!hasCompletedTodo}
>
Clear completed
</button>
</footer>
)}
</div>

{/* DON'T use conditional rendering to hide the notification */}
{/* Add the 'hidden' class to hide the message smoothly */}
<div
data-cy="ErrorNotification"
className={`notification is-danger ${error === ERROR_TYPE.NONE ? 'hidden' : ''} is-light has-text-weight-normal`}
>
<button
data-cy="HideErrorButton"
onClick={handleCloseError}
type="button"
className="delete"
/>
{/* show only one message at a time */}
{error === ERROR_TYPE.LOAD && `Unable to load todos`}
{error === ERROR_TYPE.TITLE && `Title should not be empty`}
{error === ERROR_TYPE.ADD && `Unable to add a todo`}
{error === ERROR_TYPE.DELETE && `Unable to delete a todo`}
{error === ERROR_TYPE.UPDATE && `Unable to update a todo`}
<br />
</div>
</div>
);
};
47 changes: 47 additions & 0 deletions src/api/todos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Todo } from '../types/Todo';
import { client } from '../utils/fetchClients';

export const USER_ID = 3716;

export const getTodos = () => {
return client.get<Todo[]>(`/todos?userId=${USER_ID}`);
};

// Add more methods here

export const postTodo = (todo: Omit<Todo, 'id'>) => {
return client.post<Todo>(`/todos`, todo);
};

export const patchTodo = (todo: Todo) => {
return client.patch<Todo>(`/todos/${todo.id}`, todo);
};

export const deleteTodo = (id: number) => {
return client.delete(`/todos/${id}`);
};

export const patchAllTodos = (todos: Todo[]) => {
const completedTodos = !todos.every(todo => todo.completed);
const alteredTodos = Promise.all([
...todos
.filter(todo => todo.completed !== completedTodos)
.map(todo =>
client.patch<Todo>(`/todos/${todo.id}`, {
...todo,
completed: completedTodos,
}),
),
]);

return alteredTodos;
};

export const deleteCompletedTodos = (todos: Todo[]) => {
const completedTodos = todos.filter(todo => todo.completed);
const deletedTodos = Promise.allSettled([
...completedTodos.map(todo => client.delete(`/todos/${todo.id}`)),
]);

return deletedTodos;
};
14 changes: 14 additions & 0 deletions src/consts/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const FILTER_TYPE = {
ALL: 'all',
ACTIVE: 'active',
COMPLETED: 'completed',
} as const;

export const ERROR_TYPE = {
LOAD: 'loadError',
TITLE: 'titleError',
ADD: 'addError',
DELETE: 'deleteError',
UPDATE: 'updateError',
NONE: 'none',
} as const;
Loading
Loading