Skip to content

Commit

Permalink
Buttons for freezing series and semester (#393)
Browse files Browse the repository at this point in the history
* add button for freezing series and semester

* fix eslint

* Freeze semester and series error handling

---------

Co-authored-by: Michal Masrna <[email protected]>
  • Loading branch information
vikibrezinova and michalmasrna1 authored Nov 22, 2024
1 parent 1cce86f commit 722bbcc
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions src/components/SemesterAdministration/SemesterAdministration.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Stack, Typography} from '@mui/material'
import {useQuery} from '@tanstack/react-query'
import axios from 'axios'
import axios, {AxiosError} from 'axios'
import {FC, useState} from 'react'

import {Button} from '@/components/Clickable/Button'
import {Link} from '@/components/Clickable/Link'
import {SemesterWithProblems} from '@/types/api/generated/competition'
import {SemesterWithProblems, SeriesWithProblems} from '@/types/api/generated/competition'
import {formatDateTime} from '@/utils/formatDate'
import {useDataFromURL} from '@/utils/useDataFromURL'
import {useHasPermissions} from '@/utils/useHasPermissions'
Expand Down Expand Up @@ -84,6 +84,35 @@ export const SemesterAdministration: FC = () => {
)
}

const [semesterFreezeError, setSemesterFreezeError] = useState<string>()
const [seriesFreezeError, setSeriesFreezeError] = useState<string>()

const freezeSemester = async (semester: SemesterWithProblems) => {
setSemesterFreezeError('')
try {
await axios.post(`/api/competition/semester/${semester.id}/results/freeze`)
} catch (error: unknown) {
if (error instanceof AxiosError) {
setSemesterFreezeError(error.response?.data.detail)
} else {
setSemesterFreezeError('Nastala neznáma chyba.')
}
}
}

const freezeSeries = async (series: SeriesWithProblems) => {
setSeriesFreezeError('')
try {
await axios.post(`/api/competition/series/${series.id}/results/freeze`)
} catch (error: unknown) {
if (error instanceof AxiosError) {
setSeriesFreezeError(error.response?.data.detail)
} else {
setSeriesFreezeError('Nastala neznáma chyba.')
}
}
}

if (
urlDataLoading.currentSeriesIsLoading ||
urlDataLoading.semesterListIsLoading ||
Expand All @@ -102,9 +131,21 @@ export const SemesterAdministration: FC = () => {
return (
<>
<Typography variant="h3">Administrácia semestra pre opravovateľov.</Typography>
<Stack mt={3} pl={2} alignItems="start">
<Button variant="button2" onClick={() => freezeSemester(semester)}>
Uzavrieť semester
</Button>
{semesterFreezeError && <Typography variant="body1">{semesterFreezeError}</Typography>}
</Stack>
{semester.series_set.map((series) => (
<Stack key={series.id} gap={1} mt={5}>
<Typography variant="h2">{series.order}. séria</Typography>
<Stack pl={2} alignItems="start">
<Button variant="button2" onClick={() => freezeSeries(series)}>
Uzavrieť sériu
</Button>
{seriesFreezeError && <Typography variant="body1">{seriesFreezeError}</Typography>}
</Stack>
<Stack direction="row" justifyContent="space-between" alignItems="center">
<Typography variant="h3">Opravovanie úloh:</Typography>
<Typography variant="body1" component="div">
Expand Down

0 comments on commit 722bbcc

Please sign in to comment.