Skip to content
Open

Develop #2239

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
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ESLINT_NO_DEV_ERRORS=true
BROWSER=chrome
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://Visol.github.io/react_todo-app-with-api/) and add it to the PR description.
237 changes: 224 additions & 13 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,237 @@
/* eslint-disable max-len */
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import React, { FormEvent, useEffect, useState } from 'react';
import { UserWarning } from './UserWarning';

const USER_ID = 0;
import { createTodo, deleteTodo, getTodos, USER_ID } from './api/todos';
import { ErrorMessage, Status, Todo } from './types/Todo';
import classNames from 'classnames';
import { getActiveTodos, getFilteredTodos } from './utils/helper';
import { TodoItem } from './components';

export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [errorMessage, setErrorMessage] = useState('');
const [statusFilter, setStatusFilter] = useState(Status.all);
const [newTitle, setNewTitle] = useState('');
const [tempTodo, setTempTodo] = useState<Todo | null>(null);
const [waitingDeleteTodos, setWaitingDeleteTodos] = useState<number[]>([]);
const [keyToForm, setKeyToForm] = useState(0);
const [formDisabled, setFormDisabled] = useState(false);

useEffect(() => {
getTodos()
.then(setTodos)
.catch(() => setErrorMessage(ErrorMessage.Load))
.finally();
}, []);

useEffect(() => {
if (!errorMessage) {
return;
}

const timerId = setTimeout(() => {
setErrorMessage('');
}, 3000);

return () => clearTimeout(timerId);
}, [errorMessage]);

if (!USER_ID) {
return <UserWarning />;
}

const handleDeleteTodo = (id: number) => {
setWaitingDeleteTodos(current => [...current, id]);
deleteTodo(id)
.then(() => {
setTodos(current => current.filter(todo => todo.id !== id));
})
.catch(() => {
setErrorMessage(ErrorMessage.Delete);
})
.finally(() => {
setWaitingDeleteTodos(current => current.filter(item => item !== id));
setKeyToForm(current => current + 1);
});
};

const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
const normalizedTitle = newTitle.trim();

if (!normalizedTitle) {
setErrorMessage(ErrorMessage.TitleEmpty);

return;
}

setTempTodo({
id: 0,
title: normalizedTitle,
completed: false,
userId: USER_ID,
});

setFormDisabled(true);

createTodo(normalizedTitle)
.then(todo => {
setTodos(current => [...current, todo]);
setNewTitle('');
})
.catch(() => setErrorMessage(ErrorMessage.Add))
.finally(() => {
setTempTodo(null);
setKeyToForm(current => current + 1);
setFormDisabled(false);
});
};

const handleBundleDelete = () => {
for (const todo of todos) {
if (todo.completed) {
handleDeleteTodo(todo.id);
}
}
};

// const showTodosAndFooter = todos.length > 0;
const showToggleAllButton = todos.length > 0;
const activeTodos = getActiveTodos(todos);
const filteredTodos = getFilteredTodos(todos, statusFilter);
const isAllCompleted = todos.length > 0 && activeTodos.length === 0;
const completedTodos = todos.length - activeTodos.length;

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 */}
<button
type="button"
className={classNames('todoapp__toggle-all', {
active: isAllCompleted,
})}
data-cy="ToggleAllButton"
/>

{/* Add a todo on form submit */}
<form onSubmit={handleSubmit} key={keyToForm}>
<input
data-cy="NewTodoField"
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
value={newTitle}
onChange={event => setNewTitle(event.target.value)}
autoFocus
disabled={formDisabled}
/>
</form>
</header>

<section className="todoapp__main" data-cy="TodoList">
{filteredTodos.map(todo => (
<TodoItem
key={todo.id}
todo={todo}
onDelete={handleDeleteTodo}
loader={waitingDeleteTodos.includes(todo.id)}
/>
))}

{tempTodo && (
<TodoItem todo={tempTodo} loader={true} onDelete={() => {}} />
)}
</section>

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

{/* Active link should have the 'selected' class */}
<nav className="filter" data-cy="Filter">
<a
href="#/"
className={classNames('filter__link', {
selected: statusFilter === Status.all,
})}
data-cy="FilterLinkAll"
onClick={() => setStatusFilter(Status.all)}
>
All
</a>

<p className="subtitle">Styles are already copied</p>
</section>
<a
href="#/active"
className={classNames('filter__link', {
selected: statusFilter === Status.active,
})}
data-cy="FilterLinkActive"
onClick={() => setStatusFilter(Status.active)}
>
Active
</a>

<a
href="#/completed"
className={classNames('filter__link', {
selected: statusFilter === Status.completed,
})}
data-cy="FilterLinkCompleted"
onClick={() => setStatusFilter(Status.completed)}
>
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={handleBundleDelete}
disabled={completedTodos < 1}
>
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={classNames(
'notification is-danger is-light has-text-weight-normal',
{ hidden: !errorMessage },
)}
>
<button
data-cy="HideErrorButton"
type="button"
className="delete"
onClick={() => setErrorMessage('')}
/>
{/* show only one message at a time */}
{errorMessage}
{/* Unable to load todos
<br />
Title should not be empty
<br />
Unable to add a todo
<br />
Unable to delete a todo
<br />
Unable to update a todo */}
</div>
</div>
);
};
22 changes: 22 additions & 0 deletions src/api/todos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Todo } from '../types/Todo';
import { client } from '../utils/fetchClient';

export const USER_ID = 4234;

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

export const createTodo = (title: string) => {
const data: Omit<Todo, 'id'> = {
userId: Number(USER_ID),
title,
completed: false,
};

return client.post<Todo>(`/todos`, data);
};

export const deleteTodo = (id: number) => {
return client.delete(`/todos/${id}`);
};
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './todoItem';
59 changes: 59 additions & 0 deletions src/components/todoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import classNames from 'classnames';
import { Todo } from '../types/Todo';

type TodoItemProps = {
todo: Todo;
loader?: boolean;
onDelete: (id: number) => void;
};

export function TodoItem({ todo, loader = false, onDelete }: TodoItemProps) {
const todoId = `todo-status-${todo.id}`;

return (
<div
key={todo.id}
data-cy="Todo"
className={classNames('todo is-active', {
'todo completed': todo.completed,
})}
>
<label className="todo__status-label" htmlFor={todoId}>
<input
id={todoId}
aria-label="Toggle todo completion"
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
checked={todo.completed}
onChange={() => {}}
/>
</label>

<span data-cy="TodoTitle" className="todo__title">
{todo.title}
</span>

{/* Remove button appears only on hover */}
<button
type="button"
className="todo__remove"
data-cy="TodoDelete"
onClick={() => onDelete(todo.id)}
>
×
</button>

{/* overlay will cover the todo while it is being deleted or updated */}
<div
data-cy="TodoLoader"
className={classNames('modal overlay', {
'is-active': loader,
})}
>
<div className="modal-background has-background-white-ter" />
<div className="loader" />
</div>
</div>
);
}
20 changes: 20 additions & 0 deletions src/types/Todo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export interface Todo {
id: number;
userId: number;
title: string;
completed: boolean;
}

export enum Status {
all = 'all',
completed = 'completed',
active = 'active',
}

export enum ErrorMessage {
Load = 'Unable to load todos',
Add = 'Unable to add a todo',
Delete = 'Unable to delete a todo',
Update = 'Unable to update a todo',
TitleEmpty = 'Title should not be empty',
}
Loading
Loading