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

add task solution #2744

Open
wants to merge 1 commit 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 @@ -29,4 +29,4 @@ loaded and show them using `TodoList` (check the code in the `api.ts`);
- 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://Dorosh90.github.io/react_dynamic-list-of-todos/) and add it to the PR description.
63 changes: 54 additions & 9 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
/* 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';

import { TodoList } from './components/TodoList';
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 = () => {
const [loader, setLoader] = useState(true);
const [allTodos, setAllTodos] = useState<Todo[]>([]);
const [filteredTodos, setFilteredTodos] = useState<Todo[]>([]);
const [filter, setFilter] = useState('');
const [searchQuery, setSearchQuery] = useState('');

useEffect(() => {
getTodos()
.then(todos => {
setAllTodos(todos);
setFilteredTodos(todos);
})
.finally(() => setLoader(false));
}, []);

useEffect(() => {
const filterTodosList = () => {
let result = allTodos;

switch (filter) {
case 'active':
result = allTodos.filter(todo => !todo.completed);
break;

case 'completed':
result = allTodos.filter(todo => todo.completed);
break;

default:
break;
}
Comment on lines +32 to +43

Choose a reason for hiding this comment

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

The filter switch statement should include a case for 'all' to ensure that all todos are displayed when this filter is selected. Currently, the default case does not handle this explicitly, which might lead to unexpected behavior.


if (searchQuery) {
result = result.filter(todo =>
todo.title.toLowerCase().includes(searchQuery.toLowerCase()),
);
}

return result;
};

setFilteredTodos(filterTodosList());
}, [filter, allTodos, searchQuery]);

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

<div className="block">
<TodoFilter />
<TodoFilter
currentFilter={filter}
setFilter={setFilter}
query={searchQuery}
setQuery={setSearchQuery}
/>
</div>

<div className="block">
<Loader />
<TodoList />
</div>
{loader ? <Loader /> : <TodoList todos={filteredTodos} />}

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. According to the task description, the Loader should be shown when waiting for any data from the server.

</div>
</div>
</div>

<TodoModal />
</>
);
};
83 changes: 55 additions & 28 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,57 @@
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>
interface Props {
currentFilter: string;
setFilter: (filter: string) => void;
query: string;
setQuery: (q: string) => void;
}

<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>
export const TodoFilter: React.FC<Props> = ({
currentFilter,
setFilter,
query,
setQuery,
}) => {
return (
<form className="field has-addons">
<p className="control">
<span className="select">
<select
value={currentFilter}
data-cy="statusSelect"
onChange={e => setFilter(e.currentTarget.value)}
>
<option value="all">All</option>
<option value="active">Active</option>
<option value="completed">Completed</option>
</select>
</span>
</p>

<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>
);
<p className="control is-expanded has-icons-left has-icons-right">
<input
value={query}
data-cy="searchInput"
type="text"
className="input"
placeholder="Search..."
onChange={event => setQuery(event.currentTarget.value)}
/>
<span className="icon is-left">
<i className="fas fa-magnifying-glass" />
</span>

{query.length > 0 && (
<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={() => setQuery('')}
/>
</span>
)}
</p>
</form>
);
};
175 changes: 81 additions & 94 deletions src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,100 +1,87 @@
import React from 'react';
import { useState } from 'react';
import { Todo } from '../../types/Todo';
import classNames from 'classnames';
import { TodoModal } from '../TodoModal';

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>
interface Props {
todos: Todo[];
}

<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">
<span className="icon">
<i className="far fa-eye" />
</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>
export const TodoList: React.FC<Props> = ({ todos }) => {
const [isTodoModal, setIsTodoModal] = useState<null | number>(null);

<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>
const handleCloseModal = () => {
setIsTodoModal(null);
};

<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>
return (
<div className="block">
<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>

<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>
{todos.map(todo => {
return (
<tr data-cy="todo" className="" key={todo.id}>
<td className="is-vcentered">{todo.id}</td>
<td className="is-vcentered">
{todo.completed && (
<span className="icon" data-cy="iconCompleted">
<i className="fas fa-check"></i>
</span>
)}
</td>
<td className="is-vcentered is-expanded">
<p
className={classNames({
'has-text-success': todo.completed,
'has-text-danger': !todo.completed,
})}
>
{todo.title}
</p>
</td>
<td className="has-text-right is-vcentered">
<button
data-cy="selectButton"
className="button"
type="button"
onClick={() => setIsTodoModal(todo.id)}
>
<span className="icon">
<i
className={classNames('far', {
'fa-eye': todo.id !== isTodoModal,
' fa-eye-slash': todo.id === isTodoModal,
})}
></i>
</span>
</button>
</td>
</tr>
);
})}
</tbody>
</table>
{isTodoModal && (
<TodoModal
todoId={isTodoModal}
todos={todos}
onClose={handleCloseModal}
/>
)}
</div>
);
};
Loading
Loading