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 #2722

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

Develop #2722

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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# React dynamic list of TODOs

The goal of this task is to teach you:

- how to work with a not detailed task description;
- to learn the existing code before you start;
- to understand tests and why they fail;
Expand All @@ -12,7 +13,7 @@ You are given the markup for the `App`, `TodosList`, `TodoFilter`, `TodoModal`
and `Loader` components. Load data from the API and show it using the given components.

1. Load [the todos](https://mate-academy.github.io/react_dynamic-list-of-todos/api/todos.json) when the `App` is
loaded and show them using `TodoList` (check the code in the `api.ts`);
loaded and show them using `TodoList` (check the code in the `api.ts`);
1. Show the `Loader` when waiting any data from the server (check the `components` folder);
1. Check how the `wait` function is used in the `api.ts` to ensure that `Loader` works as expected;
1. When the `Show` button is clicked open the `TodoModal` with a selected `todo`;
Expand All @@ -21,12 +22,13 @@ loaded and show them using `TodoList` (check the code in the `api.ts`);
1. `x` button should close the modal;
1. The `select` should filter todos by the `completed` status: `all`, `completed` and `active`(not completed) todos;
1. Use the `input` in the `TodoFilter` to filter the `todos` by `title`;
- show the `x` button when the `query` is entered;
- the `x` button should clear the `query` and reset the todos;
- show the `x` button when the `query` is entered;
- the `x` button should clear the `query` and reset the todos;

## Instructions

- 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).
- Open one more terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_dynamic-list-of-todos/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://stanishevskyy.github.io/react_dynamic-list-of-todos/) and add it to the PR description.
78 changes: 73 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-len */
import React from 'react';
import React, { useEffect, useState } from 'react';
import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';

Expand All @@ -8,7 +8,64 @@ import { TodoFilter } from './components/TodoFilter';
import { TodoModal } from './components/TodoModal';
import { Loader } from './components/Loader';

import { Todo } from './types/Todo';
import { getTodos } from './api';

export const App: React.FC = () => {
//#region state for TodoFilter
const [filterTodo, setFilterTodo] = useState('');
const [filterBySelect, setFilterBySelect] = useState('all');
//#endregion

//#region state for TodoList
const [todosLists, setTodosLists] = useState<Todo[]>([]);
const [todo, setTodo] = useState<Todo | null>(null);
//#endregion

//#region state for Loader
const [loading, setLoading] = useState(false);
//#endregion

//#region useEffect for Todo and filtered Todo
useEffect(() => {
const fetchData = async () => {
setLoading(true);

try {
const data = await getTodos();

setTodosLists(data);
} finally {
setLoading(false);
}
};

fetchData();
}, []);

const filterTodos = () => {
let filteredTodo = todosLists.filter(todoList =>
todoList.title.toLowerCase().includes(filterTodo.trim().toLowerCase()),
);

if (filterBySelect === 'completed') {
filteredTodo = filteredTodo.filter(
todoList => todoList.completed === true,
);
}

if (filterBySelect === 'active') {
filteredTodo = filteredTodo.filter(
todoList => todoList.completed === false,
);
}

return filteredTodo;
};

const filteredTodos = filterTodos();
//#endregion

return (
<>
<div className="section">
Expand All @@ -17,18 +74,29 @@ export const App: React.FC = () => {
<h1 className="title">Todos:</h1>

<div className="block">
<TodoFilter />
<TodoFilter
filterTodo={filterTodo}
setFilterTodo={setFilterTodo}
filterBySelect={filterBySelect}
setFilterBySelect={setFilterBySelect}
/>
</div>

<div className="block">
<Loader />
<TodoList />
{loading && <Loader />}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the Loader component is displayed correctly while data is being fetched. This aligns with the task requirement to show the Loader when waiting for data from the server .

{!loading && todosLists && (
<TodoList
todosLists={filteredTodos}
todo={todo}
setTodo={setTodo}
/>
)}
</div>
</div>
</div>
</div>

<TodoModal />
{todo && <TodoModal todo={todo} setTodo={setTodo} />}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify that the TodoModal opens correctly when a todo is selected. This is crucial for meeting the task requirement of opening the TodoModal with a selected todo .

</>
);
};
80 changes: 53 additions & 27 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,56 @@
export const TodoFilter = () => (
<form className="field has-addons">
<p className="control">
<span className="select">
<select data-cy="statusSelect">
<option value="all">All</option>
<option value="active">Active</option>
<option value="completed">Completed</option>
</select>
</span>
</p>
import React from 'react';

<p className="control is-expanded has-icons-left has-icons-right">
<input
data-cy="searchInput"
type="text"
className="input"
placeholder="Search..."
/>
<span className="icon is-left">
<i className="fas fa-magnifying-glass" />
</span>
type Props = {
filterTodo: string;
filterBySelect: string;
setFilterTodo: (value: string) => void;
setFilterBySelect: (value: string) => void;
};

<span className="icon is-right" style={{ pointerEvents: 'all' }}>
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
<button data-cy="clearSearchButton" type="button" className="delete" />
</span>
</p>
</form>
export const TodoFilter: React.FC<Props> = React.memo(
({ filterTodo, filterBySelect, setFilterTodo, setFilterBySelect }) => (
<form className="field has-addons">
<p className="control">
<span className="select">
<select
data-cy="statusSelect"
value={filterBySelect}
onChange={event => setFilterBySelect(event.target.value)}
>
<option value="all">All</option>
<option value="active">Active</option>
<option value="completed">Completed</option>
</select>
</span>
</p>

<p className="control is-expanded has-icons-left has-icons-right">
<input
data-cy="searchInput"
type="text"
className="input"
placeholder="Search..."
onChange={event => setFilterTodo(event.target.value)}
value={filterTodo}
/>
<span className="icon is-left">
<i className="fas fa-magnifying-glass" />
</span>

{filterTodo && (
<span className="icon is-right" style={{ pointerEvents: 'all' }}>
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
<button
data-cy="clearSearchButton"
type="button"
className="delete"
onClick={() => setFilterTodo('')}
/>
</span>
)}
</p>
</form>
),
);

TodoFilter.displayName = 'TodoFilter';
165 changes: 72 additions & 93 deletions src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,100 +1,79 @@
import React from 'react';
import { Todo } from '../../types/Todo';
import classNames from 'classnames';

export const TodoList: React.FC = () => (
<table className="table is-narrow is-fullwidth">
<thead>
<tr>
<th>#</th>
<th>
<span className="icon">
<i className="fas fa-check" />
</span>
</th>
<th>Title</th>
<th> </th>
</tr>
</thead>
type Props = {
todosLists: Todo[];
todo: Todo | null;
setTodo: (value: Todo | null) => void;
};

<tbody>
<tr data-cy="todo" className="">
<td className="is-vcentered">1</td>
<td className="is-vcentered" />
<td className="is-vcentered is-expanded">
<p className="has-text-danger">delectus aut autem</p>
</td>
<td className="has-text-right is-vcentered">
<button data-cy="selectButton" className="button" type="button">
export const TodoList: React.FC<Props> = React.memo(
({ todosLists, todo, setTodo }) => (
<table className="table is-narrow is-fullwidth">
<thead>
<tr>
<th>#</th>
<th>
<span className="icon">
<i className="far fa-eye" />
<i className="fas fa-check" />
</span>
</button>
</td>
</tr>
<tr data-cy="todo" className="has-background-info-light">
<td className="is-vcentered">2</td>
<td className="is-vcentered" />
<td className="is-vcentered is-expanded">
<p className="has-text-danger">quis ut nam facilis et officia qui</p>
</td>
<td className="has-text-right is-vcentered">
<button data-cy="selectButton" className="button" type="button">
<span className="icon">
<i className="far fa-eye-slash" />
</span>
</button>
</td>
</tr>

<tr data-cy="todo" className="">
<td className="is-vcentered">1</td>
<td className="is-vcentered" />
<td className="is-vcentered is-expanded">
<p className="has-text-danger">delectus aut autem</p>
</td>
<td className="has-text-right is-vcentered">
<button data-cy="selectButton" className="button" type="button">
<span className="icon">
<i className="far fa-eye" />
</span>
</button>
</td>
</tr>
</th>
<th>Title</th>
<th> </th>
</tr>
</thead>

<tr data-cy="todo" className="">
<td className="is-vcentered">6</td>
<td className="is-vcentered" />
<td className="is-vcentered is-expanded">
<p className="has-text-danger">
qui ullam ratione quibusdam voluptatem quia omnis
</p>
</td>
<td className="has-text-right is-vcentered">
<button data-cy="selectButton" className="button" type="button">
<span className="icon">
<i className="far fa-eye" />
</span>
</button>
</td>
</tr>

<tr data-cy="todo" className="">
<td className="is-vcentered">8</td>
<td className="is-vcentered">
<span className="icon" data-cy="iconCompleted">
<i className="fas fa-check" />
</span>
</td>
<td className="is-vcentered is-expanded">
<p className="has-text-success">quo adipisci enim quam ut ab</p>
</td>
<td className="has-text-right is-vcentered">
<button data-cy="selectButton" className="button" type="button">
<span className="icon">
<i className="far fa-eye" />
</span>
</button>
</td>
</tr>
</tbody>
</table>
<tbody>
{todosLists.map(todoList => (
<tr
data-cy="todo"
className={
todo && todo.id === todoList.id ? 'has-background-info-light' : ''
}
key={todoList.id}
>
<td className="is-vcentered">{todoList.id}</td>
<td className="is-vcentered">
{todoList.completed && (
<span className="icon" data-cy="iconCompleted">
<i className="fas fa-check" />
</span>
)}
</td>
<td className="is-vcentered is-expanded">
<p
className={classNames({
'has-text-success': todoList.completed,
'has-text-danger': !todoList.completed,
})}
>
{todoList.title}
</p>
</td>
<td className="has-text-right is-vcentered">
<button
data-cy="selectButton"
className="button"
type="button"
onClick={() => setTodo(todoList)}
>
{todo && todo.id === todoList.id ? (
<span className="icon">
<i className="far fa-eye-slash" />
</span>
) : (
<span className="icon">
<i className="far fa-eye" />
</span>
)}
</button>
</td>
</tr>
))}
</tbody>
</table>
),
);

TodoList.displayName = 'TodoList';
Loading
Loading