-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add task solution #1497
base: master
Are you sure you want to change the base?
Add task solution #1497
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job!
There was a problem hiding this 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 solution
Watch the attached video and fix the bug
https://www.loom.com/share/351ebf9f477e46f0bbe0d5437efdb54d
src/App.tsx
Outdated
return <UserWarning />; | ||
} | ||
const [todos, setTodos] = useState<Todo[]>([]); | ||
const [error, setError] = useState<string | null>(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const [error, setError] = useState<string | null>(null); | |
const [error, setError] = useState<ErrorMessages>(ErrorMessages.DEFAULT); |
src/App.tsx
Outdated
const [isEditingId, setIsEditingId] = useState<number | null>(null); | ||
|
||
const handleAddTodo = useCallback(async (): Promise<void> => { | ||
if (title.trim() === '') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a variable for trimmed title
as u use it more than once.
src/App.tsx
Outdated
} catch { | ||
setError(ErrorMessages.ADDING_TODOS); | ||
setTempTodo(null); | ||
setTimeout(() => setError(null), 3000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider clearing timeouts when the component unmounts to avoid potential memory leaks and ensure smooth handling of state updates. This will improve the reliability of your code and prevent errors in case the DOM element is no longer available.
src/App.tsx
Outdated
const fetchTodos = async () => { | ||
try { | ||
const data = await getTodos(); | ||
|
||
setTodos(data); | ||
} catch (err) { | ||
setError(ErrorMessages.LOAD_TODOS); | ||
const timer = setTimeout(() => setError(null), 3000); | ||
|
||
return () => clearTimeout(timer); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the fetch Todos
function out from useEffect
src/components/Footer.tsx
Outdated
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{`${activeTodos} items left`} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{`${activeTodos} items left`} | |
{activeTodos} items left |
src/components/ErrorNotification.tsx
Outdated
onHideError: () => void; | ||
} | ||
|
||
export const ErrorNotification: React.FC<ErrorNotificationProps> = ({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export const ErrorNotification: React.FC<ErrorNotificationProps> = ({ | |
export const ErrorNotification: FC<ErrorNotificationProps> = ({ |
src/components/Footer.tsx
Outdated
</span> | ||
|
||
<nav className="filter" data-cy="Filter"> | ||
{Object.values(FilterStates).map(state => ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a variable for this Object.values(FilterStates)
src/components/TodoItem.tsx
Outdated
await onDelete(todo.id); | ||
} catch { | ||
setError(ErrorMessages.DELETING_TODOS); | ||
setTimeout(() => setError(null), 3000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the setTimeout
to the useEffect
and add the array dependency with ErrorMessages
src/components/TodoItem.tsx
Outdated
}; | ||
|
||
return ( | ||
<div data-cy="Todo" className={`todo ${todo.completed ? 'completed' : ''}`}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's better to use classnames
in cases like that, it allows you to dynamically toggle classes based on state or props without manually concatenating strings, making the code more maintainable and easier to read.
https://WatarJoy.github.io/react_todo-app-with-api/