Skip to content
Open

Develop #2126

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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and implement the ability to toggle and rename todos.
## Toggling a todo status

Toggle the `completed` status on `TodoStatus` change:

- Install Prettier Extention and use this [VSCode settings](https://mate-academy.github.io/fe-program/tools/vscode/settings.json) to enable format on save.
- covered the todo with a loader overlay while waiting for API response;
- the status should be changed on success;
Expand Down Expand Up @@ -38,6 +39,7 @@ Implement the ability to edit a todo title on double click:
- or the deletion error message if we tried to delete the todo.

## If you want to enable tests

- open `cypress/integration/page.spec.js`
- replace `describe.skip` with `describe` for the root `describe`

Expand All @@ -47,4 +49,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://maksymivivan2000-dotcom.github.io/react_todo-app-with-api/) and add it to the PR description.
88 changes: 74 additions & 14 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,86 @@
/* 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 { USER_ID } from './api/todos';
import { ErrorNotification } from './components/ErrorNotification';
import { Footer } from './components/Footer';
import { Header } from './components/Header';
import { TodoList } from './components/TodoList';
import { useTodos } from './hooks/useTodos';
import { UserWarning } from './UserWarning';

const USER_ID = 0;

export const App: React.FC = () => {
const {
todos,
isLoading,
isAdding,
errorMessage,
filter,
newTitle,
tempTodo,
loadingTodoIds,
newTodoInputRef,
visibleTodos,
setFilter,
setNewTitle,
closeError,
handleAddTodo,
handleDeleteTodo,
handleClearCompleted,
handleToggleTodo,
handleToggleAll,
handleRenameTodo,
} = useTodos();

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>

<p className="subtitle">Styles are already copied</p>
</section>
<div className="todoapp">
<h1 className="todoapp__title">todos</h1>

<div className="todoapp__content">
{isLoading && (
<div className="modal overlay is-active">
<div className="modal-background has-background-white-ter" />
<div className="loader" />
</div>
)}

<Header
allCompleted={todos.length > 0 && todos.every(todo => todo.completed)}
newTitle={newTitle}
isAdding={isAdding}
inputRef={newTodoInputRef}
onNewTitleChange={setNewTitle}
onSubmit={handleAddTodo}
hasTodos={todos.length > 0}
onToggleAll={handleToggleAll}
/>

{!isLoading && (todos.length > 0 || tempTodo) && (
<TodoList
onToggle={handleToggleTodo}
todos={visibleTodos}
loadingTodoIds={loadingTodoIds}
tempTodo={tempTodo}
onDelete={handleDeleteTodo}
onRename={handleRenameTodo}
/>
)}

{todos.length > 0 && (
<Footer
todos={todos}
filter={filter}
onFilterChange={setFilter}
onClearCompleted={handleClearCompleted}
/>
)}
</div>

<ErrorNotification message={errorMessage} onClose={closeError} />
</div>
);
};
23 changes: 23 additions & 0 deletions src/api/todos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Todo } from '../types/Todo';
import { client } from '../utils/fetchClient';

export const USER_ID = 12345;

type TodoPatch = Partial<Omit<Todo, 'id' | 'userId'>>;
type NewTodo = Omit<Todo, 'id'>;

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

export const addTodo = (newTodo: NewTodo) => {
return client.post<Todo>('/todos', newTodo);
};

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

export const updateTodo = (id: number, data: TodoPatch) => {
return client.patch<Todo>(`/todos/${id}`, data);
};
23 changes: 23 additions & 0 deletions src/components/ErrorNotification.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';

type Props = {
message: string;
onClose: () => void;
};

export const ErrorNotification: React.FC<Props> = ({ message, onClose }) => {
return (
<div
data-cy="ErrorNotification"
className={`notification is-danger is-light has-text-weight-normal ${message ? '' : 'hidden'}`}
>
{message}
<button
data-cy="HideErrorButton"
type="button"
className="delete"
onClick={onClose}
/>
</div>
);
};
74 changes: 74 additions & 0 deletions src/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React from 'react';
import { Todo } from '../types/Todo';

type FilterStatus = 'all' | 'active' | 'completed';

type Props = {
todos: Todo[];
filter: FilterStatus;
onFilterChange: (filter: FilterStatus) => void;
onClearCompleted: () => void;
};

export const Footer: React.FC<Props> = ({
todos,
filter,
onFilterChange,
onClearCompleted,
}) => {
return (
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
{`${todos.filter(todo => !todo.completed).length} items left`}
</span>

<nav className="filter" data-cy="Filter">
<a
href="#/"
className={`filter__link ${filter === 'all' ? 'selected' : ''}`}
data-cy="FilterLinkAll"
onClick={event => {
event.preventDefault();
onFilterChange('all');
}}
>
All
</a>

<a
href="#/active"
className={`filter__link ${filter === 'active' ? 'selected' : ''}`}
data-cy="FilterLinkActive"
onClick={event => {
event.preventDefault();
onFilterChange('active');
}}
>
Active
</a>

<a
href="#/completed"
className={`filter__link ${filter === 'completed' ? 'selected' : ''}`}
data-cy="FilterLinkCompleted"
onClick={event => {
event.preventDefault();
onFilterChange('completed');
}}
>
Completed
</a>
</nav>

<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
disabled={!todos.some(todo => todo.completed)}
onClick={onClearCompleted}
>
Clear completed
</button>
</footer>
);
};
51 changes: 51 additions & 0 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-disable jsx-a11y/label-has-associated-control */
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';

type Props = {
allCompleted: boolean;
newTitle: string;
isAdding: boolean;
inputRef: React.RefObject<HTMLInputElement>;
onNewTitleChange: (value: string) => void;
onSubmit: (event: React.FormEvent) => void;
hasTodos: boolean;
onToggleAll: () => void;
};

export const Header: React.FC<Props> = ({
allCompleted,
newTitle,
isAdding,
inputRef,
onNewTitleChange,
onSubmit,
hasTodos,
onToggleAll,
}) => {
return (
<header className="todoapp__header">
{hasTodos && (
<button
type="button"
className={`todoapp__toggle-all ${allCompleted ? 'active' : ''}`}
data-cy="ToggleAllButton"
onClick={onToggleAll}
/>
)}

<form onSubmit={onSubmit}>
<input
ref={inputRef}
value={newTitle}
data-cy="NewTodoField"
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
disabled={isAdding}
onChange={event => onNewTitleChange(event.target.value)}
/>
</form>
</header>
);
};
Loading