Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
save-v committed Dec 22, 2024
1 parent b39ea1b commit 5a1fdd3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
27 changes: 14 additions & 13 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ export const App: React.FC = () => {
});
}

function handleTitleChange(newTitle: string, editingTodoId1: number | null) {
function handleTitleChange(newTitle: string, editingTodoId: number | null) {
const updateStatus = { title: newTitle };

return client
.patch<Todo>(`/todos/${editingTodoId1}`, updateStatus)
.patch<Todo>(`/todos/${editingTodoId}`, updateStatus)
.then(fetchedTodo => {
changeState(editingTodoId1 as number, setFilteredTodos, fetchedTodo);
changeState(editingTodoId1 as number, setTodos, fetchedTodo);
changeState(editingTodoId as number, setFilteredTodos, fetchedTodo);
changeState(editingTodoId as number, setTodos, fetchedTodo);
})
.catch(error => {
ShowError(Error.UpdateError);
Expand All @@ -132,23 +132,27 @@ export const App: React.FC = () => {
});
}

function setFocusOnAddInput() {
if (addTodoField.current !== null) {
addTodoField.current.focus();
}
}

useEffect(() => {
client
.get<Todo[]>(`/todos?userId=${USER_ID}`)
.then(fetchedTodos => {
setTodos(fetchedTodos);
setFilteredTodos(fetchedTodos);
if (addTodoField.current !== null) {
addTodoField.current.focus();
}
setFocusOnAddInput();
})
.catch(() => ShowError(Error.LoadError));
}, []);

useEffect(() => {
if (addTodoField.current !== null && tempTodo === null) {
if (tempTodo === null) {
/*tempTodo === null для того, не виконувати це два рази (бо стейт tempTodo спочатку змінюється на об'єкт а потім змінюється на null)*/
addTodoField.current.focus();
setFocusOnAddInput();
}
}, [tempTodo]);

Expand Down Expand Up @@ -207,10 +211,7 @@ export const App: React.FC = () => {
});

Promise.all(promises).then(() => {
//винести
if (addTodoField.current !== null) {
addTodoField.current.focus();
}
setFocusOnAddInput();
});
}

Expand Down
10 changes: 6 additions & 4 deletions src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ export const TodoItem: React.FC<Props> = ({
const [editingTodoId, setEditingTodoId] = useState<number | null>(null);
const [isUpdatingTitle, setIsUpdatingTitle] = useState<boolean>(false);

useEffect(() => {
function setFocusOnEditInput() {
if (editTodoField.current !== null) {
editTodoField.current.focus();
}
}

useEffect(() => {
setFocusOnEditInput();
}, [editingTodoId]);

function trimTitle(text: string) {
Expand Down Expand Up @@ -102,9 +106,7 @@ export const TodoItem: React.FC<Props> = ({
setEditingTodoId(null);
})
.catch(() => {
if (editTodoField.current !== null) {
editTodoField.current.focus();
}
setFocusOnEditInput();
})
.finally(() => {
setIsUpdatingTitle(false);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/fetchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function request<T>(
}

// DON'T change the delay it is required for tests +
return wait(400)
return wait(100)
.then(() => fetch(BASE_URL + url, options))
.then(response => {
if (!response.ok) {
Expand Down

0 comments on commit 5a1fdd3

Please sign in to comment.