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

dynamic list of todos #2758

Open
wants to merge 4 commits 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
56 changes: 51 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
/* eslint-disable max-len */
import React from 'react';
import React, { useEffect, useMemo, 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 { getTodos } from './api';
import { Todo } from './types/Todo';
import { FilterStatus } from './types/FilterStatus';

export const App: React.FC = () => {
const [todosFromAPI, setTodosFromAPI] = useState<Todo[]>([]);
const [loading, setLoading] = useState(false);
const [selectedId, setSelectedId] = useState<number | null>(null);

Choose a reason for hiding this comment

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

Good job on using null as the initial value for selectedId. This helps avoid potential issues with a todo having an ID of 0.

const [query, setQuery] = useState('');
const [filterStatus, setFilterStatus] = useState(FilterStatus.All);

useEffect(() => {
setLoading(true);
getTodos()
.then(setTodosFromAPI)
.finally(() => setLoading(false));
}, []);

Choose a reason for hiding this comment

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

The dependency array for useEffect is empty, which means this effect will only run once when the component mounts. Ensure this is the intended behavior, as it will not re-fetch todos if any dependencies change.


const filteredTodos = useMemo(() => {
return todosFromAPI.filter(todo => {
const matchesStatus =
filterStatus === FilterStatus.All ||
(filterStatus === FilterStatus.Active && !todo.completed) ||
(filterStatus === FilterStatus.Completed && todo.completed);

const matchesQuery = todo.title
.toLowerCase()
.includes(query.toLowerCase());

return matchesStatus && matchesQuery;
});
}, [todosFromAPI, filterStatus, query]);

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

<div className="block">
<TodoFilter />
<TodoFilter
status={filterStatus}
setStatus={setFilterStatus}
query={query}
setQuery={setQuery}
/>
</div>

<div className="block">
<Loader />
<TodoList />
{loading && <Loader />}
<TodoList
todos={filteredTodos}
onSelect={setSelectedId}
selectedId={selectedId}
/>
</div>
</div>
</div>
</div>

<TodoModal />
{selectedId && (

Choose a reason for hiding this comment

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

Using selectedId directly in a boolean context may lead to unexpected behavior if a todo with an ID of 0 exists. Consider checking for selectedId !== 0 or using a more explicit condition.

Choose a reason for hiding this comment

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

Ensure that the conditional rendering of TodoModal aligns with your application's logic. Currently, it renders when selectedId is truthy, which means it won't render if selectedId is 0 or null. This is fine if null is intended to represent no selection.

<TodoModal
id={selectedId}
todos={filteredTodos}
onClose={setSelectedId}
/>
)}
</>
);
};
58 changes: 58 additions & 0 deletions src/components/PostItem/PostItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import classNames from 'classnames';
import { Todo } from '../../types/Todo';
import React from 'react';

type Props = {
todo: Todo;
selectedId: number | null;
onSelect: (id: number) => void;
};

export const PostItem: React.FC<Props> = ({ todo, selectedId, onSelect }) => {
const { id, title, completed } = todo;

return (
<tr
data-cy="todo"
className={classNames({
'has-background-info-light': selectedId === id,
})}
>
<td className="is-vcentered">{id}</td>
<td className="is-vcentered">
{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': completed,
'has-text-danger': !completed,
})}
>
{title}
</p>
</td>
<td className="has-text-right is-vcentered">
<button
data-cy="selectButton"
className="button"
type="button"
onClick={() => onSelect(id)}
>
<span className="icon">
<i
className={classNames('far', {
'fa-eye': selectedId !== id,
'fa-eye-slash': selectedId === id,
})}
/>
</span>
</button>
</td>
</tr>
);
};
104 changes: 74 additions & 30 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,74 @@
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>

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

<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>
);
import React from 'react';
import { FilterStatus } from '../../types/FilterStatus';

type Props = {
query: string;
setQuery: (query: string) => void;
status: FilterStatus;
setStatus: (status: FilterStatus) => void;
};

export const TodoFilter: React.FC<Props> = ({
query,
setQuery,
status,
setStatus,
}) => {
function handleTitleChange(event: React.ChangeEvent<HTMLInputElement>) {
const newQuery = event.target.value;

setQuery(newQuery);
}

function handleFilterChange(event: React.ChangeEvent<HTMLSelectElement>) {
setStatus(event.target.value as FilterStatus);
}

function clearInput() {
setQuery('');
}

return (
<form className="field has-addons">
<p className="control">
<span className="select">
<select
data-cy="statusSelect"
value={status}
onChange={handleFilterChange}
>
<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"
value={query}
className="input"
placeholder="Search..."
onChange={handleTitleChange}
/>
<span className="icon is-left">
<i className="fas fa-magnifying-glass" />
</span>

<span className="icon is-right" style={{ pointerEvents: 'all' }}>
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
{query.length > 0 && (
<button
data-cy="clearSearchButton"
type="button"
className="delete"
onClick={clearInput}
/>
)}
</span>
</p>
</form>
);
};
127 changes: 33 additions & 94 deletions src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,100 +1,39 @@
import React from 'react';
import { Todo } from '../../types/Todo';
import { PostItem } from '../PostItem/PostItem';

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 = {
todos: Todo[];
onSelect: (id: number) => void;
selectedId: number | null;
};

<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> = ({ todos, onSelect, selectedId }) => {
return (
<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>
{todos.map(todo => (
<PostItem
todo={todo}
onSelect={onSelect}
selectedId={selectedId}
key={todo.id}
/>
))}
</tbody>
</table>
);
};
Loading