Add API client and wire up live data fetching
All checks were successful
Build frontend / build (push) Successful in 43s
All checks were successful
Build frontend / build (push) Successful in 43s
- Created app/utils/api.js with functions for all OpenAPI endpoints - Updated main page to fetch RentProperties + SaleProperties from API - Updated properties listing page with API integration - Updated property detail page to fetch by ID from API - Added mapApiProperty() adapter to transform API responses to UI format - All pages gracefully fall back to dummy data if API is unavailable
This commit is contained in:
104
app/utils/api.js
Normal file
104
app/utils/api.js
Normal file
@ -0,0 +1,104 @@
|
||||
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');
|
||||
}
|
||||
Reference in New Issue
Block a user