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

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

Develop #2697

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
36 changes: 31 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
/* 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';

export const App: React.FC = () => {
const [isLoading, setIsLoading] = useState(true);
const [isModalVisible, setIsModalVisible] = useState(false);
const [todos, setTodos] = useState<Todo[] | null>(null);
const [todo, setTodo] = useState<Todo | null>(null);

useEffect(() => {
setTimeout(() => {
setIsLoading(false);
}, 1000);
}, []);
Comment on lines +18 to +22

Choose a reason for hiding this comment

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

The useEffect hook is set to run only once with an empty dependency array. Ensure this is the intended behavior, as it will not react to changes in any state or props.


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

<div className="block">
<TodoFilter />
<TodoFilter setTodos={setTodos} />
</div>

<div className="block">
<Loader />
<TodoList />
{isLoading ? (
<Loader />
) : (
<TodoList
setModalVisible={setIsModalVisible}
setSelectedTodo={setTodo}
todos={todos}
selectedTodo={todo}
Comment on lines +42 to +43

Choose a reason for hiding this comment

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

Consider using optional chaining when accessing properties of potentially null or undefined objects, e.g., todos?.map(...) and selectedTodo?.id === todo.id && 'has-background-info-light'.

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

<TodoModal />
{isModalVisible && (
<TodoModal
setVisible={setIsModalVisible}
todo={todo}
setTodo={setTodo}
/>
)}
</>
);
};
108 changes: 78 additions & 30 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,78 @@
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, { useEffect, useState } from 'react';
import { Todo } from '../../types/Todo';
import { getTodos } from '../../api';

type Props = {
setTodos: React.Dispatch<React.SetStateAction<Todo[] | null>>;
};

export const TodoFilter: React.FC<Props> = ({ setTodos }) => {
const [qwerty, setQwerty] = useState('');

Choose a reason for hiding this comment

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

The variable name qwerty is not descriptive. Consider renaming it to something more meaningful, such as searchQuery, to better convey its purpose.

Choose a reason for hiding this comment

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

Consider renaming qwerty to searchQuery to better reflect its purpose and improve code readability.

const [status, setStatus] = useState('');

useEffect(() => {
getTodos().then(serverTodos => {
let currentTodos: Todo[] | null = serverTodos.filter(todo =>
todo.title.toLocaleLowerCase().includes(qwerty.toLocaleLowerCase()),
);
Comment on lines +15 to +17

Choose a reason for hiding this comment

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

Consider using optional chaining when accessing properties of potentially null or undefined objects, e.g., serverTodos?.filter(...).


switch (status) {
case 'active':
currentTodos = currentTodos.filter(todo => !todo.completed);
break;
case 'completed':
currentTodos = currentTodos.filter(todo => todo.completed);
break;
}

setTodos(currentTodos);
});
}, [qwerty, status]);

Check warning on line 30 in src/components/TodoFilter/TodoFilter.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

React Hook useEffect has a missing dependency: 'setTodos'. Either include it or remove the dependency array. If 'setTodos' changes too often, find the parent component that defines it and wrap that definition in useCallback

return (
<form className="field has-addons">
<p className="control">
<span className="select">
<select
data-cy="statusSelect"
onChange={e => setStatus(e.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..."
value={qwerty}
onChange={e => {
setQwerty(e.target.value);
}}
/>
<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 */}
{qwerty && (
<button
data-cy="clearSearchButton"
type="button"
className="delete"
onClick={() => {
setQwerty('');
}}
/>
)}
</span>
</p>
</form>
);
};
150 changes: 68 additions & 82 deletions src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,100 +1,86 @@
import React from 'react';
import { Todo } from '../../types/Todo';

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[] | null;
setModalVisible: React.Dispatch<React.SetStateAction<boolean>>;
setSelectedTodo: React.Dispatch<React.SetStateAction<Todo | null>>;
selectedTodo: Todo | 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">
<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> = ({
setModalVisible,
todos,
setSelectedTodo,
selectedTodo,
}) => {
// console.log(todos);

<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" />
function mapTodos() {
return todos?.map(todo => (
<tr
data-cy="todo"
className={
selectedTodo?.id === todo.id ? 'has-background-info-light' : undefined
}
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" />
</span>
</button>
)}
</td>
</tr>

<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
className={todo.completed ? 'has-text-success' : 'has-text-danger'}
>
{todo.title}
</p>
</td>
<td className="has-text-right is-vcentered">
<button data-cy="selectButton" className="button" type="button">
<button
data-cy="selectButton"
className="button"
type="button"
onClick={() => {
setModalVisible(true);
setSelectedTodo(todo);
}}
>
<span className="icon">
<i className="far fa-eye" />
<i
className={
selectedTodo && selectedTodo.id === todo.id
? 'far fa-eye-slash'
: '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">
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>
</tbody>
</table>
);
</th>
<th>Title</th>
<th> </th>
</tr>
</thead>

<tbody>{mapTodos()}</tbody>
</table>
);
};
Loading
Loading