integratedhub/islands/DeleteLeaseButton.tsx

57 lines
1.5 KiB
TypeScript

// islands/DeleteLeaseButton.tsx
import { useState } from "preact/hooks";
interface DeleteLeaseButtonProps {
leaseId: string;
propertyAddress: string;
tenantName?: string;
}
export default function DeleteLeaseButton({ leaseId, propertyAddress, tenantName }: DeleteLeaseButtonProps) {
const [isDeleting, setIsDeleting] = useState(false);
const handleDelete = async () => {
const tenantInfo = tenantName ? ` for ${tenantName}` : "";
const confirmed = confirm(
`Are you sure you want to delete the lease for "${propertyAddress}"${tenantInfo}? This action cannot be undone.`
);
if (!confirmed) return;
setIsDeleting(true);
try {
const formData = new FormData();
formData.append('lease_id', leaseId);
formData.append('sender', globalThis.location.pathname);
const response = await fetch('/api/delete/lease', {
method: 'POST',
body: formData
});
if (response.redirected) {
globalThis.location.href = response.url;
} else if (response.ok) {
globalThis.location.reload();
} else {
alert('Failed to delete lease. Please try again.');
}
} catch (_error) {
alert('Error deleting lease');
} 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>
);
}