Skip to content

Commit

Permalink
Done
Browse files Browse the repository at this point in the history
  • Loading branch information
Iryna Mariiko authored and Iryna Mariiko committed Dec 26, 2024
1 parent 12e12c9 commit a7ba732
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export const App: React.FC = () => {
const [isInputDisabled, setIsInputDisabled] = useState(false);
const [isToggleAllActive, setIsToggleAllActive] = useState(false);
const [query, setQuery] = useState('');
const [isLoading, setIsLoading] = useState(false);
const inputRef = useRef<HTMLInputElement | null>(null);

useEffect(() => {
Expand Down Expand Up @@ -187,17 +186,18 @@ export const App: React.FC = () => {
};

const handleRenamingTodo = (renamedTodo: Todo) => {
setIsLoading(true);
updateTodos(renamedTodo)
setCurrentTodoIds([...currentTodoIds, renamedTodo.id]);

return updateTodos(renamedTodo)
.then(() => {
setTodos(currentTodos =>
currentTodos.map(todo =>
setTodos(prevTodos =>
prevTodos.map(todo =>
todo.id === renamedTodo.id ? renamedTodo : todo,
),
);
})
.catch(() => setError(ErrorMessage.UpdateTodo))
.finally(() => setIsLoading(false));
.finally(() => setCurrentTodoIds([]));
};

if (!USER_ID) {
Expand Down Expand Up @@ -228,7 +228,7 @@ export const App: React.FC = () => {
todo={todo}
onToggleStatus={toggleTodoStatus}
handleDeleteTodo={handleDeleteTodo}
isLoading={currentTodoIds.includes(todo.id) || isLoading}
isLoading={currentTodoIds.includes(todo.id)}
onRenamingTodo={handleRenamingTodo}
/>
))}
Expand Down
12 changes: 4 additions & 8 deletions src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ type Props = {
onToggleStatus: (v: Todo) => void;
handleDeleteTodo: (v: number) => void;
isLoading: boolean;
onRenamingTodo: (v: Todo) => void;
onRenamingTodo: (v: Todo) => Promise<void>;
};

export const TodoItem: React.FC<Props> = ({
todo: todoItem, // змінив ім'я параметра
todo: todoItem,
onToggleStatus,
handleDeleteTodo,
isLoading,
Expand Down Expand Up @@ -42,7 +42,7 @@ export const TodoItem: React.FC<Props> = ({
const trimmedTitle = renamedTodo.trim();

if (trimmedTitle === todoItem.title) {
setIsEditing(false); // Залишити закритим, якщо нічого не змінилося
setIsEditing(false);

return;
}
Expand All @@ -53,14 +53,10 @@ export const TodoItem: React.FC<Props> = ({
return;
}

// Викликаємо onRenamingTodo без .catch(), якщо це не асинхронна функція
onRenamingTodo({
...todoItem,
title: trimmedTitle,
});

// Якщо редагування успішне, закриваємо форму
setIsEditing(false);
}).finally(() => setIsEditing(false));
};

return (
Expand Down

0 comments on commit a7ba732

Please sign in to comment.