forked from Somnia/SomniaRoomsApp
Homepage - Initial implementation.
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user