Skip to content

Commit

Permalink
Refetching semester on successful freeze of semester or series
Browse files Browse the repository at this point in the history
  • Loading branch information
michalmasrna1 committed Nov 24, 2024
1 parent cd1fbd5 commit f370462
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 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 @@ -87,31 +91,30 @@ 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) {
const {mutate: freezeSemester} = useMutation({
mutationFn: (semester: SemesterWithProblems) =>
axios.post(`/api/competition/semester/${semester.id}/results/freeze`),
onSuccess: () => refetch(),
onError: (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) {
const {mutate: freezeSeries} = useMutation({
mutationFn: (series: SeriesWithProblems) => axios.post(`/api/competition/series/${series.id}/results/freeze`),
onSuccess: () => refetch(),
onError: (error: unknown) => {
if (error instanceof AxiosError) {
setSeriesFreezeError(error.response?.data.detail)
} else {
setSeriesFreezeError('Nastala neznáma chyba.')
}
}
}
},
})

if (
urlDataLoading.currentSeriesIsLoading ||
Expand Down

0 comments on commit f370462

Please sign in to comment.