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';
|
import { getRentProperties, getSaleProperties } from './utils/api';
|
||||||
|
|
||||||
// Map API property data to the format the UI expects
|
// Map API property data to the format the UI expects
|
||||||
// API returns { propInfoId, deposit, monthlyRent, dailyRent, rating, ... }
|
// API returns { propertyInformationId, deposit, monthlyRent, dailyRent, rating, propertyInformation: {...}, ... }
|
||||||
// If propertyInformation is nested, use it; otherwise use flat response with defaults
|
|
||||||
function mapApiProperty(item, index) {
|
function mapApiProperty(item, index) {
|
||||||
const info = item.propertyInformation || {};
|
const info = item.propertyInformation || {};
|
||||||
const hasNestedInfo = !!item.propertyInformation;
|
|
||||||
|
|
||||||
const dailyPrice = item.dailyRent ?? item.monthlyRent ?? item.price ?? 0;
|
const dailyPrice = item.dailyRent ?? item.monthlyRent ?? item.price ?? 0;
|
||||||
const monthlyPrice = item.monthlyRent ?? 0;
|
const monthlyPrice = item.monthlyRent ?? 0;
|
||||||
@ -57,7 +55,6 @@ function mapApiProperty(item, index) {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
id: item.id ?? index + 1,
|
id: item.id ?? index + 1,
|
||||||
propInfoId: item.propInfoId,
|
|
||||||
title: info.address || `عقار #${item.id || index + 1}`,
|
title: info.address || `عقار #${item.id || index + 1}`,
|
||||||
description: info.description || '',
|
description: info.description || '',
|
||||||
type: propType,
|
type: propType,
|
||||||
|
|||||||
@ -36,7 +36,6 @@ import { getRentProperties, getSaleProperties } from '../utils/api';
|
|||||||
// Map API data to UI format
|
// Map API data to UI format
|
||||||
function mapApiProperty(item, index) {
|
function mapApiProperty(item, index) {
|
||||||
const info = item.propertyInformation || {};
|
const info = item.propertyInformation || {};
|
||||||
const hasNestedInfo = !!item.propertyInformation;
|
|
||||||
|
|
||||||
const dailyPrice = item.dailyRent ?? item.monthlyRent ?? item.price ?? 0;
|
const dailyPrice = item.dailyRent ?? item.monthlyRent ?? item.price ?? 0;
|
||||||
const monthlyPrice = item.monthlyRent ?? 0;
|
const monthlyPrice = item.monthlyRent ?? 0;
|
||||||
|
|||||||
@ -49,7 +49,6 @@ function mapApiDetail(item) {
|
|||||||
if (!item) return null;
|
if (!item) return null;
|
||||||
|
|
||||||
const info = item.propertyInformation || {};
|
const info = item.propertyInformation || {};
|
||||||
const hasNestedInfo = !!item.propertyInformation;
|
|
||||||
|
|
||||||
const dailyPrice = item.dailyRent ?? item.monthlyRent ?? item.price ?? 0;
|
const dailyPrice = item.dailyRent ?? item.monthlyRent ?? item.price ?? 0;
|
||||||
const monthlyPrice = item.monthlyRent ?? 0;
|
const monthlyPrice = item.monthlyRent ?? 0;
|
||||||
|
|||||||
@ -37,39 +37,13 @@ async function apiFetch(endpoint, options = {}) {
|
|||||||
// ─── Rent Properties ───
|
// ─── Rent Properties ───
|
||||||
|
|
||||||
export async function getRentProperties() {
|
export async function getRentProperties() {
|
||||||
const rentList = await apiFetch('/RentProperties/GetRentProperties');
|
return 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getRentProperty(id) {
|
export async function getRentProperty(id) {
|
||||||
const item = await apiFetch(`/RentProperties/GetRentProperties?id=${id}`);
|
const items = await apiFetch('/RentProperties/GetRentProperties');
|
||||||
if (!item) return item;
|
if (!Array.isArray(items)) return items;
|
||||||
|
return items.find(p => p.id == id) || items[0];
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getRentPropertyLocations(params = {}) {
|
export async function getRentPropertyLocations(params = {}) {
|
||||||
@ -83,35 +57,13 @@ export async function getRentPropertyLocations(params = {}) {
|
|||||||
// ─── Sale Properties ───
|
// ─── Sale Properties ───
|
||||||
|
|
||||||
export async function getSaleProperties() {
|
export async function getSaleProperties() {
|
||||||
const saleList = await apiFetch('/SaleProperties/GetSaleProperties');
|
return 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getSaleProperty(id) {
|
export async function getSaleProperty(id) {
|
||||||
const item = await apiFetch(`/SaleProperties/GetSaleProperties?id=${id}`);
|
const items = await apiFetch('/SaleProperties/GetSaleProperties');
|
||||||
const property = Array.isArray(item) ? item.find(p => p.id == id) || item[0] : item;
|
if (!Array.isArray(items)) return items;
|
||||||
|
return items.find(p => p.id == id) || items[0];
|
||||||
if (property?.propInfoId) {
|
|
||||||
try {
|
|
||||||
const propInfo = await apiFetch(`/Properties/Get/${property.propInfoId}`);
|
|
||||||
return { ...property, propertyInformation: propInfo };
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return property;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Properties (generic) ───
|
// ─── Properties (generic) ───
|
||||||
|
|||||||
Reference in New Issue
Block a user