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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
90 changes: 84 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,77 @@
/* 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, getUser } from './api';
import { User } from './types/User';

export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [loading, setLoading] = useState(false);

Choose a reason for hiding this comment

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

Boolean variables should start from is or has

const [selectedTodo, setSelectedTodo] = useState<Todo | null>(null);
const [user, setUser] = useState<User | null>(null);
const [filteredField, setFilteredField] = useState<string>('All');

Choose a reason for hiding this comment

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

all/active/completed are magic values

const [query, setQuery] = useState<string>('');

const preparedTodos = todos.filter(todo => {
if (filteredField === 'completed') {
return todo.completed === true;
}

if (filteredField === 'active') {
return todo.completed === false;
}

Choose a reason for hiding this comment

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

Recommendation:
Consider using Enum for limited options eg.
Enum Field {
All = 'all',
Active = 'active',
Completed = 'completed',
}
And replace 'All' with Field.All

Choose a reason for hiding this comment

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

Recommendation: Consider using Enum for limited options eg. Enum Field { All = 'all', Active = 'active', Completed = 'completed', } And replace 'All' with Field.All

Good point
And use switch case instead


return true;
});

const filteredTodos = preparedTodos.filter(todo => {
if (query) {
return todo.title.toLowerCase().includes(query.toLowerCase());

Choose a reason for hiding this comment

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

If you don't have query you don't need to filter at all

}

return true;
});

useEffect(() => {
setLoading(true);

getTodos()
.then(setTodos)
.finally(() => setLoading(false));
}, []);

const handelTodo = (todo: Todo) => {

Choose a reason for hiding this comment

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

There is a typo in the function name handelTodo. It should be handleTodo to maintain consistency and avoid confusion.

Choose a reason for hiding this comment

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

Suggested change
const handelTodo = (todo: Todo) => {
const handleTodo = (todo: Todo) => {

typo in word

setLoading(true);
setSelectedTodo(todo);

getUser(todo.userId)
.then(setUser)
.finally(() => setLoading(false));
};

const handleChangeStatus = (newStatus: string) => {
setFilteredField(newStatus);
};

const handleChangeQuery = (event: React.ChangeEvent<HTMLInputElement>) => {
setQuery(event.target.value);
};

const onReset = () => {
setQuery('');
setFilteredField('All');
setSelectedTodo(null);
setUser(null);
};

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

<div className="block">
<TodoFilter />
<TodoFilter
onChangeStatus={handleChangeStatus}
query={query}
onChangeQuery={handleChangeQuery}
onReset={onReset}
/>
</div>

<div className="block">
<Loader />
<TodoList />
{loading && <Loader />}
<TodoList
todos={filteredTodos}
showSelectedTodo={handelTodo}

Choose a reason for hiding this comment

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

The function handelTodo is used here, but it contains a typo. It should be handleTodo to match the corrected function name.

selectedTodo={selectedTodo}
/>
</div>
</div>
</div>
</div>

<TodoModal />
{selectedTodo && (
<TodoModal
user={user}
todo={selectedTodo}
loading={loading}

Choose a reason for hiding this comment

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

Recommendation: The TodoModal component should have a personal loading state. When you click on the Todo button, the loadings duplicate.

Choose a reason for hiding this comment

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

Recommendation: The TodoModal component should have a personal loading state. When you click on the Todo button, the loadings duplicate.

Good point

onReset={onReset}
/>
)}
</>
);
};
54 changes: 54 additions & 0 deletions src/components/Todo/Todo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import classNames from 'classnames';
import { Todo } from '../../types/Todo';
import React from 'react';

type Props = {
todo: Todo;
selectedTodo: Todo | null;
showSelectedTodo: (todo: Todo) => void;
};

export const TodoItem: React.FC<Props> = props => {
const { todo, showSelectedTodo, selectedTodo } = props;

return (
<tr data-cy="todo" className="">
<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>
)}
</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={() => showSelectedTodo(todo)}
>
{todo.id === selectedTodo?.id ? (
<span className="icon">
<i className="far fa-eye-slash" />
</span>
) : (
<span className="icon">
<i className="far fa-eye" />

Choose a reason for hiding this comment

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

It's almost the same in both cases, only one class is different

</span>
)}
</button>
</td>
</tr>
);
};
37 changes: 31 additions & 6 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
export const TodoFilter = () => (
import React from 'react';

type Props = {
query: string;
onChangeStatus: (newStatus: string) => void;
onChangeQuery: (event: React.ChangeEvent<HTMLInputElement>) => void;
onReset: () => void;
};

export const TodoFilter: React.FC<Props> = ({
query,
onChangeStatus,
onChangeQuery,
onReset,
}) => (
<form className="field has-addons">
<p className="control">
<span className="select">
<select data-cy="statusSelect">
<select
data-cy="statusSelect"
onChange={event => onChangeStatus(event.target.value)}
>
<option value="all">All</option>
<option value="active">Active</option>
<option value="completed">Completed</option>
Expand All @@ -16,15 +33,23 @@ export const TodoFilter = () => (
type="text"
className="input"
placeholder="Search..."
value={query}
onChange={onChangeQuery}
/>
<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>
{query && (
<span className="icon is-right" style={{ pointerEvents: 'all' }}>
<button

Choose a reason for hiding this comment

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

Avoid inline styles, use classes instead

data-cy="clearSearchButton"
type="button"
className="delete"
onClick={onReset}
/>
</span>
)}
</p>
</form>
);
127 changes: 34 additions & 93 deletions src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,100 +1,41 @@
import React from 'react';
import { Todo } from '../../types/Todo';
import { TodoItem } from '../Todo/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[];
selectedTodo: Todo | null;
showSelectedTodo: (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">
<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> = props => {
const { todos, showSelectedTodo, selectedTodo } = props;

<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">
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>
</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 => (
<TodoItem
key={todo.id}
todo={todo}
showSelectedTodo={showSelectedTodo}
selectedTodo={selectedTodo}
/>
))}
</tbody>
</table>
);
};
Loading
Loading