1
1
import { useState , useEffect } from "react" ;
2
- import { useState , useEffect } from "react" ;
3
2
import { Header } from "./header" ;
4
3
import { Sidebar } from "./sidebar" ;
5
4
import { DataRibbon } from "./data-ribbon" ;
6
5
import { ChartSection } from "./chart-section" ;
7
- import { DataRibbonStatsFC , ChartDataFC } from "@/helpers/analytics-dashboard/types" ;
8
- import { getRibbonStats } from "@/helpers/analytics-dashboard/api" ;
6
+ import { SeasonDataFC } from "@/helpers/analytics-dashboard/types" ;
7
+ import { getSeasonStats } from "@/helpers/analytics-dashboard/api" ;
9
8
import Loading from "../common/loading" ;
10
9
import axios from "axios" ;
11
10
@@ -15,51 +14,62 @@ export default function Dashboard() {
15
14
const [ currentView , setCurrentView ] = useState < "reports" | "trends" > ( "reports" ) ;
16
15
const [ season , setSeason ] = useState < string > ( "" ) ;
17
16
const [ yearRange , setYearRange ] = useState < [ number , number ] > ( [ new Date ( ) . getFullYear ( ) - 5 , new Date ( ) . getFullYear ( ) ] ) ;
18
- const [ ribbonStats , setRibbonStats ] = useState < DataRibbonStatsFC | null > ( null )
19
- const [ chartData , setChartData ] = useState < ChartDataFC | null > ( null )
20
- const [ isLoading , setIsLoading ] = useState ( false )
17
+ const [ seasonData , setSeasonData ] = useState < SeasonDataFC | null > ( null )
18
+ const [ isLoading , setIsLoading ] = useState ( true )
21
19
const [ error , setError ] = useState < string | null > ( null )
22
20
23
21
const [ optionsx , setOptionsx ] = useState ( [ ] ) ;
24
22
let options : any = [ ] ;
25
23
useEffect ( ( ) => {
26
24
axios . get ( `${ baseUrl } /api/v1/jaf` ) . then ( ( res ) => {
27
- res . data . seasons . map ( ( season : any ) => {
28
- const seasonString = `${ season . type } ${ season . year } `
29
- options . push ( { value : season . id , label : seasonString } ) ;
25
+ const newOptions = res . data . seasons . map ( ( season : any ) => {
26
+ const seasonString = `${ season . type } ${ season . year } ` ;
27
+ return { value : season . id , label : seasonString , type : season . type , year : season . year } ;
30
28
} ) ;
31
- setOptionsx ( options ) ;
32
- if ( options . length > 0 ) {
33
- setSeason ( options [ 0 ] . value ) ;
34
- }
29
+
30
+ // Sort the options
31
+ newOptions . sort ( ( a : any , b : any ) => {
32
+ if ( a . year !== b . year ) {
33
+ return b . year - a . year ;
34
+ }
35
+ if ( a . type === b . type ) {
36
+ return 0 ;
37
+ }
38
+ return a . type === "Placement" ? - 1 : 1 ;
39
+ } ) ;
40
+
41
+ setOptionsx ( newOptions ) ;
42
+ if ( newOptions . length > 0 ) {
43
+ setSeason ( newOptions [ 0 ] . value ) ;
44
+ }
45
+ setIsLoading ( false ) ;
35
46
} )
36
47
. catch ( ( err ) => {
37
48
console . log ( err ) ;
38
- }
39
- ) ;
49
+ } ) ;
40
50
} , [ ] ) ;
41
51
42
- // useEffect(() => {
43
- // async function fetchSeasonData() {
44
- // setIsLoading(true)
45
- // try {
46
- // const { ribbonStats, chartData } = await getRibbonStats({
47
- // year: seasonYear,
48
- // type: seasonType
49
- // })
50
-
51
- // setRibbonStats(ribbonStats)
52
- // setError(null)
53
- // } catch (err) {
54
- // setError('Failed to load season data')
55
- // console.error(err)
56
- // } finally {
57
- // setIsLoading(false)
58
- // }
59
- // }
52
+ useEffect ( ( ) => {
53
+ async function fetchSeasonData ( ) {
54
+ setIsLoading ( true )
55
+ try {
56
+ let data = await getSeasonStats ( season ) ;
57
+ setSeasonData ( data ) ;
58
+ console . log ( seasonData ) ;
59
+ setError ( null ) ;
60
+ } catch ( err ) {
61
+ setError ( 'Failed to load season data' )
62
+ console . error ( err )
63
+ } finally {
64
+ setIsLoading ( false )
65
+ }
66
+ }
67
+ if ( season !== "" ) {
68
+ fetchSeasonData ( ) ;
69
+ }
70
+ } , [ season ] )
60
71
61
- // fetchSeasonData()
62
- // }, [seasonYear, seasonType])
72
+
63
73
64
74
if ( isLoading ) {
65
75
return Loading ( ) ;
@@ -74,7 +84,7 @@ export default function Dashboard() {
74
84
< Header currentView = { currentView } onViewChange = { setCurrentView } />
75
85
< div className = "flex h-screen overflow-y-auto bg-background" >
76
86
< main className = "flex-1 overflow-y-auto p-6 no-scrollbar" >
77
- { /* <DataRibbon stats={ribbonStats } /> */ }
87
+ { /* <DataRibbon stats={seasonData } /> */ }
78
88
< ChartSection />
79
89
</ main >
80
90
< Sidebar
@@ -88,5 +98,4 @@ export default function Dashboard() {
88
98
</ div >
89
99
</ div >
90
100
)
91
- )
92
101
}
0 commit comments