forked from Somnia/SomniaRoomsApp
Search Page - Initial Implementation.
- Added components. - Query API to get availability for selected period.
This commit is contained in:
@@ -1,13 +1,25 @@
|
||||
import { RoomCategory } from "@/model/RoomCategory";
|
||||
import { fetcherUnwraper } from "./backend";
|
||||
import { fetcher, 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
|
||||
}
|
||||
}
|
||||
export function useAPIRoomCategories() {
|
||||
const { data, error, isLoading } = useSWR<RoomCategory[]>('/api/room/category', fetcherUnwraper)
|
||||
|
||||
return {
|
||||
roomCategoryList: data,
|
||||
isLoading,
|
||||
isError: error
|
||||
}
|
||||
}
|
||||
|
||||
export function useAPIRoomCategoryId(categoryId: number) {
|
||||
const url = categoryId ? '/api/room/category/' + categoryId : null;
|
||||
|
||||
const { data, error, isLoading } = useSWR<RoomCategory>(url, fetcher)
|
||||
|
||||
return {
|
||||
roomCategory: data,
|
||||
isLoading,
|
||||
isError: error
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { fetcherUnwraperWithFilter } from "./backend";
|
||||
import useSWR from 'swr';
|
||||
import { AvailabilityFilter, AvailabilityRequest, RoomAvailabity } from "@/model/RoomSearch";
|
||||
|
||||
export function useAPIRoomAvailability(filter: AvailabilityFilter | null) {
|
||||
|
||||
const request: AvailabilityRequest | null = filter ? {
|
||||
filter: filter
|
||||
} : null;
|
||||
|
||||
const { data, isLoading, error } = useSWR(request ? ['/api/search/availability', request] : null, ([url, request]) => fetcherUnwraperWithFilter<RoomAvailabity[]>(url, request))
|
||||
|
||||
return {
|
||||
roomAvailabilityList: data,
|
||||
isLoading,
|
||||
isError: error
|
||||
}
|
||||
}
|
||||
+42
-7
@@ -8,13 +8,13 @@ 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>;
|
||||
})
|
||||
.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> {
|
||||
@@ -28,4 +28,39 @@ export function fetcherUnwraper<T>(endpoint: string): Promise<T> {
|
||||
.then(data => {
|
||||
return data.results
|
||||
})
|
||||
}
|
||||
|
||||
export function fetcherWithFilter<T>(endpoint: string, body: NonNullable<unknown>): Promise<T> {
|
||||
return fetch(url + endpoint, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText)
|
||||
}
|
||||
return response.json() as Promise<T>
|
||||
})
|
||||
}
|
||||
|
||||
export function fetcherUnwraperWithFilter<T>(endpoint: string, body: NonNullable<unknown>): Promise<T> {
|
||||
return fetch(url + endpoint, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
})
|
||||
.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