Skip to content

Commit

Permalink
fix code structure and add hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
taniabarkovskya committed Dec 20, 2024
1 parent 8430161 commit 460597e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
78 changes: 44 additions & 34 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-len */
/* eslint-disable jsx-a11y/label-has-associated-control */
/* eslint-disable jsx-a11y/control-has-associated-label */
import React, { useCallback, useEffect, useState } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { UserWarning } from './UserWarning';
import {
createTodo,
Expand All @@ -27,8 +27,16 @@ export const App: React.FC = () => {

const [tempTodo, setTempTodo] = useState<Todo | null>(null);

const handleErrorClose = useCallback(() => {
setErrorTodos(ErrorType.NoErrors);
useEffect(() => {
setIsLoading(true);
getTodos()
.then(setTodos)
.catch(() => {
setErrorTodos(ErrorType.Loading);
})
.finally(() => {
setIsLoading(false);
});
}, []);

const visibleTodos = todos.filter(todo => {
Expand All @@ -42,35 +50,37 @@ export const App: React.FC = () => {
}
});

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

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

const completedTodos = todos.filter(todo => todo.completed);
const completedTodosCount = completedTodos.length;

useEffect(() => {
setIsLoading(true);
getTodos()
.then(setTodos)
.catch(() => {
setErrorTodos(ErrorType.Loading);
})
.finally(() => {
setIsLoading(false);
});
const handleErrorClose = useCallback(() => {
setErrorTodos(ErrorType.NoErrors);
}, []);

const onAddTodo = async (newTodo: Omit<Todo, 'id'>): Promise<void> => {
try {
const createdTodo = await createTodo(newTodo);
const onAddTodo = useCallback(
async (newTodo: Omit<Todo, 'id'>): Promise<void> => {
try {
const createdTodo = await createTodo(newTodo);

setTodos(currentTodos => [...currentTodos, createdTodo]);
} catch (error) {
setErrorTodos(ErrorType.Add);
throw error;
}
};
setTodos(currentTodos => [...currentTodos, createdTodo]);
} catch (error) {
setErrorTodos(ErrorType.Add);
throw error;
}
},
[],
);

const onDeleteTodo = async (todoId: number) => {
const onDeleteTodo = useCallback(async (todoId: number) => {
setLoadingTodosIds(currentIds => [...currentIds, todoId]);
try {
await deleteTodo(todoId);
Expand All @@ -81,15 +91,15 @@ export const App: React.FC = () => {
} finally {
setLoadingTodosIds(currentIds => currentIds.filter(id => id !== todoId));
}
};
}, []);

const onDeleteAllCompleted = () => {
const onDeleteAllCompleted = useCallback(() => {
completedTodos.forEach(completedTodo => {
onDeleteTodo(completedTodo.id);
});
};
}, [completedTodos, onDeleteTodo]);

const onUpdateTodo = async (newTodo: Todo) => {
const onUpdateTodo = useCallback(async (newTodo: Todo) => {
setLoadingTodosIds(currentIds => [...currentIds, newTodo.id]);
try {
const updatedTodo = await updateTodo(newTodo);
Expand All @@ -107,11 +117,11 @@ export const App: React.FC = () => {
currentIds.filter(id => id !== newTodo.id),
);
}
};
}, []);

const onUpdateToggleAll = () => {
const onUpdateToggleAll = useCallback(() => {
todos.forEach(todo => {
if (todos.length === completedTodos.length) {
if (todos.length === completedTodosCount) {
onUpdateTodo({ ...todo, completed: false });

return;
Expand All @@ -123,7 +133,7 @@ export const App: React.FC = () => {
return;
}
});
};
}, [todos, completedTodosCount, onUpdateTodo]);

if (!USER_ID) {
return <UserWarning />;
Expand All @@ -144,7 +154,7 @@ export const App: React.FC = () => {
onUpdateToggleAll={onUpdateToggleAll}
/>

{todos.length > 0 && (
{!!todos.length && (
<>
<TodoList
todos={visibleTodos}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Footer/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const Footer: React.FC<Props> = props => {
{Object.values(FilterType).map(filter => (
<a
key={filter}
href={`#/${filter === FilterType.All ? '' : filter.toLowerCase()}`}
href={`#/${filter !== FilterType.All && filter.toLowerCase()}`}
className={cn('filter__link', {
selected: status === filter,
})}
Expand Down

0 comments on commit 460597e

Please sign in to comment.