Skip to content

Commit

Permalink
task solution --focus and errors improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
olshum8 committed Oct 27, 2024
1 parent 6b55b46 commit 3561680
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 29 deletions.
10 changes: 2 additions & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const App: React.FC = () => {
const [errorMessage, setErrorMessage] = useState('');
const [tempTodo, setTempTodo] = useState<Todo | null>();
const inputRef = useRef<HTMLInputElement>(null);
const [isErrorHiden, setIsErrorHiden] = useState(true);

const focusInput = () => {
if (inputRef.current) {
Expand All @@ -32,9 +31,7 @@ export const App: React.FC = () => {
await postService
.addTodos(todo)
.then(newTodo => setTodos([...todos, newTodo]));
setIsErrorHiden(true);
} catch (error) {
setIsErrorHiden(false);
setErrorMessage('Unable to add a todo');
throw error;
}
Expand Down Expand Up @@ -188,8 +185,7 @@ export const App: React.FC = () => {
filteredTodos={filteredTodos}
onAddTodo={addTodo}
onTempTodo={setTempTodo}
errorMessage={setErrorMessage}
isErrorHidden={setIsErrorHiden}
onErrorMessage={setErrorMessage}
onBulkUpdate={bulkCheckTodo}
/>
</header>
Expand All @@ -216,9 +212,7 @@ export const App: React.FC = () => {

<ErrorMessage
errorMessage={errorMessage}
isHidden={isErrorHiden}
setIsHidden={setIsErrorHiden}
setErrorMessage={setErrorMessage}
onErrorMessage={setErrorMessage}
/>
</div>
);
Expand Down
17 changes: 5 additions & 12 deletions src/components/ErrorMessage/ErrorMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,33 @@ import { useEffect } from 'react';

interface Props {
errorMessage: string;
isHidden: boolean;
setIsHidden: (boolean: boolean) => void;
setErrorMessage: (string: string) => void;
onErrorMessage: (string: string) => void;
}

export const ErrorMessage: React.FC<Props> = ({
errorMessage,
isHidden,
setIsHidden,
setErrorMessage,
onErrorMessage,
}) => {
const handleClose = () => {
setIsHidden(true);
setErrorMessage('');
onErrorMessage('');
};

useEffect(() => {
if (errorMessage) {
setIsHidden(false);

const timer = setTimeout(() => {
handleClose();
}, 3000);

return () => clearTimeout(timer);
}
}, [errorMessage, isHidden]);
}, [errorMessage]);

Check warning on line 25 in src/components/ErrorMessage/ErrorMessage.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

React Hook useEffect has a missing dependency: 'handleClose'. Either include it or remove the dependency array

return (
<div
data-cy="ErrorNotification"
className={classNames(
'notification is-danger is-light has-text-weight-normal',
isHidden && 'hidden',
!errorMessage && 'hidden',
)}
>
<button
Expand Down
13 changes: 4 additions & 9 deletions src/components/TodoForm/TodoForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@ interface Props {
todos: Todo[];
filteredTodos: Todo[];
onAddTodo: (todo: Todo) => Promise<void>;
errorMessage: (error: string) => void;
onErrorMessage: (error: string) => void;
onTempTodo: (todo: Todo | null) => void;
onBulkUpdate: (todoArr: Todo[], status: boolean) => void;
focusInput: () => void;
inputRef: React.RefObject<HTMLInputElement>;
isErrorHidden: (boolean: boolean) => void;
}

export const TodoForm: React.FC<Props> = ({
todos,
filteredTodos,
onAddTodo,
errorMessage,
onErrorMessage,
onTempTodo,
focusInput,
inputRef,
isErrorHidden,
onBulkUpdate,
}) => {
const [todo, setTodo] = useState('');
Expand All @@ -40,7 +38,6 @@ export const TodoForm: React.FC<Props> = ({
};

const handleInput = (evnt: React.ChangeEvent<HTMLInputElement>) => {
isErrorHidden(true);
setTodo(evnt.target.value);
setDisableInput(false);

Expand All @@ -56,8 +53,7 @@ export const TodoForm: React.FC<Props> = ({
setDisableInput(true);

if (!todo.trim()) {
isErrorHidden(false);
errorMessage('Title should not be empty');
onErrorMessage('Title should not be empty');
setDisableInput(false);

return;
Expand All @@ -81,8 +77,7 @@ export const TodoForm: React.FC<Props> = ({
} catch (error) {
setDisableInput(false);
onTempTodo(null);
isErrorHidden(false);
errorMessage('Unable to add a todo');
onErrorMessage('Unable to add a todo');
}
};

Expand Down

0 comments on commit 3561680

Please sign in to comment.