Homepage - Initial implementation.

This commit is contained in:
2024-05-05 12:45:59 +02:00
parent 391f2fb3ae
commit e81660914f
13 changed files with 363 additions and 168 deletions
+13
View File
@@ -0,0 +1,13 @@
import { RoomCategory } from "@/model/RoomCategory";
import { fetcherUnwraper } from "./backend";
import useSWR from 'swr';
export function useAPIRoomCategories () {
const { data, error, isLoading } = useSWR<RoomCategory[]>(`/api/room/category`, fetcherUnwraper)
return {
roomCategoryList: data,
isLoading,
isError: error
}
}
+31
View File
@@ -0,0 +1,31 @@
const BACKEND_HOST = import.meta.env.VITE_BACKEND_HOST;
const BACKEND_PORT = import.meta.env.VITE_BACKEND_PORT;
const url = BACKEND_HOST + (BACKEND_PORT == 443 || BACKEND_PORT == 80 ? "" : ":" + BACKEND_PORT)
export function fetcher<T>(endpoint: string): Promise<T> {
return fetch(url + endpoint, {
method: "GET"
})
.then(r => {
if (!r.ok) {
throw new Error(r.statusText)
}
const data = r.json();
return data as Promise<T>;
})
}
export function fetcherUnwraper<T>(endpoint: string): Promise<T> {
return fetch(url + endpoint)
.then(response => {
if (!response.ok) {
throw new Error(response.statusText)
}
return response.json() as Promise<{ results: T }>
})
.then(data => {
return data.results
})
}