diff --git a/app/owner/properties/page.js b/app/owner/properties/page.js index f885fdc..63302cc 100644 --- a/app/owner/properties/page.js +++ b/app/owner/properties/page.js @@ -46,6 +46,7 @@ import { } from 'lucide-react'; import toast, { Toaster } from 'react-hot-toast'; import AuthService from '../../services/AuthService'; +import { getMyRentListings } from '../../utils/api'; const DeleteConfirmationModal = ({ isOpen, onClose, onConfirm, propertyTitle }) => { if (!isOpen) return null; @@ -708,56 +709,65 @@ export default function OwnerPropertiesPage() { - const loadProperties = () => { - const storedProperties = localStorage.getItem('ownerProperties'); - if (storedProperties) { - setProperties(JSON.parse(storedProperties)); - } else { - const mockProperties = [ - { - id: 1, - title: 'فيلا فاخرة في المزة', - propertyType: 'villa', - purpose: 'rent', - rentType: 'both', - dailyPrice: 500000, - monthlyPrice: 15000000, - location: 'دمشق، المزة', - bedrooms: 5, - bathrooms: 4, - area: 450, - livingRooms: 3, - status: 'available', - images: ['/villa1.jpg'], - createdAt: new Date().toISOString(), - furnished: true, - description: 'فيلا فاخرة مع حديقة خاصة ومسبح', - address: 'شارع المزة - فيلات غربية', - city: 'دمشق', - district: 'المزة', - services: { - electricity: true, - internet: true, - heating: true, - water: true, - airConditioning: true, - parking: true, - elevator: false - }, - terms: { - noSmoking: true, - noPets: false, - noParties: true, - noAlcohol: false, - suitableForChildren: true, - suitableForElderly: true - } - } - ]; - setProperties(mockProperties); - localStorage.setItem('ownerProperties', JSON.stringify(mockProperties)); + const loadProperties = async () => { + const authUser = AuthService.getUser(); + const userId = authUser?.id; + + if (!userId) { + console.warn('[OwnerProperties] No user ID found'); + setIsLoading(false); + return; + } + + try { + console.log('[OwnerProperties] Fetching listings for user:', userId); + const data = await getMyRentListings(userId); + const list = Array.isArray(data) ? data : (data ? [data] : []); + console.log('[OwnerProperties] API returned:', list.length, 'properties'); + + const mapped = list.map((item) => { + const info = item.propertyInformation || {}; + const details = (() => { + try { return JSON.parse(info.detailsJSON || '{}'); } catch { return {}; } + })(); + + return { + id: item.id, + title: info.address || `عقار #${item.id}`, + propertyType: { 0: 'apartment', 1: 'villa', 2: 'house' }[info.buildingType] || 'apartment', + purpose: 'rent', + rentType: { 0: 'daily', 1: 'weekly', 2: 'monthly' }[item.rentType] || 'daily', + dailyPrice: item.dailyRent || 0, + monthlyPrice: item.monthlyRent || 0, + deposit: item.deposit || 0, + location: info.address || '', + bedrooms: info.numberOfBedRooms || 0, + bathrooms: info.numberOfBathRooms || 0, + area: info.space || 0, + livingRooms: details.livingRooms || 0, + status: { 0: 'available', 1: 'booked', 2: 'maintenance' }[info.status] || 'available', + images: ['/property-placeholder.jpg'], + createdAt: item.createdAt || new Date().toISOString(), + furnished: details.furnished || false, + description: info.description || '', + address: info.address || '', + city: '', + district: '', + services: details.services || {}, + terms: details.terms || {}, + rating: item.rating || 0, + currencyId: item.currencyId, + _raw: item, + }; + }); + + setProperties(mapped); + } catch (err) { + console.error('[OwnerProperties] Failed to load properties:', err); + toast.error('فشل في تحميل العقارات'); + } finally { + setIsLoading(false); } - setIsLoading(false); }; const updatePropertiesInStorage = (newProperties) => { diff --git a/app/utils/api.js b/app/utils/api.js index 20e2ebd..d7f6866 100644 --- a/app/utils/api.js +++ b/app/utils/api.js @@ -177,6 +177,11 @@ export async function getOwnerByUserId(userId) { // ─── Properties ─── +export async function getMyRentListings(userId) { + console.log('[API] Fetching my rent listings for user:', userId); + return apiFetch(`/RentProperties/GetMyRentListings/${userId}`); +} + export async function addRentProperty(data) { console.log('[API] Adding rent property:', data.PropertyInformation?.Address); return apiFetch('/RentProperties/AddRentProperty', {