Skip to content

Commit a5aa25f

Browse files
setting the defaults to current year
1 parent 17d7195 commit a5aa25f

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/client/components/Statistics.tsx

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,29 @@ import CloudDownloadIcon from '@mui/icons-material/CloudDownload'
2828
import { Statistic } from '@shared/types'
2929

3030
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)
3333
const [selectedFaculty, setFaculties] = useState('H00')
3434
const { data: statistics, isSuccess } = useStatistics()
3535
const { t, i18n } = useTranslation()
3636
const { language } = i18n
3737
const { user, isLoading: isUserLoading } = useCurrentUser()
3838
const dataDownloadLink = useRef<HTMLAnchorElement | null>(null)
3939

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+
4054
if (!isSuccess || isUserLoading) return null
4155

4256
const namesOf = (codes: string[]) => {
@@ -46,7 +60,13 @@ const Statistics = () => {
4660
return codes.map((c) => (programme[c] ? programme[c][language] : c)).join(', ')
4761
}
4862

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()
5070

5171
const byUsage = (a, b) => b.usedTokens - a.usedTokens
5272

@@ -106,26 +126,33 @@ const Statistics = () => {
106126
})
107127
exportToCSV(mangledStatistics)
108128
}
109-
129+
110130

111131
const handleToChange = (e) => {
112132
// in case of: from: 2026 and to 2024, lets change to into from
113133
const newVal = parseInt(e.target.value as string, 10)
114-
if(from > newVal){
134+
if(from && from > newVal){
115135
setTo(from)
116136
}else{
117137
setTo(newVal)
118138
}
119139
}
120140
const handleFromChange = (e) => {
121141
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){
123143
setTo(newVal)
124144
}
125145
setFrom(newVal)
126146
}
127147

128-
148+
const readTermFilter = (term) => {
149+
if(term){
150+
return term
151+
}
152+
else{
153+
return 0
154+
}
155+
}
129156
return (
130157
<Container sx={{ mt: '4rem', mb: '10rem' }} maxWidth="xl">
131158
<Box my={2}>
@@ -144,7 +171,7 @@ const handleFromChange = (e) => {
144171
<span style={{ margin: 10 }}>{t('stats:timePeriodStop')}</span>
145172
<Select value={to} onChange={handleToChange}>
146173
{statistics.terms
147-
.filter((trm) => trm.id >= from)
174+
.filter((trm) => trm.id >= readTermFilter(from))
148175
.map((term) => (
149176
<MenuItem key={term.id} value={term.id}>
150177
{term.label[language]}

0 commit comments

Comments
 (0)