Estado inicial: frontend React + backend Laravel
This commit is contained in:
24
frontend-plataforma-tutoriais/src/hooks/useGetVideos.ts
Normal file
24
frontend-plataforma-tutoriais/src/hooks/useGetVideos.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import type { ApiErrorResponse, Video } from "../types";
|
||||
|
||||
export function useGetVideos() {
|
||||
async function getVideos(searchQuery?: string) {
|
||||
const response = await fetch(`http://127.0.0.1:8000/api/videos?search=${encodeURIComponent(searchQuery ?? "")}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${localStorage.getItem("token")}`
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok) {
|
||||
return data.data as Video[];
|
||||
} else {
|
||||
return (data as ApiErrorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
return { getVideos };
|
||||
}
|
||||
Reference in New Issue
Block a user