Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RostyslavSharuiev committed Oct 14, 2024
1 parent b2617a5 commit e59577b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
26 changes: 19 additions & 7 deletions src/components/Footer/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useContext } from 'react';
import { FC, useContext, useMemo } from 'react';
import cn from 'classnames';

import { Filter } from '../../types/Filter';
Expand All @@ -9,14 +9,25 @@ export const Footer: FC = () => {
const dispatch = useContext(DispatchContext);
const { todos, filter } = useContext(StateContext);

const completedTodosCount = todos.filter(todo => todo.completed).length;
const uncompletedTodos = todos.filter(todo => !todo.completed);
const completedTodosCount = useMemo(
() => todos.filter(todo => todo.completed).length,
[todos],
);

const uncompletedTodos = useMemo(
() => todos.filter(todo => !todo.completed),
[todos],
);

const message = `${uncompletedTodos.length} ${uncompletedTodos.length === 1 ? 'item' : 'items'} left`;

const handleClearCompleted = () => {
dispatch({ type: 'changeTodos', payload: uncompletedTodos });
};

const message = `${uncompletedTodos.length} ${uncompletedTodos.length === 1 ? 'item' : 'items'} left`;
const handleChooseFilter = (filterTitle: Filter) => {
dispatch({ type: 'setFilter', payload: filterTitle });
};

if (!todos.length) {
return null;
Expand All @@ -38,9 +49,7 @@ export const Footer: FC = () => {
selected: filter === filterTitle,
})}
data-cy={`FilterLink${filterTitle}`}
onClick={() =>
dispatch({ type: 'setFilter', payload: filterTitle })
}
onClick={() => handleChooseFilter(filterTitle)}
>
{filterTitle}
</a>
Expand All @@ -54,6 +63,9 @@ export const Footer: FC = () => {
data-cy="ClearCompletedButton"
onClick={handleClearCompleted}
disabled={!completedTodosCount}
style={{
visibility: completedTodosCount ? 'visible' : 'hidden',
}}
>
Clear completed
</button>
Expand Down
32 changes: 17 additions & 15 deletions src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ChangeEvent,
FC,
FormEvent,
useCallback,
useContext,
useEffect,
useState,
Expand Down Expand Up @@ -32,15 +33,15 @@ export const TodoItem: FC<Props> = ({ todo }) => {
});
};

const handleDeleteTodo = () => {
const handleDeleteTodo = useCallback(() => {
dispatch({ type: 'removeTodo', payload: todo.id });
};
}, [dispatch, todo.id]);

const handleTitleChange = (event: ChangeEvent<HTMLInputElement>) => {
setTitle(event.target.value.trimStart());
};

const handleTitleSave = () => {
const handleTitleSave = useCallback(() => {
const trimmedTitle = title.trim();

if (!trimmedTitle) {
Expand All @@ -58,28 +59,28 @@ export const TodoItem: FC<Props> = ({ todo }) => {
});

setIsEditing(false);
};
}, [title, todo, dispatch, handleDeleteTodo]);

const handleFormSubmit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
};

const handleKeyUp = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
setIsEditing(false);
setTitle(todo.title);
} else if (event.key === 'Enter') {
handleTitleSave();
}
};
const handleKeyUp = useCallback(
(event: KeyboardEvent) => {
if (event.key === 'Escape') {
setIsEditing(false);
setTitle(todo.title);
} else if (event.key === 'Enter') {
handleTitleSave();
}
},
[todo.title, handleTitleSave],
);

useEffect(() => {
if (isEditing) {
document.addEventListener('keyup', handleKeyUp);
}
// else {
// document.addEventListener('keyup', handleKeyUp);
// }

return () => {
document.removeEventListener('keyup', handleKeyUp);
Expand All @@ -95,6 +96,7 @@ export const TodoItem: FC<Props> = ({ todo }) => {
onDoubleClick={() => setIsEditing(true)}
>
<label className="todo__status-label">
{' '}
<input
data-cy="TodoStatus"
type="checkbox"
Expand Down
2 changes: 0 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { createRoot } from 'react-dom/client';

import './styles/index.scss';
// import './styles/todo-list.scss';
// import './styles/filters.scss';

import { App } from './App';
import { GlobalStateProvider } from './store/store';
Expand Down

0 comments on commit e59577b

Please sign in to comment.