Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semester administration enhancements #499

Merged
merged 6 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 36 additions & 39 deletions src/components/SemesterAdministration/SemesterAdministration.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Stack, Typography} from '@mui/material'
import {useQuery} from '@tanstack/react-query'
import {useMutation, useQuery} from '@tanstack/react-query'
import axios, {AxiosError} from 'axios'
import {FC, useState} from 'react'

Expand Down Expand Up @@ -33,7 +33,11 @@ export const SemesterAdministration: FC = () => {

const {hasPermissions, permissionsIsLoading} = useHasPermissions()

const {data: semesterData, isLoading: semesterIsLoading} = useQuery({
const {
data: semesterData,
isLoading: semesterIsLoading,
refetch,
} = useQuery({
queryKey: ['competition', 'semester', semesterId],
queryFn: () => axios.get<SemesterWithProblems>(`/api/competition/semester/${semesterId}`),
// router.query.params su v prvom renderi undefined, tak pustime query az so spravnym semesterId
Expand Down Expand Up @@ -84,34 +88,22 @@ 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 [seriesFreezeErrors, setSeriesFreezeErrors] = useState<Map<number, string>>()

const freezeSeries = async (series: SeriesWithProblems) => {
setSeriesFreezeError('')
try {
await axios.post(`/api/competition/series/${series.id}/results/freeze`)
} catch (error: unknown) {
const {mutate: freezeSeries} = useMutation({
mutationFn: (series: SeriesWithProblems) => axios.post(`/api/competition/series/${series.id}/results/freeze`),
onSuccess: (_, variables: SeriesWithProblems) => {
setSeriesFreezeErrors((prev) => new Map(prev).set(variables.id, ''))
refetch()
},
onError: (error: unknown, variables: SeriesWithProblems) => {
if (error instanceof AxiosError) {
setSeriesFreezeError(error.response?.data.detail)
setSeriesFreezeErrors((prev) => new Map(prev).set(variables.id, error.response?.data.detail))
} else {
setSeriesFreezeError('Nastala neznáma chyba.')
setSeriesFreezeErrors((prev) => new Map(prev).set(variables.id, 'Nastala neznáma chyba.'))
}
}
}
},
})

if (
urlDataLoading.currentSeriesIsLoading ||
Expand All @@ -130,24 +122,29 @@ 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 alignItems="start" direction="row" spacing={2}>
<Typography variant="h2">Semester</Typography>
{semester.complete && <Typography variant="body1">Semester je uzavretý</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 alignItems="start" direction="row" spacing={2}>
<Typography variant="h3">{series.order}. séria</Typography>
{series.complete ? (
<Typography variant="body1">Séria je uzavretá</Typography>
) : (
<Button variant="button2" onClick={() => freezeSeries(series)}>
Uzavrieť sériu
</Button>
)}
{seriesFreezeErrors?.get(series.id) && (
<Typography variant="body1">{seriesFreezeErrors?.get(series.id)}</Typography>
)}
</Stack>
<Stack direction="row" justifyContent="space-between" alignItems="center">
<Typography variant="h3">Opravovanie úloh:</Typography>
<Typography variant="body1" component="div">
<b>Opravovanie úloh:</b>
</Typography>
<Typography variant="body1" component="div">
<b>Termín série:</b> {formatDateTime(series.deadline)}
</Typography>
Expand Down
2 changes: 2 additions & 0 deletions src/types/api/generated/competition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export interface Semester {
start: string
end: string
additional_name?: string | null
complete: boolean
frozen_results?: string | null
competition?: any | null
late_tags?: any[]
Expand All @@ -159,6 +160,7 @@ export interface SemesterWithProblems {
start: string
end: string
additional_name?: string | null
complete: boolean
frozen_results?: string | null
competition?: any | null
late_tags?: any[]
Expand Down
Loading