105 lines
2.7 KiB
JavaScript
105 lines
2.7 KiB
JavaScript
|
|
const API_BASE = process.env.NEXT_PUBLIC_API_URL || 'http://45.93.137.91/api';
|
||
|
|
|
||
|
|
async function apiFetch(endpoint, options = {}) {
|
||
|
|
const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null;
|
||
|
|
|
||
|
|
const headers = {
|
||
|
|
'Content-Type': 'application/json',
|
||
|
|
...(token && { Authorization: `Bearer ${token}` }),
|
||
|
|
...options.headers,
|
||
|
|
};
|
||
|
|
|
||
|
|
const res = await fetch(`${API_BASE}${endpoint}`, {
|
||
|
|
...options,
|
||
|
|
headers,
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!res.ok) {
|
||
|
|
const text = await res.text().catch(() => '');
|
||
|
|
throw new Error(`API ${res.status}: ${text || res.statusText}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Some endpoints return empty body
|
||
|
|
const text = await res.text();
|
||
|
|
return text ? JSON.parse(text) : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// ─── Rent Properties ───
|
||
|
|
|
||
|
|
export async function getRentProperties() {
|
||
|
|
return apiFetch('/RentProperties');
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function getRentProperty(id) {
|
||
|
|
return apiFetch(`/RentProperties/${id}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function getRentPropertyLocations(params = {}) {
|
||
|
|
const qs = new URLSearchParams();
|
||
|
|
if (params.maxOffset != null) qs.set('maxOffset', params.maxOffset);
|
||
|
|
if (params.minOffset != null) qs.set('minOffset', params.minOffset);
|
||
|
|
const query = qs.toString();
|
||
|
|
return apiFetch(`/RentProperties/locations${query ? `?${query}` : ''}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ─── Sale Properties ───
|
||
|
|
|
||
|
|
export async function getSaleProperties() {
|
||
|
|
return apiFetch('/SaleProperties');
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function getSaleProperty(id) {
|
||
|
|
return apiFetch(`/SaleProperties/${id}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function getSalePropertiesPaginated(page = 1, pageSize = 10) {
|
||
|
|
return apiFetch(`/SaleProperties/paginated?pageNumber=${page}&pageSize=${pageSize}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ─── Properties (generic) ───
|
||
|
|
|
||
|
|
export async function getProperty(id) {
|
||
|
|
return apiFetch(`/Properties/Get/${id}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ─── Recommendations ───
|
||
|
|
|
||
|
|
export async function getRecommendations() {
|
||
|
|
return apiFetch('/Recommendations');
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function getTopRecommendations(count = 10) {
|
||
|
|
return apiFetch(`/Recommendations/top/${count}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ─── Reservations ───
|
||
|
|
|
||
|
|
export async function getReservations() {
|
||
|
|
return apiFetch('/Reservations');
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function getReservation(id) {
|
||
|
|
return apiFetch(`/Reservations/${id}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function checkAvailability(propertyId, fromDate = null, toDate = null) {
|
||
|
|
const qs = new URLSearchParams();
|
||
|
|
if (fromDate) qs.set('fromDate', fromDate);
|
||
|
|
if (toDate) qs.set('toDate', toDate);
|
||
|
|
const query = qs.toString();
|
||
|
|
return apiFetch(`/Reservations/available/${propertyId}${query ? `?${query}` : ''}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function bookReservation(data) {
|
||
|
|
return apiFetch('/Reservations/book', {
|
||
|
|
method: 'POST',
|
||
|
|
body: JSON.stringify(data),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// ─── Terms ───
|
||
|
|
|
||
|
|
export async function getTerms() {
|
||
|
|
return apiFetch('/Terms');
|
||
|
|
}
|