@@ -28,15 +28,29 @@ import CloudDownloadIcon from '@mui/icons-material/CloudDownload'
28
28
import { Statistic } from '@shared/types'
29
29
30
30
const Statistics = ( ) => {
31
- const [ from , setFrom ] = useState ( 1 )
32
- const [ to , setTo ] = useState ( 4 )
31
+ const [ from , setFrom ] = useState < number | null > ( null )
32
+ const [ to , setTo ] = useState < number | null > ( null )
33
33
const [ selectedFaculty , setFaculties ] = useState ( 'H00' )
34
34
const { data : statistics , isSuccess } = useStatistics ( )
35
35
const { t, i18n } = useTranslation ( )
36
36
const { language } = i18n
37
37
const { user, isLoading : isUserLoading } = useCurrentUser ( )
38
38
const dataDownloadLink = useRef < HTMLAnchorElement | null > ( null )
39
39
40
+ useEffect ( ( ) => {
41
+ if ( isSuccess ) {
42
+ // sets the default to be the current year, the highest id is the end of the current year and the one below that is the start of current year
43
+ const statisticsSortedById = statistics . terms . sort ( ( a , b , ) => b . id - a . id )
44
+ if ( statisticsSortedById . length >= 2 ) {
45
+ const fromId = statisticsSortedById [ 1 ]
46
+ const toId = statisticsSortedById [ 0 ]
47
+ setFrom ( fromId . id )
48
+ setTo ( toId . id )
49
+ }
50
+ console . log ( statisticsSortedById )
51
+ }
52
+ } , [ isSuccess ] )
53
+
40
54
if ( ! isSuccess || isUserLoading ) return null
41
55
42
56
const namesOf = ( codes : string [ ] ) => {
@@ -46,7 +60,13 @@ const Statistics = () => {
46
60
return codes . map ( ( c ) => ( programme [ c ] ? programme [ c ] [ language ] : c ) ) . join ( ', ' )
47
61
}
48
62
49
- const selectedTerms = Array . from ( { length : to - from + 1 } , ( _ , i ) => i + from )
63
+ const selectTerms = ( ) => {
64
+ if ( ! to || ! from ) {
65
+ return [ ]
66
+ }
67
+ return Array . from ( { length : to - from + 1 } , ( _ , i ) => i + from )
68
+ }
69
+ const selectedTerms = selectTerms ( )
50
70
51
71
const byUsage = ( a , b ) => b . usedTokens - a . usedTokens
52
72
@@ -106,26 +126,33 @@ const Statistics = () => {
106
126
} )
107
127
exportToCSV ( mangledStatistics )
108
128
}
109
-
129
+
110
130
111
131
const handleToChange = ( e ) => {
112
132
// in case of: from: 2026 and to 2024, lets change to into from
113
133
const newVal = parseInt ( e . target . value as string , 10 )
114
- if ( from > newVal ) {
134
+ if ( from && from > newVal ) {
115
135
setTo ( from )
116
136
} else {
117
137
setTo ( newVal )
118
138
}
119
139
}
120
140
const handleFromChange = ( e ) => {
121
141
const newVal = parseInt ( e . target . value as string , 10 ) // in case of: from: 2026 and to 2024, lets change to into from
122
- if ( newVal > to ) {
142
+ if ( to && newVal > to ) {
123
143
setTo ( newVal )
124
144
}
125
145
setFrom ( newVal )
126
146
}
127
147
128
-
148
+ const readTermFilter = ( term ) => {
149
+ if ( term ) {
150
+ return term
151
+ }
152
+ else {
153
+ return 0
154
+ }
155
+ }
129
156
return (
130
157
< Container sx = { { mt : '4rem' , mb : '10rem' } } maxWidth = "xl" >
131
158
< Box my = { 2 } >
@@ -144,7 +171,7 @@ const handleFromChange = (e) => {
144
171
< span style = { { margin : 10 } } > { t ( 'stats:timePeriodStop' ) } </ span >
145
172
< Select value = { to } onChange = { handleToChange } >
146
173
{ statistics . terms
147
- . filter ( ( trm ) => trm . id >= from )
174
+ . filter ( ( trm ) => trm . id >= readTermFilter ( from ) )
148
175
. map ( ( term ) => (
149
176
< MenuItem key = { term . id } value = { term . id } >
150
177
{ term . label [ language ] }
0 commit comments