Skip to content
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

Todo app with api #766

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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 @@ -41,4 +41,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://dimkamg21.github.io/react_todo-app-with-api/) and add it to the PR description.
322 changes: 308 additions & 14 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,318 @@
/* eslint-disable max-len */
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import classNames from 'classnames';
import { Todo } from './types/Todo';
import { UserWarning } from './UserWarning';

const USER_ID = 0;
import { TodoList } from './components/TodoList/TodoList';
import * as todoService from './api/todos';
import { USER_ID } from './api/Personal_Id';
import { Status } from './types/Status';

export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [title, setTitle] = useState('');
const [errorMessage, setErrorMessage] = useState('');
const [status, setStatus] = useState<Status>(Status.All);
const [visible, setVisible] = useState(false);
const [disableInput, setDisableInput] = useState(false);
const [tempoTodo, setTempoTodo] = useState<Todo | null>(null);
const [loadingTodoId, setLoadingTodoId] = useState<number[]>([]);

const completedTodosCount = todos.reduce((acc, todo) => {
return todo.completed ? acc + 1 : acc;
}, 0);

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

const activeTodosCount = todos.length - completedTodosCount;

const visibleTodos = todos.filter(todo => {
switch (status) {
case Status.Active:
return !todo.completed;

case Status.Completed:
return todo.completed;

case Status.All:
default:
return true;
}
});

const resetAddTodoState = () => {
setTempoTodo(null);
setTitle('');
setDisableInput(false);
};

const createTempTodo = () => ({
id: 0,
userId: USER_ID,
title: title.trim(),
completed: false,
});

const handleSubmit = (todoTitle: string) => {
const trimmedTitle = todoTitle.trim();
const tempTodo = createTempTodo();

setTempoTodo(tempTodo);

setDisableInput(true);

if (!trimmedTitle) {
setErrorMessage("Title can't be empty");

return;
}

setTimeout(() => {
todoService
.addTodos(tempTodo)
.then((newTodo) => setTodos([...todos, newTodo]))
.catch(() => setErrorMessage('Unable to add a todo'))
.finally(() => resetAddTodoState());
}, 500);
};

const deleteTodo = (todoId: number) => {
setLoadingTodoId([todoId]);

todoService.deletePost(todoId)
.then(() => setTodos([
...todos.filter(todo => todo.id !== todoId),
]))
.catch(() => setErrorMessage('Unable to delete the todo'));
};

const handleClearCompletedTodos = useCallback(() => {
const completedTodos = todos.filter(todo => todo.completed);

const completedTodoIds = completedTodos.map(todo => todo.id);

setLoadingTodoId(completedTodoIds);

Promise.all(
completedTodos.map(todo => (
todoService.deletePost(todo.id)
)),
)
.then(() => {
setTodos(todos.filter(todo => !todo.completed));
})
.catch(() => {
setErrorMessage('Unable to delete completed todos');
})
.finally(() => setLoadingTodoId([]));
}, [todos]);

const toggleTodoStatus = async (todoId: number, completed: boolean) => {
try {
setLoadingTodoId([todoId]);
await todoService.updatePostStatus(todoId, completed);

setTodos(currentTodos => currentTodos.map(
todo => (todoId === todo.id ? { ...todo, completed } : todo),
));
} catch {
setErrorMessage('Unable to update a todo');
} finally {
setLoadingTodoId([]);
}
};

const handleToggleTodosAll = async (completed: boolean) => {
const todosIds = todos
.filter(todo => todo.completed !== completed)
.map(todo => todo.id);

try {
setLoadingTodoId(todosIds);

const updatedTodos = todos.map(todo => ({
...todo,
completed,
}));

setTodos(updatedTodos);

await Promise.all(
todos.map(todo => todoService.updatePostStatus(todo.id, completed)),
);
} catch {
setErrorMessage('Unable to update a todos');
} finally {
setLoadingTodoId([]);
}
};

const handleEditTodo = async (todoId: number, newTitle: string) => {
if (!newTitle) {
setErrorMessage('Title can`t be empty');

return;
}

try {
setLoadingTodoId([todoId]);
const updatedTodo = await todoService.updateTodoTitle(todoId, newTitle);

setTodos(currentTodos => currentTodos.map(
todo => (todoId === todo.id ? updatedTodo : todo),
));
} catch {
setErrorMessage('Title can`t be empty');
} finally {
setLoadingTodoId([]);
}
};

useEffect(() => {
if (errorMessage) {
setTimeout(() => {
setErrorMessage('');
}, 3000);
}
}, [errorMessage]);

useEffect(() => {
todoService.getTodos()
.then(setTodos)
.catch(() => setErrorMessage('Unable to load a todo'));

setTimeout(() => {
setVisible(true); // Set to true to trigger the transition
}, 1000);
}, []);

if (!USER_ID) {
return <UserWarning />;
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={classNames('todoapp__content', {
visible,
})}
>
<header className="todoapp__header">
{todos.length > 0 && (
<button
type="button"
className={`todoapp__toggle-all ${hasCompleted ? 'active' : ''} `}
onClick={() => handleToggleTodosAll(!hasCompleted)}
/>
)}

<form onSubmit={(e) => {
handleSubmit(title);
e.preventDefault();
}}
>
<input
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
value={title}
onChange={event => setTitle(event.target.value)}
disabled={disableInput}
/>
</form>
</header>

{(todos.length > 0 || tempoTodo) && (
<div
className={classNames('todo-list', {
visible,
})}
>

<TodoList
todos={visibleTodos}
deleteTodo={deleteTodo}
tempoTodo={tempoTodo}
uptadeTodoStatus={toggleTodoStatus}
loadingTodoId={loadingTodoId}
onChangeTodoTitle={handleEditTodo}
/>

<footer className="todoapp__footer">
<span className="todo-count">
{`${activeTodosCount} items left`}
</span>

<nav className="filter">
<a
href="#/"
className={classNames('filter__link', {
selected: status === Status.All,
})}
onClick={() => setStatus(Status.All)}

>
All
</a>

<a
href="#/active"
className={classNames('filter__link', {
selected: status === Status.Active,
})}
onClick={() => setStatus(Status.Active)}
>
Active
</a>

<a
href="#/completed"
className={classNames('filter__link', {
selected: status === Status.Completed,
})}
onClick={() => setStatus(Status.Completed)}
>
Completed
</a>
</nav>

<button
type="button"
className={classNames('todoapp__clear-completed', {
'todoapp__clear-completed--hidden': completedTodosCount === 0,
})}
onClick={handleClearCompletedTodos}
>
Clear completed
</button>
</footer>
</div>
)}
</div>

<div
className={classNames(
'notification',
'is-danger',
'is-light',
'has-text-weight-normal',
{
hidden: !errorMessage,
},
)}
>
<button
type="button"
className="delete"
onClick={() => setErrorMessage('')}
/>

{errorMessage}
</div>
</div>
);
};
1 change: 1 addition & 0 deletions src/api/Personal_Id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const USER_ID = 11407;
25 changes: 25 additions & 0 deletions src/api/todos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Todo } from '../types/Todo';
import { client } from '../utils/fetchClient';
import { USER_ID } from './Personal_Id';

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

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

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

export const updatePostStatus = (id: number, completed: boolean) => {
return client.patch(`/todos/${id}`, { completed });
};

export const updateTodoTitle = (
todoId: number, title: string,
): Promise<Todo> => {
return client.patch<Todo>(`/todos/${todoId}`, { title });
};
30 changes: 30 additions & 0 deletions src/components/TempoTodoItem/TempoTodoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { Todo } from '../../types/Todo';

type Props = {
tempoTodo: Todo;
};

export const TempoTodoItem: React.FC<Props> = ({ tempoTodo }) => (
<div className="todo">
<label className="todo__status-label">
<input
type="checkbox"
className="todo__status"
title="chekbox"
/>
</label>

<span className="todo__title">{tempoTodo.title}</span>
<button
type="button"
className="todo__remove"
>
×
</button>
<div className="modal overlay is-active">
<div className="modal-background has-background-white-ter" />
<div className="loader" />
</div>
</div>
);
Loading
Loading