Skip to content

Commit

Permalink
fixed2
Browse files Browse the repository at this point in the history
  • Loading branch information
Krrampuss committed Dec 24, 2024
1 parent 6badcb2 commit 42650a3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 31 deletions.
24 changes: 0 additions & 24 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,6 @@ export const App = () => {
return todos;
}
}, [todos, filteredBy]);
// setLoadingTodoId(todoId);
// deleteTodo(todoId)
// .then(() => {
// setTodos(currentTodos =>
// currentTodos.filter(todo => todo.id !== todoId),
// );
// })
// .catch(() => {
// setErrorMessage(Error.Delete);
// setTimeout(() => setErrorMessage(Error.Default), 3000);
// })
// .finally(() => {
// setLoadingTodoId(null);
// inputRef.current?.focus();
// });
// }, []);

// const deleteCompleted = useCallback(() => {
// const completedIds = todos
// .filter(todo => todo.completed)
// .map(todo => todo.id);

// completedIds.forEach(id => deletePost(id));
// }, [todos, deletePost]);

useEffect(() => {
loadTodos();
Expand Down
9 changes: 5 additions & 4 deletions src/api/todos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@ import { Todo } from '../types/Todo';
import { client } from '../utils/fetchClient';

export const USER_ID = 2129;
const TODOS = '/todos';

export const getTodos = () => {
const baseUrl = `/todos?userId=${USER_ID}`;
const baseUrl = `${TODOS}?userId=${USER_ID}`;

return client.get<Todo[]>(baseUrl);
};

export const deleteTodo = (id: number) => {
const baseUrl = `/todos/${id}`;
const baseUrl = `${TODOS}/${id}`;

return client.delete(baseUrl);
};

export const addTodo = (newTodo: Omit<Todo, 'id'>) => {
const baseUrl = `/todos`;
const baseUrl = `${TODOS}`;

return client.post<Todo>(baseUrl, newTodo);
};

export const updateTodo = (id: number, completed: boolean, title?: string) => {
const baseUrl = `/todos/${id}`;
const baseUrl = `${TODOS}/${id}`;

const updateData: { completed: boolean; title?: string } = { completed };

Expand Down
16 changes: 13 additions & 3 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,21 @@ export const Header: React.FC<Props> = ({
function handleSubmit(event: React.FormEvent) {
event.preventDefault();

let timerId;

const trimmedTitle = newTodoTitle.trim();

if (!trimmedTitle) {
setErrorMessage(Error.Empty);
setTimeout(() => setErrorMessage(Error.Default), 3000);

if (timerId) {
clearTimeout(timerId);
}

timerId = setTimeout(() => {
setErrorMessage(Error.Default);
timerId = null;
}, 3000);

return;
}
Expand All @@ -78,7 +88,7 @@ export const Header: React.FC<Props> = ({
addPost(loadingTodo);
}

const completeAll = useCallback(async () => {
const handleCompleteAll = useCallback(async () => {
const allCompleted = todos.every(todo => todo.completed);
const newCompletionState = !allCompleted;

Expand Down Expand Up @@ -113,7 +123,7 @@ export const Header: React.FC<Props> = ({
<header className="todoapp__header">
{!!todos.length && (
<button
onClick={completeAll}
onClick={handleCompleteAll}
type="button"
className={cn('todoapp__toggle-all', activeCount === 0 && 'active')}
data-cy="ToggleAllButton"
Expand Down

0 comments on commit 42650a3

Please sign in to comment.