11'use client' ;
22
3- import { useState , useEffect , useMemo } from 'react' ;
3+ import { useState , useEffect , useMemo , useCallback } from 'react' ;
44import { useRouter , useSearchParams , useParams } from 'next/navigation' ;
55import { useHackathonData } from '@/lib/providers/hackathonProvider' ;
66import { useRegisterHackathon } from '@/hooks/hackathon/use-register-hackathon' ;
7+ import { useLeaveHackathon } from '@/hooks/hackathon/use-leave-hackathon' ;
78import { RegisterHackathonModal } from '@/components/hackathons/overview/RegisterHackathonModal' ;
89
910import { HackathonBanner } from '@/components/hackathons/hackathonBanner' ;
@@ -16,6 +17,9 @@ import { HackathonDiscussions } from '@/components/hackathons/discussion/comment
1617import { TeamFormationTab } from '@/components/hackathons/team-formation/TeamFormationTab' ;
1718import LoadingScreen from '@/components/landing-page/project/CreateProjectModal/LoadingScreen' ;
1819import { useTimelineEvents } from '@/hooks/hackathon/use-timeline-events' ;
20+ import { toast } from 'sonner' ;
21+ import { Participant } from '@/lib/api/hackathons' ;
22+ // import { HackathonResources } from '@/components/hackathons/resources/resources';
1923
2024export default function HackathonPage ( ) {
2125 const router = useRouter ( ) ;
@@ -28,14 +32,16 @@ export default function HackathonPage() {
2832 submissions,
2933 loading,
3034 setCurrentHackathon,
35+ refreshCurrentHackathon,
3136 } = useHackathonData ( ) ;
37+
3238 const timeline_Events = useTimelineEvents ( currentHackathon , {
3339 includeEndDate : false ,
3440 dateFormat : { month : 'short' , day : 'numeric' , year : 'numeric' } ,
3541 } ) ;
42+
3643 const hackathonTabs = useMemo ( ( ) => {
3744 const hasParticipants = participants . length > 0 ;
38- // const hasSubmissions = submissions.filter(p => p.status === 'Approved').length > 0;
3945
4046 const tabs = [
4147 { id : 'overview' , label : 'Overview' } ,
@@ -81,19 +87,37 @@ export default function HackathonPage() {
8187 const [ activeTab , setActiveTab ] = useState ( 'overview' ) ;
8288 const [ showRegisterModal , setShowRegisterModal ] = useState ( false ) ;
8389
84- // Registration logic
85- const { isRegistered, hasSubmitted, checkStatus } = useRegisterHackathon ( {
90+ // Function to refresh all hackathon data
91+ const refreshHackathonData = useCallback ( async ( ) => {
92+ if ( hackathonId && refreshCurrentHackathon ) {
93+ await refreshCurrentHackathon ( ) ;
94+ }
95+ } , [ hackathonId , refreshCurrentHackathon ] ) ;
96+
97+ // Registration logic - use the updated hook with setters
98+ const {
99+ isRegistered,
100+ hasSubmitted,
101+ checkStatus,
102+ // isChecking,
103+ setIsRegistered,
104+ setParticipant,
105+ } = useRegisterHackathon ( {
86106 hackathonSlugOrId : hackathonId ,
87- organizationId : undefined , // organizationId not available in currentHackathon type
107+ organizationId : undefined ,
88108 autoCheck : ! ! hackathonId ,
89109 } ) ;
90110
111+ // Leave hackathon logic
112+ const { isLeaving, leave : leaveHackathon } = useLeaveHackathon ( {
113+ hackathonSlugOrId : hackathonId ,
114+ organizationId : undefined ,
115+ } ) ;
116+
91117 const isEnded = useMemo ( ( ) => {
92118 if ( ! currentHackathon ?. deadline ) return false ;
93-
94119 const deadline = new Date ( currentHackathon . deadline ) ;
95120 const now = new Date ( ) ;
96-
97121 return now > deadline ;
98122 } , [ currentHackathon ?. deadline ] ) ;
99123
@@ -111,8 +135,33 @@ export default function HackathonPage() {
111135 setShowRegisterModal ( true ) ;
112136 } ;
113137
114- const handleRegisterSuccess = ( ) => {
115- checkStatus ( ) ;
138+ const handleLeaveClick = async ( ) => {
139+ try {
140+ setIsRegistered ( false ) ;
141+ setParticipant ( null ) ;
142+
143+ await leaveHackathon ( ) ;
144+
145+ refreshHackathonData ( ) ;
146+
147+ router . push ( '?tab=overview' ) ;
148+ } catch ( error ) {
149+ const errorMessage =
150+ error instanceof Error ? error . message : 'Failed to leave hackathon' ;
151+ toast . error ( errorMessage ) ;
152+ setIsRegistered ( true ) ;
153+ checkStatus ( ) ;
154+ }
155+ } ;
156+
157+ const handleRegisterSuccess = async ( participantData : Participant ) => {
158+ // IMMEDIATELY update state with the returned participant data
159+ setIsRegistered ( true ) ;
160+ setParticipant ( participantData ) ;
161+
162+ // Refresh hackathon data in background (participants count)
163+ await refreshHackathonData ( ) ;
164+
116165 router . push ( '?tab=submission' ) ;
117166 } ;
118167
@@ -151,6 +200,7 @@ export default function HackathonPage() {
151200 if ( loading ) {
152201 return < LoadingScreen /> ;
153202 }
203+
154204 if ( ! currentHackathon ) {
155205 return (
156206 < div className = 'flex min-h-screen items-center justify-center' >
@@ -187,6 +237,8 @@ export default function HackathonPage() {
187237 registrationDeadline = { currentHackathon . registrationDeadline }
188238 isTeamFormationEnabled = { isTeamFormationEnabled }
189239 onJoinClick = { handleJoinClick }
240+ onLeaveClick = { handleLeaveClick }
241+ isLeaving = { isLeaving }
190242 onSubmitClick = { handleSubmitClick }
191243 onViewSubmissionClick = { handleViewSubmissionClick }
192244 onFindTeamClick = { handleFindTeamClick }
@@ -199,7 +251,7 @@ export default function HackathonPage() {
199251 onOpenChange = { setShowRegisterModal }
200252 hackathonSlugOrId = { hackathonId }
201253 organizationId = { undefined }
202- onSuccess = { handleRegisterSuccess }
254+ onSuccess = { handleRegisterSuccess } // Now passes participant data
203255 participantType = {
204256 ( currentHackathon ?. participantType as
205257 | 'team'
@@ -232,9 +284,6 @@ export default function HackathonPage() {
232284 { activeTab === 'participants' && participants . length > 0 && (
233285 < HackathonParticipants />
234286 ) }
235- { /* {activeTab === 'resources' && (
236- <HackathonResources hackathonSlugOrId={hackathonId} />
237- )} */ }
238287
239288 { activeTab === 'submission' && (
240289 < SubmissionTab
0 commit comments