Last active
May 16, 2023 08:11
-
-
Save omnajjar/a7e92df05f02058e8e2b1728b26edbbd to your computer and use it in GitHub Desktop.
Primer Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HTTPError } from "./errors"; | |
const BASE_API_ENDPOINT = import.meta.API_END_POINT; | |
const AUTH_TOKEN = import.meta.env.VITE_API_TOKEN; | |
const todosAPIClient = createTodosAPIClient(); | |
export async function getTodos() { | |
try { | |
const res = await todosAPIClient(); | |
if (!res.ok) { | |
throw new HTTPError("Oops! something went wrong fetching the todos", res.status, res.statusText); | |
} | |
const data = await res.json(); | |
return data; | |
} catch (error) { | |
console.error(error); | |
throw error; | |
} | |
} | |
export async function createNewTodo(content) { | |
try { | |
const newTodo = { content }; | |
const res = await todosAPIClient(BASE_API_ENDPOINT, "POST", JSON.stringify(newTodo)); | |
if (!res.ok) { | |
throw new HTTPError("Oops! something went wrong while creating a new todo", res.status, res.statusText); | |
} | |
} catch (error) { | |
console.error(error); | |
throw error; | |
} | |
} | |
export async function completeTodo(id) { | |
try { | |
const res = await todosAPIClient(`${BASE_API_ENDPOINT}/${id}/close`, "POST"); | |
if (!res.ok) { | |
throw new HTTPError("Oops! something went wrong while attempting to close a todo", res.status, res.statusText); | |
} | |
} catch (error) { | |
console.error(error); | |
throw error; | |
} | |
} | |
function createTodosAPIClient() { | |
const headers = { | |
"Content-Type": "application/json", | |
"Authorization": `Bearer ${AUTH_TOKEN}` | |
}; | |
return (url = BASE_API_ENDPOINT, method = "GET", body = undefined) => { | |
return fetch(url, { | |
headers, | |
method, | |
...(body ? { body } : {}) | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class HTTPError extends Error { | |
constructor(message, statusCode, statusText,) { | |
super(message); | |
this.statusCode = statusCode; | |
this.statusText = statusText; | |
this.name = 'HTTPError'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect, useCallback } from "react"; | |
import { getTodos, createNewTodo, completeTodo } from "./api"; | |
export function useTodos() { | |
const [todos, setTodos] = useState([]); | |
const [loading, setLoading] = useState(false); | |
const [error, setError] = useState(null); | |
const fetchTodos = useCallback(async () => { | |
try { | |
setLoading(true); | |
setError(null); | |
const todos = await getTodos(); | |
setTodos(todos); | |
setLoading(false); | |
} catch (error) { | |
setError(error); | |
setLoading(false); | |
} | |
}, []); | |
const addTodo = useCallback(async (content) => { | |
try { | |
setLoading(true); | |
setError(null); | |
await createNewTodo(content); | |
await fetchTodos(); | |
} catch (error) { | |
setError(error); | |
setLoading(false); | |
} | |
}, []); | |
const setTodoToDone = useCallback(async (id) => { | |
try { | |
setLoading(true); | |
setError(null); | |
await completeTodo(id); | |
await fetchTodos(); | |
} catch (error) { | |
setError(error); | |
setLoading(false); | |
} | |
}, []); | |
useEffect(() => { | |
fetchTodos(); | |
}, []); | |
return { | |
todos, | |
loading, | |
error, | |
addTodo, | |
setTodoToDone | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment