Clean up API client - use nested propertyInformation directly
All checks were successful
Build frontend / build (push) Successful in 42s
All checks were successful
Build frontend / build (push) Successful in 42s
- Removed manual enrichment calls (Properties/Get fallback no longer needed) - Removed unused hasNestedInfo variables - API now returns propertyInformation nested in RentProperties/SaleProperties
This commit is contained in:
@ -31,11 +31,9 @@ import Image from 'next/image';
|
||||
import { getRentProperties, getSaleProperties } from './utils/api';
|
||||
|
||||
// Map API property data to the format the UI expects
|
||||
// API returns { propInfoId, deposit, monthlyRent, dailyRent, rating, ... }
|
||||
// If propertyInformation is nested, use it; otherwise use flat response with defaults
|
||||
// API returns { propertyInformationId, deposit, monthlyRent, dailyRent, rating, propertyInformation: {...}, ... }
|
||||
function mapApiProperty(item, index) {
|
||||
const info = item.propertyInformation || {};
|
||||
const hasNestedInfo = !!item.propertyInformation;
|
||||
|
||||
const dailyPrice = item.dailyRent ?? item.monthlyRent ?? item.price ?? 0;
|
||||
const monthlyPrice = item.monthlyRent ?? 0;
|
||||
@ -57,7 +55,6 @@ function mapApiProperty(item, index) {
|
||||
|
||||
return {
|
||||
id: item.id ?? index + 1,
|
||||
propInfoId: item.propInfoId,
|
||||
title: info.address || `عقار #${item.id || index + 1}`,
|
||||
description: info.description || '',
|
||||
type: propType,
|
||||
|
||||
@ -36,7 +36,6 @@ import { getRentProperties, getSaleProperties } from '../utils/api';
|
||||
// Map API data to UI format
|
||||
function mapApiProperty(item, index) {
|
||||
const info = item.propertyInformation || {};
|
||||
const hasNestedInfo = !!item.propertyInformation;
|
||||
|
||||
const dailyPrice = item.dailyRent ?? item.monthlyRent ?? item.price ?? 0;
|
||||
const monthlyPrice = item.monthlyRent ?? 0;
|
||||
|
||||
@ -49,7 +49,6 @@ function mapApiDetail(item) {
|
||||
if (!item) return null;
|
||||
|
||||
const info = item.propertyInformation || {};
|
||||
const hasNestedInfo = !!item.propertyInformation;
|
||||
|
||||
const dailyPrice = item.dailyRent ?? item.monthlyRent ?? item.price ?? 0;
|
||||
const monthlyPrice = item.monthlyRent ?? 0;
|
||||
|
||||
@ -37,39 +37,13 @@ async function apiFetch(endpoint, options = {}) {
|
||||
// ─── Rent Properties ───
|
||||
|
||||
export async function getRentProperties() {
|
||||
const rentList = await apiFetch('/RentProperties/GetRentProperties');
|
||||
if (!Array.isArray(rentList) || rentList.length === 0) return rentList;
|
||||
|
||||
// Fetch property info for each rent property's propInfoId
|
||||
const enriched = await Promise.all(
|
||||
rentList.map(async (item) => {
|
||||
try {
|
||||
const propInfo = await apiFetch(`/Properties/Get/${item.propInfoId}`);
|
||||
return { ...item, propertyInformation: propInfo };
|
||||
} catch {
|
||||
return item;
|
||||
}
|
||||
})
|
||||
);
|
||||
return enriched;
|
||||
return apiFetch('/RentProperties/GetRentProperties');
|
||||
}
|
||||
|
||||
export async function getRentProperty(id) {
|
||||
const item = await apiFetch(`/RentProperties/GetRentProperties?id=${id}`);
|
||||
if (!item) return item;
|
||||
|
||||
// If it's an array (all results), pick the matching one
|
||||
const property = Array.isArray(item) ? item.find(p => p.id == id) || item[0] : item;
|
||||
|
||||
if (property?.propInfoId) {
|
||||
try {
|
||||
const propInfo = await apiFetch(`/Properties/Get/${property.propInfoId}`);
|
||||
return { ...property, propertyInformation: propInfo };
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return property;
|
||||
const items = await apiFetch('/RentProperties/GetRentProperties');
|
||||
if (!Array.isArray(items)) return items;
|
||||
return items.find(p => p.id == id) || items[0];
|
||||
}
|
||||
|
||||
export async function getRentPropertyLocations(params = {}) {
|
||||
@ -83,35 +57,13 @@ export async function getRentPropertyLocations(params = {}) {
|
||||
// ─── Sale Properties ───
|
||||
|
||||
export async function getSaleProperties() {
|
||||
const saleList = await apiFetch('/SaleProperties/GetSaleProperties');
|
||||
if (!Array.isArray(saleList) || saleList.length === 0) return saleList;
|
||||
|
||||
const enriched = await Promise.all(
|
||||
saleList.map(async (item) => {
|
||||
try {
|
||||
const propInfo = await apiFetch(`/Properties/Get/${item.propInfoId}`);
|
||||
return { ...item, propertyInformation: propInfo };
|
||||
} catch {
|
||||
return item;
|
||||
}
|
||||
})
|
||||
);
|
||||
return enriched;
|
||||
return apiFetch('/SaleProperties/GetSaleProperties');
|
||||
}
|
||||
|
||||
export async function getSaleProperty(id) {
|
||||
const item = await apiFetch(`/SaleProperties/GetSaleProperties?id=${id}`);
|
||||
const property = Array.isArray(item) ? item.find(p => p.id == id) || item[0] : item;
|
||||
|
||||
if (property?.propInfoId) {
|
||||
try {
|
||||
const propInfo = await apiFetch(`/Properties/Get/${property.propInfoId}`);
|
||||
return { ...property, propertyInformation: propInfo };
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return property;
|
||||
const items = await apiFetch('/SaleProperties/GetSaleProperties');
|
||||
if (!Array.isArray(items)) return items;
|
||||
return items.find(p => p.id == id) || items[0];
|
||||
}
|
||||
|
||||
// ─── Properties (generic) ───
|
||||
|
||||
Reference in New Issue
Block a user