integratedhub/islands/DeleteApplicationButton.tsx

60 lines
1.6 KiB
TypeScript

// islands/DeleteApplicationButton.tsx
import { useState } from "preact/hooks";
interface DeleteApplicationButtonProps {
applicationId: string;
applicantName: string;
applicantEmail: string;
}
export default function DeleteApplicationButton({
applicationId,
applicantName,
applicantEmail
}: DeleteApplicationButtonProps) {
const [isDeleting, setIsDeleting] = useState(false);
const handleDelete = async () => {
const confirmed = confirm(
`Are you sure you want to delete the application from ${applicantName} (${applicantEmail})? This action cannot be undone.`
);
if (!confirmed) return;
setIsDeleting(true);
try {
const formData = new FormData();
formData.append('id', applicationId);
formData.append('sender', globalThis.location.pathname);
const response = await fetch('/api/delete/tenant_application', {
method: 'POST',
body: formData
});
if (response.redirected) {
globalThis.location.href = response.url;
} else if (response.ok) {
globalThis.location.reload();
} else {
alert('Failed to delete application. Please try again.');
}
} catch (_error) {
alert('Error deleting application');
} finally {
setIsDeleting(false);
}
};
return (
<button
type="button"
onClick={handleDelete}
disabled={isDeleting}
class="text-xs bg-red-100 text-red-800 px-2 py-1 rounded hover:bg-red-200 disabled:opacity-50 disabled:cursor-not-allowed"
>
{isDeleting ? 'Deleting...' : 'Delete'}
</button>
);
}