Skip to content

Commit

Permalink
fix: error hook, clear timeout, disable linter warn for some deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Ann-Maksay committed Oct 22, 2024
1 parent 1d2fa12 commit 9d404a7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
updateTodo,
} from './api/todos';
import { FilterOption, Todo, Error } from './types';
import { useError } from './hooks';

export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [error, setError] = useState<Error>(Error.DEFAULT);
const [filterOption, setFilterOption] = useState<FilterOption>(
FilterOption.all,
);
Expand All @@ -21,21 +21,14 @@ export const App: React.FC = () => {
const [loadingTodoIds, setLoadingTodoIds] = useState<number[]>([]);
const [editingTodoId, setEditingTodoId] = useState<number | null>(null);
const [focusHeader, setFocusHeader] = useState(true);
const { error, handleError, handleResetError } = useError();

const todosAmount = useMemo(() => todos.length, [todos]);
const uncompletedTodosAmount = useMemo(
() => todos.reduce((amount, todo) => amount + (todo.completed ? 0 : 1), 0),
[todos],
);

const handleResetError = () => setError(Error.DEFAULT);

const handleError = (message: Error) => {
setError(message);

setTimeout(handleResetError, 3000);
};

const handleSubmitTodo = (title: string) => {
if (!title) {
handleError(Error.EMPTY_TITLE);
Expand Down Expand Up @@ -170,6 +163,7 @@ export const App: React.FC = () => {
.catch(() => {
handleError(Error.LOADING_TODOS);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
Expand Down
1 change: 1 addition & 0 deletions src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const TodoItem: React.FC<Props> = ({
return () => {
document.removeEventListener('keyup', handleReverseUpdate);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isEditing]);

return (
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useError } from './useError';
20 changes: 20 additions & 0 deletions src/hooks/useError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useRef, useState } from 'react';
import { Error } from '../types';

export const useError = () => {
const [error, setError] = useState<Error>(Error.DEFAULT);

const errorTimeoutId = useRef<NodeJS.Timeout>();

const handleResetError = () => setError(Error.DEFAULT);

const handleError = (message: Error) => {
setError(message);

clearTimeout(errorTimeoutId.current);

errorTimeoutId.current = setTimeout(handleResetError, 3000);
};

return { error, handleError, handleResetError };
};

0 comments on commit 9d404a7

Please sign in to comment.