diff --git a/src/components/Problems/Problem.module.scss b/src/components/Problems/Problem.module.scss new file mode 100644 index 00000000..d08c7726 --- /dev/null +++ b/src/components/Problems/Problem.module.scss @@ -0,0 +1,28 @@ +.problem { + padding: 20px; + + .problemTitle { + font-weight: bold; + } +} + +.actions { + margin-top: .5rem; + display: flex; + justify-content: flex-end; + column-gap: 20px; +} + +.imageContainer { + display: grid; + place-items: center; +} + +.image { + width: auto; + height: auto; + min-height: 3rem; + min-width: 3rem; + max-height: 50vh; + margin-top: 1rem; +} \ No newline at end of file diff --git a/src/components/Problems/Problem.module.scss.d.ts b/src/components/Problems/Problem.module.scss.d.ts new file mode 100644 index 00000000..d976e305 --- /dev/null +++ b/src/components/Problems/Problem.module.scss.d.ts @@ -0,0 +1,13 @@ +export type Styles = { + actions: string + image: string + imageContainer: string + problem: string + problemTitle: string +} + +export type ClassNames = keyof Styles + +declare const styles: Styles + +export default styles diff --git a/src/components/Problems/Problem.tsx b/src/components/Problems/Problem.tsx new file mode 100644 index 00000000..6041b6e9 --- /dev/null +++ b/src/components/Problems/Problem.tsx @@ -0,0 +1,115 @@ +import Image from 'next/image' +import {Dispatch, FC, SetStateAction, useState} from 'react' + +import {Button, Link} from '@/components/Clickable/Clickable' +import {Problem as ProblemType} from '@/types/api/competition' + +import {Latex} from '../Latex/Latex' +import styles from './Problem.module.scss' +import {UploadProblemForm} from './UploadProblemForm' + +export const Problem: FC<{ + problem: ProblemType + 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) && ( + + )} +
+ )} +
+ ) +} diff --git a/src/components/Problems/Problems.module.scss b/src/components/Problems/Problems.module.scss index e519f668..8a42084c 100644 --- a/src/components/Problems/Problems.module.scss +++ b/src/components/Problems/Problems.module.scss @@ -9,21 +9,6 @@ margin-left: auto; } -.problem { - padding: 20px; - - .problemTitle { - font-weight: bold; - } -} - -.actions { - margin-top: .5rem; - display: flex; - justify-content: flex-end; - column-gap: 20px; -} - .debug { margin-left: .5rem; margin-bottom: .5rem; @@ -42,34 +27,4 @@ bottom: 2rem; right: 20px; width: calc(25vw - 40px); -} - -.registerButton { - background-color: black; - color: white; - height: 2rem; - display: flex; - justify-content: center; - align-items: center; - cursor: pointer; - user-select: none; - text-transform: uppercase; - font-size: .8rem; - font-style: italic; - font-weight: bold; - margin-bottom: 1rem; -} - -.imageContainer { - display: grid; - place-items: center; -} - -.image { - width: auto; - height: auto; - min-height: 3rem; - min-width: 3rem; - max-height: 50vh; - margin-top: 1rem; } \ No newline at end of file diff --git a/src/components/Problems/Problems.module.scss.d.ts b/src/components/Problems/Problems.module.scss.d.ts index 1fbd19cf..8d3ae811 100644 --- a/src/components/Problems/Problems.module.scss.d.ts +++ b/src/components/Problems/Problems.module.scss.d.ts @@ -1,13 +1,7 @@ export type Styles = { - actions: string adminSection: string container: string debug: string - image: string - imageContainer: string - problem: string - problemTitle: string - registerButton: string sideContainer: string } diff --git a/src/components/Problems/Problems.tsx b/src/components/Problems/Problems.tsx index 11784984..158e0954 100644 --- a/src/components/Problems/Problems.tsx +++ b/src/components/Problems/Problems.tsx @@ -1,232 +1,158 @@ -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 {useRouter} from 'next/router' +import {FC, useState} from 'react' + +import {Button, Link} from '@/components/Clickable/Clickable' +import {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 {Loading} from '../Loading/Loading' +import {Discussion} from './Discussion' +import {Problem} from './Problem' +import styles from './Problems.module.scss' + +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 [displayRegisterDialog, setDisplayRegisterDialog] = useState(false) + const closeRegisterDialog = () => setDisplayRegisterDialog(false) + const editProfile = () => { + closeRegisterDialog() + router.push(`/${seminar}/profil/uprava`) + } + const agree = () => { + displayRegisterDialog && registerToSemester(semesterId) + closeRegisterDialog() + } + + return ( + <> + + + + + } + /> +
+ {(loading.semesterListIsLoading || + loading.currentSeriesIsLoading || + seriesIsLoading || + permissionsIsLoading) && } + {hasPermissions && ( +
+ Admin: Opravovanie +
+ )} + {problems.map((problem) => ( + setDisplayRegisterDialog(true)} + /> + ))} + + {/* 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} + /> + )} +
+ + ) +} diff --git a/src/components/SemesterAdministration/SemesterAdministration.module.scss b/src/components/SemesterAdministration/SemesterAdministration.module.scss new file mode 100644 index 00000000..66f7f244 --- /dev/null +++ b/src/components/SemesterAdministration/SemesterAdministration.module.scss @@ -0,0 +1,6 @@ +.actions { + margin-top: .5rem; + display: flex; + justify-content: flex-end; + column-gap: 20px; +} \ No newline at end of file diff --git a/src/components/SemesterAdministration/SemesterAdministration.module.scss.d.ts b/src/components/SemesterAdministration/SemesterAdministration.module.scss.d.ts new file mode 100644 index 00000000..2d2673e6 --- /dev/null +++ b/src/components/SemesterAdministration/SemesterAdministration.module.scss.d.ts @@ -0,0 +1,9 @@ +export type Styles = { + actions: string +} + +export type ClassNames = keyof Styles + +declare const styles: Styles + +export default styles diff --git a/src/components/SemesterAdministration/SemesterAdministration.tsx b/src/components/SemesterAdministration/SemesterAdministration.tsx index bcb967e5..312ac5d3 100644 --- a/src/components/SemesterAdministration/SemesterAdministration.tsx +++ b/src/components/SemesterAdministration/SemesterAdministration.tsx @@ -9,9 +9,9 @@ import {useHasPermissions} from '@/utils/useHasPermissions' import {Button, Link} from '../Clickable/Clickable' import {Loading} from '../Loading/Loading' -import styles from '../Problems/Problems.module.scss' import {PublicationUploader} from '../PublicationUploader/PublicationUploader' import {Result} from '../Results/ResultsRow' +import styles from './SemesterAdministration.module.scss' interface PostalCard { code: number