diff --git a/src/components/Problems/Problems.tsx b/src/components/Problems/Problems.tsx index 11784984..1c013973 100644 --- a/src/components/Problems/Problems.tsx +++ b/src/components/Problems/Problems.tsx @@ -1,232 +1,266 @@ -import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query' -import axios from 'axios' -import Image from 'next/image' -import {Dispatch, FC, SetStateAction, useState} from 'react' - -import {Button, Link} from '@/components/Clickable/Clickable' -import {Problem, SeriesWithProblems} from '@/types/api/competition' -import {useDataFromURL} from '@/utils/useDataFromURL' -import {useHasPermissions} from '@/utils/useHasPermissions' - -import {Latex} from '../Latex/Latex' -import {Loading} from '../Loading/Loading' -import {Discussion} from './Discussion' -import styles from './Problems.module.scss' -import {UploadProblemForm} from './UploadProblemForm' - -const Problem: FC<{ - problem: Problem - setDisplaySideContent: Dispatch< - SetStateAction<{ - type: string - problemId: number - problemNumber: number - problemSubmitted?: boolean - }> - > - registered: boolean - canRegister: boolean - canSubmit: boolean - invalidateSeriesQuery: () => Promise -}> = ({problem, registered, setDisplaySideContent, canSubmit, invalidateSeriesQuery}) => { - const handleDiscussionButtonClick = () => { - setDisplaySideContent((prevState) => { - if (prevState.type === 'discussion' && prevState.problemId === problem.id) { - return {type: '', problemId: -1, problemNumber: -1} - } else { - return {type: 'discussion', problemId: problem.id, problemNumber: problem.order} - } - }) - } - const handleUploadClick = () => { - setDisplayProblemUploadForm((prevState) => !prevState) - setDisplayActions(false) - } - - const [displayProblemUploadForm, setDisplayProblemUploadForm] = useState(false) - const [displayActions, setDisplayActions] = useState(true) - - return ( -
-

{problem.order}. ÚLOHA

- {problem.text} - {problem.image && ( -
- {`Obrázok -
- )} - {displayProblemUploadForm && ( - - )} - {displayActions && ( -
- {problem.solution_pdf && ( - - vzorové riešenie - - )} - {registered && ( - <> - - moje riešenie - - - opravené riešenie{!!problem.submitted?.corrected_solution && ` (${problem.submitted.score || '?'})`} - - - )} - - {registered && ( - - )} -
- )} -
- ) -} - -const overrideCycle = (prev: boolean | undefined) => { - if (prev === undefined) return true - if (prev === true) return false - return undefined -} - -export const Problems: FC = () => { - const {id, seminar, loading} = useDataFromURL() - - // used to display discussions - const [displaySideContent, setDisplaySideContent] = useState<{ - type: string - problemId: number - problemNumber: number - problemSubmitted?: boolean - }>({type: '', problemId: -1, problemNumber: -1, problemSubmitted: false}) - - const {data: seriesData, isLoading: seriesIsLoading} = useQuery({ - queryKey: ['competition', 'series', id.seriesId], - queryFn: () => axios.get(`/api/competition/series/${id.seriesId}`), - enabled: id.seriesId !== -1, - }) - const series = seriesData?.data - const problems = series?.problems ?? [] - // const semesterId = series?.semester ?? -1 - const canSubmit = series?.can_submit ?? false - - const [overrideCanRegister, setOverrideCanRegister] = useState() - const [overrideIsRegistered, setOverrideIsRegistered] = useState() - const toggleCanRegister = () => setOverrideCanRegister((prevState) => overrideCycle(prevState)) - const toggleIsRegistered = () => setOverrideIsRegistered((prevState) => overrideCycle(prevState)) - - const canRegister = overrideCanRegister ?? series?.can_participate ?? false - const isRegistered = overrideIsRegistered ?? series?.is_registered ?? false - - const queryClient = useQueryClient() - - const invalidateSeriesQuery = () => queryClient.invalidateQueries({queryKey: ['competition', 'series', id.seriesId]}) - - const {mutate: registerToSemester} = useMutation({ - mutationFn: (id: number) => axios.post(`/api/competition/event/${id}/register`), - onSuccess: () => { - // refetch semestra, nech sa aktualizuje is_registered - invalidateSeriesQuery() - }, - }) - - const {hasPermissions, permissionsIsLoading} = useHasPermissions() - - return ( - <> -
- {(loading.semesterListIsLoading || - loading.currentSeriesIsLoading || - seriesIsLoading || - permissionsIsLoading) && } - {hasPermissions && ( -
- Admin: Opravovanie -
- )} - {problems.map((problem) => ( - - ))} - - {/* TODO: odstranit z produkcie */} -
- debug sekcia: -
- - - {' '} - {overrideIsRegistered === undefined ? 'no override' : overrideIsRegistered ? 'on' : 'off'} - -
-
- - - {' '} - {overrideCanRegister === undefined ? 'no override' : overrideCanRegister ? 'on' : 'off'} - -
-
-
- -
- {!isRegistered && canRegister ? ( -
registerToSemester(id.semesterId)} className={styles.registerButton}> - Chcem riešiť! -
- ) : ( - // sideCointainer grid rata s tymto childom, aj ked prazdnym -
- )} - {displaySideContent.type === 'discussion' && ( - setDisplaySideContent({type: '', problemId: -1, problemNumber: -1})} - invalidateSeriesQuery={invalidateSeriesQuery} - /> - )} - {/* {displaySideContent.type === 'uploadProblemForm' && ( - - )} */} -
- - ) -} +import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query' +import axios from 'axios' +import Image from 'next/image' +import {useRouter} from 'next/router' +import {Dispatch, FC, SetStateAction, useState} from 'react' + +import {Button, Link} from '@/components/Clickable/Clickable' +import {Problem, SeriesWithProblems} from '@/types/api/competition' +import {Profile} from '@/types/api/personal' +import {AuthContainer} from '@/utils/AuthContainer' +import {useDataFromURL} from '@/utils/useDataFromURL' +import {useHasPermissions} from '@/utils/useHasPermissions' + +import {Dialog} from '../Dialog/Dialog' +import {Latex} from '../Latex/Latex' +import {Loading} from '../Loading/Loading' +import {Discussion} from './Discussion' +import styles from './Problems.module.scss' +import {UploadProblemForm} from './UploadProblemForm' + +const Problem: FC<{ + problem: Problem + setDisplaySideContent: Dispatch< + SetStateAction<{ + type: string + problemId: number + problemNumber: number + problemSubmitted?: boolean + }> + > + registered: boolean + canRegister: boolean + canSubmit: boolean + invalidateSeriesQuery: () => Promise + displayRegisterDialog: () => void +}> = ({ + problem, + registered, + setDisplaySideContent, + canRegister, + canSubmit, + invalidateSeriesQuery, + displayRegisterDialog, +}) => { + const handleDiscussionButtonClick = () => { + setDisplaySideContent((prevState) => { + if (prevState.type === 'discussion' && prevState.problemId === problem.id) { + return {type: '', problemId: -1, problemNumber: -1} + } else { + return {type: 'discussion', problemId: problem.id, problemNumber: problem.order} + } + }) + } + const handleUploadClick = () => { + if (!registered && canRegister) { + displayRegisterDialog() + } else { + setDisplayProblemUploadForm((prevState) => !prevState) + setDisplayActions(false) + } + } + + const [displayProblemUploadForm, setDisplayProblemUploadForm] = useState(false) + const [displayActions, setDisplayActions] = useState(true) + + return ( +
+

{problem.order}. ÚLOHA

+ {problem.text} + {problem.image && ( +
+ {`Obrázok +
+ )} + {displayProblemUploadForm && ( + + )} + {displayActions && ( +
+ {problem.solution_pdf && ( + + vzorové riešenie + + )} + {registered && ( + <> + + moje riešenie + + + opravené riešenie{!!problem.submitted?.corrected_solution && ` (${problem.submitted.score || '?'})`} + + + )} + + {(registered || canRegister) && ( + + )} +
+ )} +
+ ) +} + +const overrideCycle = (prev: boolean | undefined) => { + if (prev === undefined) return true + if (prev === true) return false + return undefined +} + +export const Problems: FC = () => { + const {id, seminar, loading} = useDataFromURL() + + const router = useRouter() + + const {isAuthed} = AuthContainer.useContainer() + + const {data} = useQuery({ + queryKey: ['personal', 'profiles', 'myprofile'], + queryFn: () => axios.get(`/api/personal/profiles/myprofile`), + enabled: isAuthed, + }) + const profile = data?.data + + // used to display discussions + const [displaySideContent, setDisplaySideContent] = useState<{ + type: string + problemId: number + problemNumber: number + problemSubmitted?: boolean + }>({type: '', problemId: -1, problemNumber: -1, problemSubmitted: false}) + + const {data: seriesData, isLoading: seriesIsLoading} = useQuery({ + queryKey: ['competition', 'series', id.seriesId], + queryFn: () => axios.get(`/api/competition/series/${id.seriesId}`), + enabled: id.seriesId !== -1, + }) + const series = seriesData?.data + const problems = series?.problems ?? [] + const semesterId = series?.semester ?? -1 + const canSubmit = series?.can_submit ?? false + + const [overrideCanRegister, setOverrideCanRegister] = useState() + const [overrideIsRegistered, setOverrideIsRegistered] = useState() + const toggleCanRegister = () => setOverrideCanRegister((prevState) => overrideCycle(prevState)) + const toggleIsRegistered = () => setOverrideIsRegistered((prevState) => overrideCycle(prevState)) + + const canRegister = overrideCanRegister ?? series?.can_participate ?? false + const isRegistered = overrideIsRegistered ?? series?.is_registered ?? false + + const queryClient = useQueryClient() + + const invalidateSeriesQuery = () => queryClient.invalidateQueries({queryKey: ['competition', 'series', id.seriesId]}) + + const {mutate: registerToSemester} = useMutation({ + mutationFn: (id: number) => axios.post(`/api/competition/event/${id}/register`), + onSuccess: () => { + // refetch semestra, nech sa aktualizuje is_registered + invalidateSeriesQuery() + }, + }) + + const {hasPermissions, permissionsIsLoading} = useHasPermissions() + + const [deleteDialogId, setDeleteDialogId] = useState() + const close = () => setDeleteDialogId(undefined) + const editProfile = () => { + close() + router.push(`/${seminar}/profil/uprava`) + } + const agree = () => { + deleteDialogId !== undefined && registerToSemester(semesterId) + close() + } + + return ( + <> + + + + + } + /> +
+ {(loading.semesterListIsLoading || + loading.currentSeriesIsLoading || + seriesIsLoading || + permissionsIsLoading) && } + {hasPermissions && ( +
+ Admin: Opravovanie +
+ )} + {problems.map((problem) => ( + setDeleteDialogId(problem.id)} + /> + ))} + + {/* TODO: odstranit z produkcie */} +
+ debug sekcia: +
+ + + {' '} + {overrideIsRegistered === undefined ? 'no override' : overrideIsRegistered ? 'on' : 'off'} + +
+
+ + + {' '} + {overrideCanRegister === undefined ? 'no override' : overrideCanRegister ? 'on' : 'off'} + +
+
+
+
+ {displaySideContent.type === 'discussion' && ( + setDisplaySideContent({type: '', problemId: -1, problemNumber: -1})} + invalidateSeriesQuery={invalidateSeriesQuery} + /> + )} +
+ + ) +}