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

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

solution #1726

wants to merge 5 commits into from

Conversation

Vitalii120296
Copy link

src/App.tsx Outdated
Comment on lines 29 to 48
useEffect(() => {
getTodos()
.then(response => {
let filteredTodos = response;

if (filter === 'active') {
filteredTodos = response.filter(todo => todo.completed === false);
}

if (filter === 'completed') {
filteredTodos = response.filter(todo => todo.completed === true);
}

setTodos(filteredTodos);
setErrorMessage('');
})
.catch(() => {
handleErrorMessage('Unable to load todos');
});
}, [filter]);

Choose a reason for hiding this comment

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

Suggested change
useEffect(() => {
getTodos()
.then(response => {
let filteredTodos = response;
if (filter === 'active') {
filteredTodos = response.filter(todo => todo.completed === false);
}
if (filter === 'completed') {
filteredTodos = response.filter(todo => todo.completed === true);
}
setTodos(filteredTodos);
setErrorMessage('');
})
.catch(() => {
handleErrorMessage('Unable to load todos');
});
}, [filter]);
useEffect(() => {
getTodos()
.then(response => {
setTodos(response);
setErrorMessage('');
})
.catch(() => {
handleErrorMessage('Unable to load todos');
});
}, []);
const filteredTodos = getFilteredTodos(todos, filter)

to avoid redundant fetch requests

Copy link

@volodymyr-soltys97 volodymyr-soltys97 left a comment

Choose a reason for hiding this comment

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

Hi, It is bad practice to write all the code in one component, you need to separate the logic into different components, for example Header, Footer, TodoList, TodoItem

src/App.tsx Outdated
}, []);

function getFilterTodos(todoForFilter: Todo[], forFilter: string): Todo[] {
if (forFilter === 'active') {

Choose a reason for hiding this comment

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

Create a enum for 'all', 'active', 'completed' and use it everywhere

src/App.tsx Outdated

function getFilterTodos(todoForFilter: Todo[], forFilter: string): Todo[] {
if (forFilter === 'active') {
return todoForFilter.filter(todo => todo.completed === false);

Choose a reason for hiding this comment

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

Suggested change
return todoForFilter.filter(todo => todo.completed === false);
return todoForFilter.filter(todo => !todo.completed);

src/App.tsx Outdated
}

if (forFilter === 'completed') {
return todoForFilter.filter(todo => todo.completed === true);

Choose a reason for hiding this comment

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

Suggested change
return todoForFilter.filter(todo => todo.completed === true);
return todoForFilter.filter(todo => todo.completed);

src/App.tsx Outdated
});
};

function handleClearCompleted() {

Choose a reason for hiding this comment

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

It is important to follow the same way of creating functions everywhere, fix it everywhere

Suggested change
function handleClearCompleted() {
const handleClearCompleted = () => {

Copy link

@volodymyr-soltys97 volodymyr-soltys97 left a comment

Choose a reason for hiding this comment

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

Good job 👍
Let's improve your code

src/App.tsx Outdated
});
}, []);

function getFilterTodos(todoForFilter: Todo[], enumFilter: number): Todo[] {

Choose a reason for hiding this comment

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

It is important to follow the same way of creating functions everywhere, fix it everywhere

Suggested change
function getFilterTodos(todoForFilter: Todo[], enumFilter: number): Todo[] {
const getFilterTodos = (todoForFilter: Todo[], enumFilter: number): Todo[] => {

@@ -0,0 +1 @@
export * from './AddTodo';

Choose a reason for hiding this comment

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

Do you need similar files everywhere? Consider will be better if you will remove it
image

return (
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
{[...todos].filter(todo => !todo.completed).length} items left

Choose a reason for hiding this comment

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

Move this logic to the helper variable and use it here

Suggested change
{[...todos].filter(todo => !todo.completed).length} items left
{todos.filter(todo => !todo.completed).length} items left

Comment on lines 26 to 56
<a
href="#/"
className={classNames('filter__link', !filter && 'selected')}
data-cy="FilterLinkAll"
onClick={() => {
setFilter(Filter.all);
}}
>
All
</a>

<a
href="#/active"
className={classNames(
'filter__link',
filter === Filter.active && 'selected',
)}
data-cy="FilterLinkActive"
onClick={() => {
setFilter(Filter.active);
}}
>
Active
</a>

<a
href="#/completed"
className={classNames(
'filter__link',
filter === Filter.completed && 'selected',
)}

Choose a reason for hiding this comment

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

Use Object.values(Filter) and render these options with map() method

className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
onClick={handleClearCompleted}
disabled={todos.filter(todo => todo.completed === true).length === 0}

Choose a reason for hiding this comment

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

Move it to helper variable

Suggested change
disabled={todos.filter(todo => todo.completed === true).length === 0}
disabled={todos.filter(todo => todo.completed).length === 0}

<div
key={todo.id}
data-cy="Todo"
className={`todo ${todo.completed ? 'completed' : 'item-enter-done'}`}

Choose a reason for hiding this comment

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

Use classnames library for add classes with conditions, fix it everywhere

Copy link

@volodymyr-soltys97 volodymyr-soltys97 left a comment

Choose a reason for hiding this comment

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

Hi, you need to fix all comments from the previous review, for example this comment still not fixed
image

src/App.tsx Outdated
deleteTodo(todo.id).catch(() => {
handleErrorMessage('Unable to delete a todo');

return Promise.reject({ id: todo.id }); // Передаємо id в reason

Choose a reason for hiding this comment

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

Remove all comments

Suggested change
return Promise.reject({ id: todo.id }); // Передаємо id в reason
return Promise.reject({ id: todo.id });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants