1- 'use client' ;
2-
31import { useState , useCallback } from 'react' ;
42import { deleteHackathon } from '@/lib/api/hackathons' ;
53import { deleteDraft } from '@/lib/api/hackathons/draft' ;
@@ -14,36 +12,43 @@ interface UseDeleteHackathonOptions {
1412 onError ?: ( error : string ) => void ;
1513}
1614
17- export function useDeleteHackathon ( {
15+ /**
16+ * Hook to handle hackathon and draft deletion with proper API routing and feedback.
17+ */
18+ export const useDeleteHackathon = ( {
1819 organizationId,
1920 hackathonId,
2021 isDraft = false ,
2122 onSuccess,
2223 onError,
23- } : UseDeleteHackathonOptions ) {
24+ } : UseDeleteHackathonOptions ) => {
2425 const { isAuthenticated } = useAuthStatus ( ) ;
2526 const [ isDeleting , setIsDeleting ] = useState ( false ) ;
2627 const [ error , setError ] = useState < string | null > ( null ) ;
2728
2829 const deleteHackathonAction = useCallback ( async ( ) => {
2930 const targetLabel = isDraft ? 'draft' : 'hackathon' ;
30-
31+
3132 if ( ! isAuthenticated ) {
3233 toast . error ( `Please sign in to delete ${ targetLabel } s` ) ;
3334 throw new Error ( 'Authentication required' ) ;
3435 }
3536
36- if ( ! organizationId || ! hackathonId ) {
37- const idError = `Organization ID and ${ isDraft ? 'Draft' : 'Hackathon' } ID are required` ;
38- toast . error ( idError ) ;
39- throw new Error ( idError ) ;
37+ if ( ! hackathonId ) {
38+ toast . error ( 'Hackathon ID is required' ) ;
39+ throw new Error ( 'Hackathon ID is required' ) ;
40+ }
41+
42+ if ( isDraft && ! organizationId ) {
43+ toast . error ( 'Organization ID is required for draft deletion' ) ;
44+ throw new Error ( 'Organization ID is required for draft deletion' ) ;
4045 }
4146
4247 setIsDeleting ( true ) ;
4348 setError ( null ) ;
4449
4550 try {
46- const response = isDraft
51+ const response = isDraft
4752 ? await deleteDraft ( organizationId , hackathonId )
4853 : await deleteHackathon ( hackathonId ) ;
4954
@@ -58,6 +63,7 @@ export function useDeleteHackathon({
5863 const errorMessage =
5964 err instanceof Error ? err . message : `Failed to delete ${ targetLabel } ` ;
6065 setError ( errorMessage ) ;
66+ console . error ( `Error deleting ${ targetLabel } :` , err ) ;
6167 toast . error ( errorMessage ) ;
6268 onError ?.( errorMessage ) ;
6369 throw err ;
@@ -71,4 +77,4 @@ export function useDeleteHackathon({
7177 error,
7278 deleteHackathon : deleteHackathonAction ,
7379 } ;
74- }
80+ } ;
0 commit comments