integratedhub/islands/DeletePropertyButton.tsx

55 lines
1.6 KiB
TypeScript

// islands/DeletePropertyButton.tsx
import { useState } from "preact/hooks";
interface DeletePropertyButtonProps {
propertyId: string;
propertyAddress: string;
}
export default function DeletePropertyButton({ propertyId, propertyAddress }: DeletePropertyButtonProps) {
const [isDeleting, setIsDeleting] = useState(false);
const handleDelete = async () => {
const confirmed = confirm(
`Are you sure you want to delete "${propertyAddress}"? This action cannot be undone and will remove all associated data including leases, applications, and maintenance requests.`
);
if (!confirmed) return;
setIsDeleting(true);
try {
const formData = new FormData();
formData.append('property_id', propertyId);
formData.append('sender', globalThis.location.pathname);
const response = await fetch('/api/delete/property', {
method: 'POST',
body: formData
});
if (response.redirected) {
globalThis.location.href = response.url;
} else if (response.ok) {
globalThis.location.reload();
} else {
alert('Failed to delete property. Please make sure it has no active leases or applications.');
}
} catch (_error) {
alert('Error deleting property');
} finally {
setIsDeleting(false);
}
};
return (
<button
type="button"
onClick={handleDelete}
disabled={isDeleting}
class="text-sm text-red-600 hover:text-red-500 disabled:opacity-50 disabled:cursor-not-allowed"
>
{isDeleting ? 'Deleting...' : 'Delete'}
</button>
);
}