feat: paginate workshops and videos pages
This commit is contained in:
@@ -1,22 +1,42 @@
|
||||
import type { ApiErrorResponse, Video } from "../types";
|
||||
|
||||
type GetVideosParams = {
|
||||
page?: number;
|
||||
search?: string;
|
||||
category?: string;
|
||||
status?: "active" | "inactive";
|
||||
watched?: 0 | 1;
|
||||
};
|
||||
|
||||
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")}`
|
||||
},
|
||||
});
|
||||
async function getVideos(params: GetVideosParams) {
|
||||
const query = new URLSearchParams();
|
||||
|
||||
if (params.page !== undefined) query.append("page", params.page.toString());
|
||||
if (params.search) query.append("search", params.search);
|
||||
if (params.category) query.append("category", params.category);
|
||||
if (params.status) query.append("status", params.status);
|
||||
if (params.watched !== undefined) query.append("watched", params.watched.toString());
|
||||
|
||||
const response = await fetch(`http://127.0.0.1:8000/api/videos?${query.toString()}`, {
|
||||
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[];
|
||||
return {
|
||||
videos: data.data as Video[],
|
||||
meta: data.meta
|
||||
};
|
||||
} else {
|
||||
return (data as ApiErrorResponse);
|
||||
return data as ApiErrorResponse;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user