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

solution #2615

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://imondok03.github.io/react_dynamic-list-of-todos/) and add it to the PR description.
51 changes: 46 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
/* 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 { getTodos } from './api';
import { Todo } from './types/Todo';

export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [isLoaded, setIsLoaded] = useState(false);
const [selectedTodo, setSelectedTodo] = useState<Todo | undefined>(undefined);
const [selectedStatus, setSelectedStatus] = useState('all');
const [query, setSelectedQuery] = useState('');

Choose a reason for hiding this comment

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

The state setter function here is named setSelectedQuery, but it should be setQuery to match the state variable name query. This follows the naming convention for state setter functions.


useEffect(() => {
getTodos().then(result => {
let filteredResult = result;

if (selectedStatus !== 'all') {
filteredResult = filteredResult.filter(todo =>
selectedStatus === 'active' ? !todo.completed : todo.completed,
);
}

if (query) {
filteredResult = filteredResult.filter(todo =>
todo.title.toLowerCase().includes(query.toLowerCase()),
);
}

setTodos(filteredResult);
setIsLoaded(true);
});
}, [selectedStatus, query]);

Choose a reason for hiding this comment

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

Using props to initialize state directly can lead to unexpected behavior. Consider using a function inside useState or handling it differently to avoid potential issues.


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

<div className="block">
<TodoFilter />
<TodoFilter
onSelect={setSelectedStatus}

Choose a reason for hiding this comment

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

The naming convention for event handler props suggests using onStatusSelect instead of onSelect to make it more descriptive and specific to the action.

onChange={setSelectedQuery}

Choose a reason for hiding this comment

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

The naming convention for event handler props suggests using onQueryChange instead of onChange to make it more descriptive and specific to the action.

/>
</div>

<div className="block">
<Loader />
<TodoList />
{!isLoaded ? (
<Loader />
) : (
<TodoList
todos={todos}
selectedTodo={selectedTodo}
onSelect={setSelectedTodo}

Choose a reason for hiding this comment

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

The naming convention for event handler props suggests using onTodoSelect instead of onSelect to make it more descriptive and specific to the action.

/>
)}
</div>
</div>
</div>
</div>

<TodoModal />
{selectedTodo && (
<TodoModal onClose={setSelectedTodo} todo={selectedTodo} />

Choose a reason for hiding this comment

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

The naming convention for event handler props suggests using onModalClose instead of onClose to make it more descriptive and specific to the action.

)}
</>
);
};
88 changes: 60 additions & 28 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,62 @@
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, { useState } 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 = {
onSelect: (value: string) => void;
onChange: (value: string) => void;
};

Choose a reason for hiding this comment

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

The naming convention for event handler props should follow the pattern 'on'. Consider renaming 'onChange' to something more descriptive like 'onInputChange' to clearly indicate what event it handles.


<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> = ({ onSelect, onChange }) => {
const [value, setValue] = useState('');
const inputHandler: React.ChangeEventHandler<HTMLInputElement> = event => {
onChange(event.target.value);
setValue(event.target.value);
};

const resetInputHandler = () => {
onChange('');
setValue('');
};

return (
<form className="field has-addons">
<p className="control">
<span className="select">
<select
data-cy="statusSelect"
onChange={event => onSelect(event.target.value)}
>

Choose a reason for hiding this comment

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

The inline function for the 'onChange' event of the select element could lead to performance issues as a new function is created on every render. Consider defining this function outside the JSX and passing it as a reference.

<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..."
value={value}
onChange={inputHandler}
/>
<span className="icon is-left">
<i className="fas fa-magnifying-glass" />
</span>

{value && (
<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={resetInputHandler}
/>
</span>
)}
</p>
</form>
);
};
165 changes: 71 additions & 94 deletions src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,100 +1,77 @@
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>
interface Props {
todos: Todo[];
selectedTodo: Todo | undefined;
onSelect: (todo: Todo) => 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> = ({
todos,
selectedTodo: { id: selectedId } = { selectedId: 0 },

Choose a reason for hiding this comment

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

Using destructuring with default values for selectedTodo can lead to unexpected behavior if selectedTodo is undefined. Consider handling this case differently to avoid potential issues.

onSelect,
}) => {
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(({ id, title, completed, userId }) => (
<tr
data-cy="todo"
className={`${id === selectedId && 'has-background-info-light'}`}

Choose a reason for hiding this comment

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

Consider using the classnames library for conditional class names instead of template literals for better readability and maintainability.

key={id}

Choose a reason for hiding this comment

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

Generating keys directly from the id prop is generally fine if id is unique and stable. However, ensure that id is indeed unique across all items to avoid potential issues.

>
<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={`
${completed ? 'has-text-success' : 'has-text-danger'}
`}
>
{title}
</p>
</td>
<td className="has-text-right is-vcentered">
<button
data-cy="selectButton"
className="button"
type="button"
onClick={() => onSelect({ id, title, completed, userId })}
>
<span className="icon">
<i
className={classNames(
'far',
selectedId === id ? 'fa-eye-slash' : 'fa-eye',
)}
/>
</span>
</button>
</td>
</tr>
))}
</tbody>
</table>
);
};
Loading
Loading