From efff157cafae11773a8ca11a95b2db906baf973c Mon Sep 17 00:00:00 2001 From: rtrembecky Date: Sun, 19 May 2024 22:51:47 +0200 Subject: [PATCH] fix #356 - show errors from API on ZIP upload --- .../ProblemAdministration.tsx | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/components/ProblemAdministration/ProblemAdministration.tsx b/src/components/ProblemAdministration/ProblemAdministration.tsx index fd34e535..638e54a0 100644 --- a/src/components/ProblemAdministration/ProblemAdministration.tsx +++ b/src/components/ProblemAdministration/ProblemAdministration.tsx @@ -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' @@ -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>( (acceptedFiles, fileRejections) => { if (fileRejections.length > 0) { @@ -157,6 +168,12 @@ export const ProblemAdministration: FC = () => { getInputProps={getInputProps} text="Vlož opravené riešenia vo formáte zip" /> + {uploadZipFileError && ( + <> + Chyby pri nahrávaní ZIPka: + {uploadZipFileErrors} + + )}