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

Develop #1534

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Develop #1534

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://VikaChereushenko.github.io/react_todo-app-with-api/) and add it to the PR description.
9 changes: 5 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": "^1.9.12",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
78 changes: 64 additions & 14 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,76 @@
/* 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 React, { useState, useEffect } from 'react';

import { UserWarning } from './UserWarning';
import { USER_ID } from './api/todos';
import { Header } from './components/Header/Header';
import { TodoList } from './components/TodoList/TodoList';
import { TempTodo } from './components/TempTodo/TempTodo';
import { Footer } from './components/Footer/Fotter';
import { Error } from './components/Error/Erros';
import { getTodos } from './api/todos';
import { filterTodos } from './components/Helpers/Helpers';

const USER_ID = 0;
import { Todo } from './types/Todo';
import { TodoStatus } from './types/Status';

export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [errorMessage, setErrorMessage] = useState('');
const [status, setStatus] = useState<TodoStatus>(TodoStatus.all);
const [tempTodo, setTempTodo] = useState<Todo | null>(null);
const [processedIds, setprocessedIds] = useState<number[]>([]);
const noTodos = todos.length === 0;
const filteredTodos = filterTodos(todos, status);

useEffect(() => {
getTodos()
.then(setTodos)
.catch(() => setErrorMessage('Unable to load todos'));
}, []);

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">
<Header
todoList={todos}
onError={setErrorMessage}
updateTodoList={setTodos}
updateTempTodo={setTempTodo}
updateProcessedIds={setprocessedIds}
/>

<TodoList
filteredTodos={filteredTodos}
updateTodolist={setTodos}
onError={setErrorMessage}
todoList={todos}
processedIds={processedIds}
updateProcessedIds={setprocessedIds}
/>

{tempTodo && <TempTodo todo={tempTodo} />}

{!noTodos && (
<Footer
todoList={todos}
updateTodolist={setTodos}
status={status}
onStatusChange={(value: TodoStatus) => setStatus(value)}
updateProcessedIds={setprocessedIds}
onError={setErrorMessage}
/>
)}
</div>

<Error errorMessage={errorMessage} hideError={setErrorMessage} />
</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 = 2042;

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

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

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

export const updateTodo = (todoId: number, todoData: Partial<Todo>) => {
return client.patch(`/todos/${todoId}`, todoData);
};

// Add more methods here
34 changes: 34 additions & 0 deletions src/components/Error/Erros.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, { useEffect } from 'react';
import classNames from 'classnames';

type Props = {
errorMessage: string;
hideError: (errorMessage: string) => void;
};

export const Error: React.FC<Props> = ({ errorMessage, hideError }) => {
useEffect(() => {
setTimeout(() => hideError(''), 3000);
});

return (
<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={() => hideError('')}
/>
{errorMessage}
</div>
);
};
Empty file added src/components/Error/index.tsx
Empty file.
96 changes: 96 additions & 0 deletions src/components/Footer/Fotter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import React, { useCallback } from 'react';
import { useMemo } from 'react';
import classNames from 'classnames';

import { deleteTodo } from '../../api/todos';
import { capitalizeFirstLetter, filterOptions } from '../Helpers/Helpers';

import { Todo } from '../../types/Todo';
import { TodoStatus } from '../../types/Status';

type Props = {
todoList: Todo[];
updateTodolist: React.Dispatch<React.SetStateAction<Todo[]>>;
status: TodoStatus;
onStatusChange: (status: TodoStatus) => void;
updateProcessedIds: React.Dispatch<React.SetStateAction<number[]>>;
onError: React.Dispatch<React.SetStateAction<string>>;
};

export const Footer: React.FC<Props> = ({
todoList,
updateTodolist,
status,
onStatusChange,
updateProcessedIds,
onError,
}) => {
const activeTodos = useMemo(
() => todoList.filter(todo => !todo.completed),
[todoList],
);
const isAnyCompleted = useMemo(
() => todoList.some(todo => todo.completed),
[todoList],
);

const handleDeleteCompletedTodos = useCallback(() => {
todoList.forEach(todo => {
if (todo.completed) {
updateProcessedIds(existing => [...existing, todo.id]);
deleteTodo(todo.id)
.then(() =>
updateTodolist(existing =>
existing.filter(current => current.id !== todo.id),
),
)
.catch(() => onError('Unable to delete a todo'))
.finally(() => {
updateProcessedIds(existing =>
existing.filter(id => id !== todo.id),
);
});
}
});
}, [todoList]);

Check warning on line 55 in src/components/Footer/Fotter.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

React Hook useCallback has missing dependencies: 'onError', 'updateProcessedIds', and 'updateTodolist'. Either include them or remove the dependency array. If 'updateProcessedIds' changes too often, find the parent component that defines it and wrap that definition in useCallback

return (
<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">
{filterOptions.map(option => {
const formattedOption = capitalizeFirstLetter(option);

return (
<a
href={option === 'all' ? '#/' : `#/${option}`}
key={option}
className={classNames('filter__link', {
selected: status === option,
})}
data-cy={`FilterLink${formattedOption}`}
onClick={() => onStatusChange(option)}
>
{formattedOption}
</a>
);
})}
</nav>

{/* this button should be disabled if there are no completed todos */}
<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
disabled={!isAnyCompleted}
onClick={handleDeleteCompletedTodos}
>
Clear completed
</button>
</footer>
);
};
Empty file added src/components/Footer/index.tsx
Empty file.
Loading
Loading