Skip to content

Commit

Permalink
converted to async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Krykunov committed Nov 30, 2024
1 parent 6236ae3 commit 9ee319a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
65 changes: 34 additions & 31 deletions src/hooks/useTodos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const useTodos = () => {
[todos, activeFilter],
);

const addTodo = (title: string) => {
const addTodo = useCallback(async (title: string) => {
setTempTodo({
id: 0,
userId: postService.USER_ID,
Expand All @@ -29,39 +29,43 @@ export const useTodos = () => {
});
setIsLoading(true);

return postService
.createTodo(title)
.then(newTodo => {
try {
try {
const newTodo = await postService.createTodo(title);

setEditingTodos(current => [...current, 0]);

setTodos(currentTodos => [...currentTodos, newTodo]);
})
.catch(() => {
} catch {
setErrorMessage('Unable to add a todo');
throw new Error('Unable to add a todo');
})
.finally(() => {
setTempTodo(null);
setIsLoading(false);
});
};
}
} finally {
setTempTodo(null);
setIsLoading(false);
}
}, []);

const loadTodos = useCallback(() => {
const loadTodos = useCallback(async () => {
setIsLoading(true);
postService
.getTodos()
.then(setTodos)
.catch(() => setErrorMessage('Unable to load todos'))
.finally(() => setIsLoading(false));
try {
const loadedTodos = await postService.getTodos();

setTodos(loadedTodos);
} catch {
setErrorMessage('Unable to load todos');
} finally {
setIsLoading(false);
}
}, []);

const updateTodo = (newTodo: Todo) => {
const updateTodo = useCallback(async (newTodo: Todo) => {
setIsLoading(true);
setEditingTodos(current => [...current, newTodo.id]);

return postService
.updateTodo(newTodo)
.then(() => {
try {
try {
await postService.updateTodo(newTodo);
setTodos(currentTodos => {
const newTodos = [...currentTodos];
const index = newTodos.findIndex(todo => todo.id === newTodo.id);
Expand All @@ -70,16 +74,15 @@ export const useTodos = () => {

return newTodos;
});
})
.catch(error => {
} catch (error) {
setErrorMessage('Unable to update a todo');
throw new Error(error);
})
.finally(() => {
setEditingTodos([]);
setIsLoading(false);
});
};
throw new Error();
}
} finally {
setEditingTodos([]);
setIsLoading(false);
}
}, []);

const deleteTodo = useCallback((id: number) => {
setIsLoading(true);
Expand Down
19 changes: 9 additions & 10 deletions src/utils/fetchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function wait(delay: number) {
// To have autocompletion and avoid mistypes
type RequestMethod = 'GET' | 'POST' | 'PATCH' | 'DELETE';

function request<T>(
async function request<T>(
url: string,
method: RequestMethod = 'GET',
data: any = null, // we can send any data to the server
Expand All @@ -27,15 +27,14 @@ function request<T>(
}

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

return response.json();
});
await wait(100);
const response = await fetch(BASE_URL + url, options);

if (!response.ok) {
throw new Error();
}

return response.json();
}

export const client = {
Expand Down

0 comments on commit 9ee319a

Please sign in to comment.