From cd0280e76af1e9c458ece489fccff83918f3db93 Mon Sep 17 00:00:00 2001 From: Michal Masrna Date: Sat, 11 Nov 2023 15:05:39 +0100 Subject: [PATCH 1/6] remove unused code upload is not in the side panel anymore --- src/components/Problems/Problems.tsx | 455 +++++++++++++-------------- 1 file changed, 223 insertions(+), 232 deletions(-) diff --git a/src/components/Problems/Problems.tsx b/src/components/Problems/Problems.tsx index 11784984..9e6b5291 100644 --- a/src/components/Problems/Problems.tsx +++ b/src/components/Problems/Problems.tsx @@ -1,232 +1,223 @@ -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 {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} + /> + )} +
+ + ) +} From 1b162285135fd7b100d3472f8c8dc5c4ead4d7e1 Mon Sep 17 00:00:00 2001 From: Michal Masrna Date: Sat, 11 Nov 2023 15:05:52 +0100 Subject: [PATCH 2/6] removed chcem riesit button --- src/components/Problems/Problems.tsx | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/components/Problems/Problems.tsx b/src/components/Problems/Problems.tsx index 9e6b5291..55ae2e77 100644 --- a/src/components/Problems/Problems.tsx +++ b/src/components/Problems/Problems.tsx @@ -199,16 +199,7 @@ export const Problems: FC = () => {
-
- {!isRegistered && canRegister ? ( -
registerToSemester(id.semesterId)} className={styles.registerButton}> - Chcem riešiť! -
- ) : ( - // sideCointainer grid rata s tymto childom, aj ked prazdnym -
- )} {displaySideContent.type === 'discussion' && ( Date: Sat, 11 Nov 2023 15:06:19 +0100 Subject: [PATCH 3/6] Register dialog when user not registered to semester tries to upload their first solution --- src/components/Problems/Problems.tsx | 62 +++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/src/components/Problems/Problems.tsx b/src/components/Problems/Problems.tsx index 55ae2e77..1c013973 100644 --- a/src/components/Problems/Problems.tsx +++ b/src/components/Problems/Problems.tsx @@ -1,13 +1,17 @@ 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' @@ -28,7 +32,16 @@ const Problem: FC<{ canRegister: boolean canSubmit: boolean invalidateSeriesQuery: () => Promise -}> = ({problem, registered, setDisplaySideContent, canSubmit, invalidateSeriesQuery}) => { + displayRegisterDialog: () => void +}> = ({ + problem, + registered, + setDisplaySideContent, + canRegister, + canSubmit, + invalidateSeriesQuery, + displayRegisterDialog, +}) => { const handleDiscussionButtonClick = () => { setDisplaySideContent((prevState) => { if (prevState.type === 'discussion' && prevState.problemId === problem.id) { @@ -39,8 +52,12 @@ const Problem: FC<{ }) } const handleUploadClick = () => { - setDisplayProblemUploadForm((prevState) => !prevState) - setDisplayActions(false) + if (!registered && canRegister) { + displayRegisterDialog() + } else { + setDisplayProblemUploadForm((prevState) => !prevState) + setDisplayActions(false) + } } const [displayProblemUploadForm, setDisplayProblemUploadForm] = useState(false) @@ -96,7 +113,7 @@ const Problem: FC<{ )} - {registered && ( + {(registered || canRegister) && ( @@ -116,6 +133,17 @@ const overrideCycle = (prev: boolean | 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 @@ -131,7 +159,7 @@ export const Problems: FC = () => { }) const series = seriesData?.data const problems = series?.problems ?? [] - // const semesterId = series?.semester ?? -1 + const semesterId = series?.semester ?? -1 const canSubmit = series?.can_submit ?? false const [overrideCanRegister, setOverrideCanRegister] = useState() @@ -156,8 +184,31 @@ export const Problems: FC = () => { 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 || @@ -177,6 +228,7 @@ export const Problems: FC = () => { canRegister={canRegister} canSubmit={canSubmit} invalidateSeriesQuery={invalidateSeriesQuery} + displayRegisterDialog={() => setDeleteDialogId(problem.id)} /> ))} From fbd5864d63643e81c701a9156d1559ef10da2ddc Mon Sep 17 00:00:00 2001 From: Michal Masrna Date: Sat, 11 Nov 2023 15:21:34 +0100 Subject: [PATCH 4/6] LF -> CRLF fix --- src/components/Problems/Problems.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Problems/Problems.tsx b/src/components/Problems/Problems.tsx index 1c013973..a528c322 100644 --- a/src/components/Problems/Problems.tsx +++ b/src/components/Problems/Problems.tsx @@ -201,7 +201,7 @@ export const Problems: FC = () => { open={deleteDialogId !== undefined} close={close} title="Skontroluj prosím, čí údaje o ročníku a škole sú správne." - contentText={`Škola: ${profile?.grade_name}, Ročník: ${profile?.school.verbose_name}`} // TODO: this is not styled, I suggest expanding the dialog component to support content as component + contentText={`Škola: ${profile?.grade_name}, Ročník: ${profile?.school.verbose_name}`} // TODO: this is not styled yet, I suggest expanding the dialog component to support content as component actions={ <> From eba5aafa7635ada49a40ef524db4ea63a26c3721 Mon Sep 17 00:00:00 2001 From: Michal Masrna Date: Sat, 11 Nov 2023 15:32:22 +0100 Subject: [PATCH 5/6] CRLF fix --- src/components/Problems/Problems.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Problems/Problems.tsx b/src/components/Problems/Problems.tsx index a528c322..1c013973 100644 --- a/src/components/Problems/Problems.tsx +++ b/src/components/Problems/Problems.tsx @@ -201,7 +201,7 @@ export const Problems: FC = () => { open={deleteDialogId !== undefined} close={close} title="Skontroluj prosím, čí údaje o ročníku a škole sú správne." - contentText={`Škola: ${profile?.grade_name}, Ročník: ${profile?.school.verbose_name}`} // TODO: this is not styled yet, I suggest expanding the dialog component to support content as component + contentText={`Škola: ${profile?.grade_name}, Ročník: ${profile?.school.verbose_name}`} // TODO: this is not styled, I suggest expanding the dialog component to support content as component actions={ <> From d7ba0a2b65dcc71872351a4d24242da55b1173fc Mon Sep 17 00:00:00 2001 From: Michal Masrna Date: Sat, 11 Nov 2023 19:14:07 +0100 Subject: [PATCH 6/6] Register to semester dialog refactor --- src/components/Problems/Problems.tsx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/components/Problems/Problems.tsx b/src/components/Problems/Problems.tsx index 1c013973..c3409167 100644 --- a/src/components/Problems/Problems.tsx +++ b/src/components/Problems/Problems.tsx @@ -184,22 +184,22 @@ export const Problems: FC = () => { const {hasPermissions, permissionsIsLoading} = useHasPermissions() - const [deleteDialogId, setDeleteDialogId] = useState() - const close = () => setDeleteDialogId(undefined) + const [displayRegisterDialog, setDisplayRegisterDialog] = useState(false) + const closeRegisterDialog = () => setDisplayRegisterDialog(false) const editProfile = () => { - close() + closeRegisterDialog() router.push(`/${seminar}/profil/uprava`) } const agree = () => { - deleteDialogId !== undefined && registerToSemester(semesterId) - close() + displayRegisterDialog && registerToSemester(semesterId) + closeRegisterDialog() } return ( <> { canRegister={canRegister} canSubmit={canSubmit} invalidateSeriesQuery={invalidateSeriesQuery} - displayRegisterDialog={() => setDeleteDialogId(problem.id)} + displayRegisterDialog={() => setDisplayRegisterDialog(true)} /> ))}