Skip to content

Commit

Permalink
356 - Show errors from API on ZIP upload (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
rtrembecky authored May 21, 2024
1 parent c9a2692 commit c31da00
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/components/ProblemAdministration/ProblemAdministration.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {FormatAlignJustify, Grading} from '@mui/icons-material'
import {Stack, Typography} from '@mui/material'
import {useMutation, useQuery} from '@tanstack/react-query'
import axios from 'axios'
import axios, {isAxiosError} from 'axios'
import {useRouter} from 'next/router'
import React, {FC, useCallback, useEffect, useState} from 'react'
import {DropzoneOptions, useDropzone} from 'react-dropzone'
Expand Down Expand Up @@ -82,12 +82,23 @@ export const ProblemAdministration: FC = () => {
setSolutions(data)
}

const {mutate: uploadZipFile} = useMutation({
const {mutate: uploadZipFile, error: uploadZipFileError} = useMutation({
mutationFn: ({data, problemId}: {data: FormData; problemId?: string}) =>
axios.post(`/api/competition/problem/${problemId}/upload-corrected`, data),
onSuccess: () => refetchProblem(),
})

const uploadZipFileErrorData =
isAxiosError(uploadZipFileError) && uploadZipFileError.response && uploadZipFileError.response.data
const uploadZipFileErrorArray = Array.isArray(uploadZipFileErrorData) ? uploadZipFileErrorData : []
const uploadZipFileErrors = uploadZipFileErrorArray
.map((errorObj) => {
const filename = 'filename' in errorObj ? `${errorObj.filename}: ` : ''
const status = 'status' in errorObj ? `${errorObj.status}` : ''
return `${filename}${status}`
})
.join('\n')

const onDrop = useCallback<NonNullable<DropzoneOptions['onDrop']>>(
(acceptedFiles, fileRejections) => {
if (fileRejections.length > 0) {
Expand Down Expand Up @@ -157,6 +168,12 @@ export const ProblemAdministration: FC = () => {
getInputProps={getInputProps}
text="Vlož opravené riešenia vo formáte zip"
/>
{uploadZipFileError && (
<>
<Typography>Chyby pri nahrávaní ZIPka:</Typography>
<Typography sx={{whiteSpace: 'pre-line'}}>{uploadZipFileErrors}</Typography>
</>
)}

<form>
<div className={styles.table}>
Expand Down

0 comments on commit c31da00

Please sign in to comment.