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

Upload of publications in administration #156

Merged
merged 6 commits into from
Nov 11, 2023
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
6 changes: 4 additions & 2 deletions src/components/FileUploader/FileUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {Accept, DropzoneOptions, useDropzone} from 'react-dropzone'
interface FileUploaderProps {
uploadLink: string
acceptedFormats?: Accept
adjustFormData?: (formData: FormData) => void
refetch: () => void
}

export const FileUploader: FC<FileUploaderProps> = ({uploadLink, acceptedFormats, refetch}) => {
export const FileUploader: FC<FileUploaderProps> = ({uploadLink, acceptedFormats, adjustFormData, refetch}) => {
const {mutate: fileUpload} = useMutation({
mutationFn: (formData: FormData) => axios.post(uploadLink, formData),
onSuccess: () => refetch(),
Expand All @@ -23,9 +24,10 @@ export const FileUploader: FC<FileUploaderProps> = ({uploadLink, acceptedFormats
}
const formData = new FormData()
formData.append('file', acceptedFiles[0])
adjustFormData?.(formData)
fileUpload(formData)
},
[fileUpload],
[adjustFormData, fileUpload],
)

const {getRootProps, getInputProps} = useDropzone({
Expand Down
43 changes: 43 additions & 0 deletions src/components/PublicationUploader/PublicationUploader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {Stack} from '@mui/material'
import {useQueryClient} from '@tanstack/react-query'
import {FC} from 'react'

import {SemesterWithProblems} from '@/types/api/generated/competition'

import {Link} from '../Clickable/Clickable'
import {FileUploader} from '../FileUploader/FileUploader'

interface PublicationUploaderProps {
semesterId: string
order: number
semesterData: SemesterWithProblems | undefined
}

export const PublicationUploader: FC<PublicationUploaderProps> = ({semesterId, order, semesterData}) => {
const queryClient = useQueryClient()

const refetch = () => queryClient.invalidateQueries({queryKey: ['competition', 'semester', semesterId]})

const appendFormData = (formData: FormData) => {
formData.append('publication_type', 'Časopisy')
formData.append('event', semesterId)
formData.append('order', order.toString())
}

const publication = semesterData?.publication_set.find((publication) => publication.order === order)

return (
<Stack direction="row" gap={2} alignItems="center">
<h4>{order}. Časopis:</h4>
{publication && (
<Link href={`/api/competition/publication/${publication.id}/download`}>{publication.name}.pdf</Link>
)}
<FileUploader
uploadLink={'/api/competition/publication/upload/'}
acceptedFormats={{'application/pdf': ['.pdf']}}
adjustFormData={appendFormData}
refetch={refetch}
/>
</Stack>
)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Stack} from '@mui/material'
import {useQuery} from '@tanstack/react-query'
import axios from 'axios'
import {useRouter} from 'next/router'
Expand All @@ -9,6 +10,7 @@ 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'

interface PostalCard {
Expand Down Expand Up @@ -125,6 +127,12 @@ export const SemesterAdministration: FC = () => {
) : (
<></>
)}
<Stack mt={1} gap={1}>
<h3>Nahrávanie časopisov</h3>
{[1, 2, 3].map((order) => (
<PublicationUploader key={order} semesterId={semesterId ?? ''} order={order} semesterData={semester} />
))}
</Stack>
</>
)
}
1 change: 1 addition & 0 deletions src/types/api/competition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Publication {
file: string
publication_type: number | null
event: number | null
order: number | null
}

export interface RegistrationLink {
Expand Down
1 change: 1 addition & 0 deletions src/types/api/generated/competition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Publication {
file: any
publication_type?: any | null
event?: any | null
order: number | null
}

export interface RegistrationLink {
Expand Down
Loading