Skip to content

Commit

Permalink
add mentor's commits
Browse files Browse the repository at this point in the history
  • Loading branch information
natalia646 committed Dec 23, 2024
1 parent 2c9669c commit c9df513
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 32 deletions.
27 changes: 11 additions & 16 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [activeStatus, setActiveStatus] = useState(Status.All);
const [tempTodo, setTempTodo] = useState<Todo | null>(null);
const [loadingTodos, setLoadingTodos] = useState<number[]>([]);
const [loadingTodoIds, setLoadingTodoIds] = useState<number[]>([]);
const [errorMessage, setErrorMessage] = useState<ErrorMessage>(
ErrorMessage.Default,
);
Expand Down Expand Up @@ -62,33 +62,30 @@ export const App: React.FC = () => {
};

const onUpdateTodo = (todoToUpdate: Todo) => {
setLoadingTodos(prevTodos => [...prevTodos, todoToUpdate.id]);
setLoadingTodoIds(prevTodos => [...prevTodos, todoToUpdate.id]);

return clientTodo
.updateTodo(todoToUpdate)
.then(updatedTodo => {
setTodos(currentTodos => {
const newTodos = [...currentTodos];
const index = newTodos.findIndex(todo => todo.id === updatedTodo.id);

newTodos.splice(index, 1, updatedTodo);

return newTodos;
return currentTodos.map(todo =>
todo.id === updatedTodo.id ? updatedTodo : todo,
);
});
})
.catch(err => {
setErrorMessage(ErrorMessage.UnableToUpdate);
throw err;
})
.finally(() => {
setLoadingTodos(prevTodos =>
setLoadingTodoIds(prevTodos =>
prevTodos.filter(id => todoToUpdate.id !== id),
);
});
};

const onDeleteTodo = (todoId: number) => {
setLoadingTodos(prevTodos => [...prevTodos, todoId]);
setLoadingTodoIds(prevTodos => [...prevTodos, todoId]);

clientTodo
.deleteTodo(todoId)
Expand All @@ -97,12 +94,11 @@ export const App: React.FC = () => {
currentTodos.filter(todo => todo.id !== todoId),
),
)
.catch(error => {
.catch(() => {
setErrorMessage(ErrorMessage.UnableToDelete);
throw error;
})
.finally(() =>
setLoadingTodos(prevTodos => prevTodos.filter(id => todoId !== id)),
setLoadingTodoIds(prevTodos => prevTodos.filter(id => todoId !== id)),
);
};

Expand All @@ -126,9 +122,8 @@ export const App: React.FC = () => {
clientTodo
.getTodos()
.then(data => setTodos(data))
.catch(error => {
.catch(() => {
setErrorMessage(ErrorMessage.UnableToLoad);
throw error;
});
}, []);

Expand All @@ -154,7 +149,7 @@ export const App: React.FC = () => {
<TodoList
tempTodo={tempTodo}
filteredTodos={filteredTodos}
loadingTodos={loadingTodos}
loadingTodoIds={loadingTodoIds}
onDeleteTodo={onDeleteTodo}
onUpdateTodo={onUpdateTodo}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/components/ErrorNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const ErrorNotification: React.FC<Props> = props => {
}, 3000);

return () => clearTimeout(timer);
}, [error]);
}, [error, setErrorMessage]);

return (
<div
Expand Down
4 changes: 1 addition & 3 deletions src/components/TodoHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ export const TodoHeader: React.FC<Props> = props => {
return;
}

return onAddTodo(title.trim())
.then(() => setTitle(''))
.catch(() => {});
onAddTodo(title.trim()).then(() => setTitle(''));
};

return (
Expand Down
12 changes: 5 additions & 7 deletions src/components/TodoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,26 @@ export const TodoItem: React.FC<Props> = props => {
const updateRef = useRef<HTMLInputElement>(null);

const handleUpdate = () => {
const normaliseTitle = updateTitle.trim();

if (todo.title === updateTitle) {
setIsUpdate(false);

return;
}

if (!normaliseTitle) {
if (!updateTitle) {
onDeleteTodo(todo.id);

return;
}

const updatedTodo: Todo = {
id: todo.id,
completed: todo.completed,
title: normaliseTitle,
title: updateTitle.trim(),
userId: USER_ID,
};

onUpdateTodo(updatedTodo)
.then(() => setIsUpdate(false))
.catch(() => {});
onUpdateTodo(updatedTodo).then(() => setIsUpdate(false));
};

const handleEscape = (event: React.KeyboardEvent<HTMLInputElement>) => {
Expand Down
15 changes: 10 additions & 5 deletions src/components/TodoList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import { TodoItem } from './TodoItem';
type Props = {
tempTodo: Todo | null;
filteredTodos: Todo[];
loadingTodos: number[];
loadingTodoIds: number[];
onDeleteTodo: (todoId: number) => void;
onUpdateTodo: (todoToUpdate: Todo) => Promise<void>;
};

export const TodoList: React.FC<Props> = props => {
const { filteredTodos, tempTodo, loadingTodos, onDeleteTodo, onUpdateTodo } =
props;
const {
filteredTodos,
tempTodo,
loadingTodoIds,
onDeleteTodo,
onUpdateTodo,
} = props;

return (
<section className="todoapp__main" data-cy="TodoList">
Expand All @@ -22,7 +27,7 @@ export const TodoList: React.FC<Props> = props => {
<TodoItem
key={todo.id}
todo={todo}
isLoading={loadingTodos.includes(todo.id)}
isLoading={loadingTodoIds.includes(todo.id)}
onDeleteTodo={onDeleteTodo}
onUpdateTodo={onUpdateTodo}
/>
Expand All @@ -33,7 +38,7 @@ export const TodoList: React.FC<Props> = props => {
<CSSTransition key={0} timeout={300} classNames="temp-item">
<TodoItem
todo={tempTodo}
isLoading={loadingTodos.includes(tempTodo.id)}
isLoading={loadingTodoIds.includes(tempTodo.id)}
onDeleteTodo={() => {}}
onUpdateTodo={onUpdateTodo}
/>
Expand Down

0 comments on commit c9df513

Please sign in to comment.