import { useState } from 'react' import { useNavigate } from 'react-router' import { Alert, Box, Button, Chip, CircularProgress, Dialog, DialogActions, DialogContent, DialogTitle, IconButton, Switch, Tooltip, Typography, } from '@mui/material' import { Plus, Trash2 } from 'lucide-react' import { useProperties, useUpdateProperty, useRemoveProperty } from '../../hooks/useProperties' import type { Property } from '../../domain/property' const ASSET_LABELS: Record = { OFFICE: 'Büro', RETAIL: 'Einzelhandel', LIGHT_INDUSTRIAL: 'Gewerbe', LOGISTICS: 'Logistik', PRODUCTION: 'Produktion', MIXED: 'Gemischt', } function formatDate(iso?: string) { if (!iso) return '–' return new Date(iso).toLocaleDateString('de-CH', { day: '2-digit', month: '2-digit', year: 'numeric' }) } export default function MyListings() { const navigate = useNavigate() const { data: listings = [], isLoading } = useProperties({ sourceType: 'DIRECT' }) const updateProperty = useUpdateProperty() const removeProperty = useRemoveProperty() const [confirmDelete, setConfirmDelete] = useState(null) const [actionError, setActionError] = useState(null) function handleToggleStatus(p: Property) { const next = p.status === 'ACTIVE' ? 'INACTIVE' : 'ACTIVE' updateProperty.mutate( { id: p.id, input: { status: next } }, { onError: () => { setActionError('Status konnte nicht geändert werden.') }, }, ) } function handleDelete(p: Property) { removeProperty.mutate(p.id, { onSuccess: () => { setConfirmDelete(null) }, onError: () => { setActionError('Inserat konnte nicht gelöscht werden.') setConfirmDelete(null) }, }) } return ( Meine Inserate Direkt erstellte Inserate — unabhängig vom Portfolio {actionError && ( setActionError(null)}> {actionError} )} {isLoading ? ( ) : listings.length === 0 ? ( Noch keine Inserate Erstellen Sie Ihr erstes direktes Inserat — ohne vollständiges Objekt im Portfolio. ) : ( {/* Header */} {['Inserat', 'Ort', 'Fläche', 'Preis/m²/J', 'Erstellt', 'Aktiv', ''].map(h => ( {h} ))} {listings.map((p, i) => { const isLast = i === listings.length - 1 return ( {/* Title + type */} {p.title} {/* City */} {p.location.city} {/* Area */} {p.areaSqm.toLocaleString('de-CH')} m² {/* Rent */} CHF {p.rentPricePerSqm.toLocaleString('de-CH')} {/* Created */} {formatDate(p.createdAt)} {/* Status toggle */} handleToggleStatus(p)} disabled={updateProperty.isPending} sx={{ '& .MuiSwitch-thumb': { width: 14, height: 14 } }} /> {/* Delete */} setConfirmDelete(p)} disabled={removeProperty.isPending} sx={{ color: '#94a3b8', '&:hover': { color: '#ef4444' } }} > ) })} )} {/* Delete confirmation dialog */} setConfirmDelete(null)} maxWidth="xs" fullWidth> Inserat löschen? {confirmDelete?.title} wird unwiderruflich gelöscht. ) }