From 18a4d642563ccdabbd0d960324bb0f7db68c5b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vikt=C3=B3ria=20Brezinov=C3=A1?= Date: Fri, 10 Nov 2023 22:25:28 +0100 Subject: [PATCH] refactor uploading publications --- .../PublicationUploader.tsx | 46 ++++++++++++------- .../SemesterAdministration.tsx | 36 ++------------- 2 files changed, 33 insertions(+), 49 deletions(-) diff --git a/src/components/PublicationUploader/PublicationUploader.tsx b/src/components/PublicationUploader/PublicationUploader.tsx index d4fbab92..f5b7bf1d 100644 --- a/src/components/PublicationUploader/PublicationUploader.tsx +++ b/src/components/PublicationUploader/PublicationUploader.tsx @@ -1,18 +1,22 @@ import {Upload} from '@mui/icons-material' +import {useQueryClient} from '@tanstack/react-query' import axios from 'axios' import {FC, useCallback} from 'react' -import {Accept, DropzoneOptions, useDropzone} from 'react-dropzone' +import {DropzoneOptions, useDropzone} from 'react-dropzone' + +import {SemesterWithProblems} from '@/types/api/generated/competition' + +import {Link} from '../Clickable/Clickable' interface PublicationUploaderProps { - uploadLink: string - acceptedFormats?: Accept - publication_type: string - event: string - order: string - refetch: () => void + semesterId: string + order: number + semesterData: SemesterWithProblems | undefined } -export const PublicationUploader: FC = ({uploadLink, acceptedFormats, publication_type, event, order, refetch}) => { +export const PublicationUploader: FC = ({semesterId, order, semesterData}) => { + const queryClient = useQueryClient() + const onDrop = useCallback>( async (acceptedFiles, fileRejections) => { if (fileRejections.length > 0) { @@ -20,28 +24,38 @@ export const PublicationUploader: FC = ({uploadLink, a } const formData = new FormData() formData.append('file', acceptedFiles[0]) - formData.append('publication_type', publication_type) - formData.append('event', event) - formData.append('order', order) - await axios.post(uploadLink, formData) - await refetch() - + formData.append('publication_type', 'Časopisy') + formData.append('event', semesterId) + formData.append('order', order.toString()) + await axios.post('/api/competition/publication/upload/', formData) + await queryClient.invalidateQueries({queryKey: ['competition', 'semester', semesterId]}) }, - [refetch, uploadLink], + [semesterId, order, queryClient], ) + const publication = semesterData?.publication_set.find((publication) => publication.order === order) const {getRootProps, getInputProps} = useDropzone({ onDrop, multiple: false, - accept: acceptedFormats ?? {}, + accept: + { + 'application/pdf': ['.pdf'], + } ?? {}, }) return ( <> +

{order}.Časopis:

+ {} + {publication && ( + + {publication.name} + + )} ) } diff --git a/src/components/SemesterAdministration/SemesterAdministration.tsx b/src/components/SemesterAdministration/SemesterAdministration.tsx index da89b343..51c1b337 100644 --- a/src/components/SemesterAdministration/SemesterAdministration.tsx +++ b/src/components/SemesterAdministration/SemesterAdministration.tsx @@ -127,39 +127,9 @@ export const SemesterAdministration: FC = () => { <> )}

Nahrávanie časopisov

- 1. časopis: - {}} - /> - 2. časopis: - {}} - /> - 3. časopis: - {}} - /> + {[1, 2, 3].map((order) => ( + + ))} ) }