diff --git a/app/(landing)/organizations/[id]/hackathons/page.tsx b/app/(landing)/organizations/[id]/hackathons/page.tsx index ee33ca87f..36bf2f9eb 100644 --- a/app/(landing)/organizations/[id]/hackathons/page.tsx +++ b/app/(landing)/organizations/[id]/hackathons/page.tsx @@ -85,6 +85,7 @@ export default function HackathonsPage() { const [hackathonToDelete, setHackathonToDelete] = useState<{ id: string; title: string; + type: 'draft' | 'hackathon'; } | null>(null); const { hackathons, hackathonsLoading, drafts, draftsLoading, refetchAll } = @@ -96,13 +97,12 @@ export default function HackathonsPage() { // Use the separate delete hook const { isDeleting, deleteHackathon } = useDeleteHackathon({ organizationId, - hackathonId: hackathonToDelete?.id || '', // This will be set when we have a hackathon to delete + hackathonId: hackathonToDelete?.id || '', + type: hackathonToDelete?.type ?? 'hackathon', + suppressToast: true, onSuccess: () => { // Refresh the hackathons list after successful deletion refetchAll(); - toast.success('Hackathon deleted successfully', { - description: `"${hackathonToDelete?.title}" has been permanently deleted.`, - }); }, onError: error => { toast.error('Failed to delete hackathon', { @@ -165,37 +165,47 @@ export default function HackathonsPage() { return { published, drafts: drafts.length, total }; }, [hackathons, drafts]); - const handleDeleteClick = (hackathonId: string) => { - // Try to find in published hackathons - const published = publishedHackathons.find(h => h.id === hackathonId); - if (published) { - setHackathonToDelete({ - id: hackathonId, - title: published.name || 'Untitled Hackathon', - }); - setDeleteDialogOpen(true); - return; - } - // Try to find in drafts - const draft = draftHackathons.find(d => d.id === hackathonId); - if (draft) { - setHackathonToDelete({ - id: hackathonId, - title: draft.data.information?.name || 'Untitled Hackathon', - }); - setDeleteDialogOpen(true); + const handleDeleteClick = ( + hackathonId: string, + type: 'draft' | 'hackathon' + ) => { + if (type === 'hackathon') { + const published = publishedHackathons.find(h => h.id === hackathonId); + if (published) { + setHackathonToDelete({ + id: hackathonId, + title: published.name || 'Untitled Hackathon', + type: 'hackathon', + }); + setDeleteDialogOpen(true); + } + } else { + const draft = draftHackathons.find(d => d.id === hackathonId); + if (draft) { + setHackathonToDelete({ + id: hackathonId, + title: draft.data.information?.name || 'Untitled Hackathon', + type: 'draft', + }); + setDeleteDialogOpen(true); + } } }; const handleDeleteConfirm = async () => { if (!hackathonToDelete) return; + const { title } = hackathonToDelete; // ✅ snapshot before state clears + setDeleteDialogOpen(false); try { await deleteHackathon(); + toast.success('Hackathon deleted successfully', { + description: `"${title}" has been permanently deleted.`, + }); } catch { - // Error handled by toast in deleteHackathon hook + // error toast handled by onError in hook } finally { setHackathonToDelete(null); } @@ -465,7 +475,7 @@ export default function HackathonsPage() {