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 @@ -70,4 +70,4 @@ Filter todos by status `All` / `Active` / `Completed`:
- Install Prettier Extention and use this [VSCode settings](https://mate-academy.github.io/fe-program/tools/vscode/settings.json) to enable format on save.
- 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-loading-todos/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://droopy-bit.github.io/react_todo-app-loading-todos/) 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": "^2.1.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
217 changes: 57 additions & 160 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
/* 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 { USER_ID, getTodos } from './api/todos';
import { Todo } from './types/Todo';
import { ErrorNotification } from './components/ErrorNotification';
import { TodoList } from './components/TodoList';
import { FilterStatus } from './types/FilterStatus';
import { TodoFilter } from './components/TodoFilter';

enum ErrorMessage {
Load = 'Unable to load todos',
}

export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [errorMessage, setErrorMessage] = useState('');
const [filter, setFilter] = useState<FilterStatus>(FilterStatus.All);

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

const visibleTodos = todos.filter(todo => {
if (filter === FilterStatus.Active) {
return !todo.completed;
} else if (filter === FilterStatus.Completed) {
return todo.completed;
}

return true;
});

const activeTodosCount = todos.filter(todo => !todo.completed).length;

if (!USER_ID) {
return <UserWarning />;
}
Expand Down Expand Up @@ -32,173 +66,36 @@ export const App: React.FC = () => {
/>
</form>
</header>

<section className="todoapp__main" data-cy="TodoList">
{/* This is a completed todo */}
<div data-cy="Todo" className="todo completed">
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
checked
/>
</label>

<span data-cy="TodoTitle" className="todo__title">
Completed Todo
</span>

{/* Remove button appears only on hover */}
<button type="button" className="todo__remove" data-cy="TodoDelete">
×
</button>

{/* overlay will cover the todo while it is being deleted or updated */}
<div data-cy="TodoLoader" className="modal overlay">
<div className="modal-background has-background-white-ter" />
<div className="loader" />
</div>
</div>

{/* This todo is an active todo */}
<div data-cy="Todo" className="todo">
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
/>
</label>

<span data-cy="TodoTitle" className="todo__title">
Not Completed Todo
</span>
<button type="button" className="todo__remove" data-cy="TodoDelete">
×
</button>

<div data-cy="TodoLoader" className="modal overlay">
<div className="modal-background has-background-white-ter" />
<div className="loader" />
</div>
</div>

{/* This todo is being edited */}
<div data-cy="Todo" className="todo">
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
/>
</label>

{/* This form is shown instead of the title and remove button */}
<form>
<input
data-cy="TodoTitleField"
type="text"
className="todo__title-field"
placeholder="Empty todo will be deleted"
value="Todo is being edited now"
/>
</form>

<div data-cy="TodoLoader" className="modal overlay">
<div className="modal-background has-background-white-ter" />
<div className="loader" />
</div>
</div>

{/* This todo is in loadind state */}
<div data-cy="Todo" className="todo">
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
/>
</label>

<span data-cy="TodoTitle" className="todo__title">
Todo is being saved now
</span>

<button type="button" className="todo__remove" data-cy="TodoDelete">
×
</button>

{/* 'is-active' class puts this modal on top of the todo */}
<div data-cy="TodoLoader" className="modal overlay is-active">
<div className="modal-background has-background-white-ter" />
<div className="loader" />
</div>
</div>
</section>
{todos.length > 0 && <TodoList todos={visibleTodos} />}

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

{/* Active link should have the 'selected' class */}
<nav className="filter" data-cy="Filter">
<a
href="#/"
className="filter__link selected"
data-cy="FilterLinkAll"
>
All
</a>
{todos.length > 0 && (
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
{activeTodosCount} items left
</span>

<a
href="#/active"
className="filter__link"
data-cy="FilterLinkActive"
>
Active
</a>
{/* Active link should have the 'selected' class */}
<TodoFilter filter={filter} onFilterChange={setFilter} />

<a
href="#/completed"
className="filter__link"
data-cy="FilterLinkCompleted"
{/* this button should be disabled if there are no completed todos */}
<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
>
Completed
</a>
</nav>

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

{/* DON'T use conditional rendering to hide the notification */}
<ErrorNotification
message={errorMessage}
onClose={() => setErrorMessage('')}
/>
{/* Add the 'hidden' class to hide the message smoothly */}
<div
data-cy="ErrorNotification"
className="notification is-danger is-light has-text-weight-normal"
>
<button data-cy="HideErrorButton" type="button" className="delete" />
{/* show only one message at a time */}
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>
);
};
2 changes: 1 addition & 1 deletion src/api/todos.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Todo } from '../types/Todo';
import { client } from '../utils/fetchClient';

export const USER_ID = 0;
export const USER_ID = 4354;

export const getTodos = () => {
return client.get<Todo[]>(`/todos?userId=${USER_ID}`);
Expand Down
24 changes: 24 additions & 0 deletions src/components/ErrorNotification.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
type Props = {
message: string;
onClose: () => void;
};
export const ErrorNotification: React.FC<Props> = ({ message, onClose }) => (
<div
data-cy="ErrorNotification"
className={
message
? 'notification is-danger is-light has-text-weight-normal'
: 'notification is-danger is-light has-text-weight-normal hidden'
}
>
<button
data-cy="HideErrorButton"
type="button"
className="delete"
onClick={onClose}
/>
{/* show only one message at a time */}
{message}
</div>
);
27 changes: 27 additions & 0 deletions src/components/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import { FilterStatus } from '../types/FilterStatus';
type Props = {
filter: FilterStatus;
onFilterChange: (status: FilterStatus) => void;
};
export const TodoFilter = ({ filter, onFilterChange }: Props) => (
<nav className="filter" data-cy="Filter">
{Object.values(FilterStatus).map(status => {
const capitalized = status[0].toUpperCase() + status.slice(1);

return (
<a
key={status}
onClick={() => onFilterChange(status)}
href={status === FilterStatus.All ? '#/' : `#/${status}`}
className={
filter === status ? 'filter__link selected' : 'filter__link'
}
data-cy={`FilterLink${capitalized}`}
>
{capitalized}
</a>
);
})}
</nav>
);
38 changes: 38 additions & 0 deletions src/components/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-disable jsx-a11y/label-has-associated-control */
import React from 'react';
import { Todo } from '../types/Todo';
type Props = {
todos: Todo[];
};

export const TodoList: React.FC<Props> = ({ todos }) => (
<section className="todoapp__main" data-cy="TodoList">
{todos.map(todo => (
<div
data-cy="Todo"
className={todo.completed ? 'todo completed' : 'todo'}
key={todo.id}
>
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
checked={todo.completed}
/>
</label>
<span data-cy="TodoTitle" className="todo__title">
{todo.title}
</span>
<button type="button" className="todo__remove" data-cy="TodoDelete">
×
</button>

<div data-cy="TodoLoader" className="modal overlay">
<div className="modal-background has-background-white-ter" />
<div className="loader" />
</div>
</div>
))}
</section>
);
5 changes: 5 additions & 0 deletions src/types/FilterStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum FilterStatus {
All = 'all',
Active = 'active',
Completed = 'completed',
}
Loading