@@ -14,23 +14,37 @@ import {
1414} from 'lucide-react' ;
1515import OptimizedDashboardService from '../../utils/api/optimizedDashboardService' ;
1616import ProgressService from '../../utils/api/progressService' ;
17+ import DataService from '../../utils/api/dataService' ;
1718import { PageHead } from "../../components/common" ;
1819import { MotionDiv , MotionSection } from '../../components/common/MotionWrapper' ;
1920import { useDashboard } from '../../hooks/useDashboard' ;
2021
2122const LeaguesPage = ( ) => {
2223 const navigate = useNavigate ( ) ;
24+ const [ cohorts , setCohorts ] = useState ( [ ] ) ;
2325
2426 // SWR: Automatic caching, revalidation, and state management
2527 const {
2628 dashboardData,
2729 leagues,
2830 basicLeagueStats : leagueStatistics ,
2931 isLoading : loading ,
30- error : swrError ,
31- mutate : refreshDashboard
32+ error : swrError
3233 } = useDashboard ( ) ;
3334
35+ // Fetch cohorts for enrollment
36+ useEffect ( ( ) => {
37+ const fetchCohorts = async ( ) => {
38+ try {
39+ const data = await DataService . getCohorts ( ) ;
40+ setCohorts ( data . cohorts || [ ] ) ;
41+ } catch ( err ) {
42+ console . error ( 'Failed to fetch cohorts:' , err ) ;
43+ }
44+ } ;
45+ fetchCohorts ( ) ;
46+ } , [ ] ) ;
47+
3448 // Format error message
3549 const error = swrError ? `Failed to load leagues. Please try again later. (${ swrError . message } )` : null ;
3650
@@ -49,14 +63,19 @@ const LeaguesPage = () => {
4963 return ;
5064 }
5165
52- // Get cohort from user's existing enrollments or use first available
53- const cohortId = dashboardData ?. enrollments ?. [ 0 ] ?. cohort ?. id || 'default' ;
66+ // Get cohort: prioritize user's existing enrollment cohort, else use first available cohort
67+ const cohortId = dashboardData ?. enrollments ?. [ 0 ] ?. cohort ?. id || cohorts [ 0 ] ?. id ;
68+
69+ if ( ! cohortId ) {
70+ alert ( 'No cohort available for enrollment. Please contact support.' ) ;
71+ return ;
72+ }
5473
5574 await ProgressService . enrollUser ( cohortId , leagueId ) ;
5675 alert ( 'Enrollment successful! Welcome to your learning journey!' ) ;
5776
58- // SWR: Trigger revalidation to refresh data
59- await refreshDashboard ( ) ;
77+ // Reload the page to refresh all data for newly enrolled user
78+ window . location . reload ( ) ;
6079 } catch ( err ) {
6180 console . error ( 'Enrollment error:' , err ) ;
6281 alert ( `Enrollment failed: ${ err . message } . Please try again.` ) ;
@@ -167,7 +186,7 @@ const LeaguesPage = () => {
167186 const stats = enrollment ?. progress || leagueStatistics [ league . id ] || { } ;
168187 const weeksCount = stats . weeksCount || league . weeksCount || league . _count ?. weeks || 0 ;
169188 const sectionsCount = stats . totalSections || league . sectionsCount || league . _count ?. sections || 0 ;
170- const resourcesCount = stats . totalResources || league . totalResources || league . _count ?. resources || 0 ;
189+ const resourcesCount = stats . totalResources || league . resourcesCount || league . _count ?. resources || 0 ;
171190
172191 return (
173192 < MotionDiv
0 commit comments