Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
andrushchenkoo committed Jan 7, 2025
1 parent f435907 commit 6f76c09
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/api/todos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ import { Todo } from '../types/Todo';
import { client } from '../utils/fetchClient';

export const USER_ID = 2167;
const TODOS_URL = '/todos';

export const getTodos = () => {
return client.get<Todo[]>(`/todos?userId=${USER_ID}`);
return client.get<Todo[]>(`${TODOS_URL}?userId=${USER_ID}`);
};

export const addTodo = (newTodo: Omit<Todo, 'id' | 'userId'>) => {
return client.post<Todo>('/todos', {
return client.post<Todo>(`${TODOS_URL}`, {
...newTodo,
userId: USER_ID,
});
};

export const deleteTodo = (todoId: number) => {
return client.delete(`/todos/${todoId}`);
return client.delete(`${TODOS_URL}/${todoId}`);
};

export const updateTodo = (todo: Todo) => {
return client.patch<Todo>(`/todos/${todo.id}`, todo);
return client.patch<Todo>(`${TODOS_URL}/${todo.id}`, todo);
};
4 changes: 1 addition & 3 deletions src/components/TodoFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ export const TodoFooter: React.FC<Props> = props => {
className="todoapp__clear-completed"
disabled={todosCompletedCounter === 0}
data-cy="ClearCompletedButton"
onClick={() => {
onClearCompleted();
}}
onClick={onClearCompleted}
>
Clear completed
</button>
Expand Down
6 changes: 4 additions & 2 deletions src/components/TodoHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ export const TodoHeader: React.FC<Props> = props => {

const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
if (inputValue.trim() === '') {
const normalizedTitle = inputValue.trim();

if (normalizedTitle === '') {
setErrorMessage(Error.EmptyTitle);

return;
}

try {
await onAddTodo(inputValue.trim());
await onAddTodo(normalizedTitle);
setInputValue('');
} catch (err) {}
};
Expand Down

0 comments on commit 6f76c09

Please sign in to comment.