From ffda5441fc52ff79741a52178f3adf4989035fd6 Mon Sep 17 00:00:00 2001 From: Peter Arentsen Date: Thu, 9 Jul 2020 17:44:07 +0200 Subject: [PATCH] fix: add inline error to file component and allow it to be turned off --- src/components/fileUpload.js | 40 ++++++++++++++++++++++++++++++------ src/prefabs/fileUpload.js | 4 ++-- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/components/fileUpload.js b/src/components/fileUpload.js index 137a322c3..1d4e212e2 100644 --- a/src/components/fileUpload.js +++ b/src/components/fileUpload.js @@ -21,7 +21,7 @@ propertyLabelOverride, actionInputId, required, - error, + hideDefaultError, disabled, helperText, fullWidth, @@ -40,6 +40,7 @@ const [uploads, setUploads] = useState({ files: [], data: [], + failedFiles: [], }); const helper = useText(helperText); const propLabel = @@ -64,10 +65,11 @@ setUploads({ files: [], data: [], + failedFiles: [], }); }; - const { files, data } = uploads; + const { files, data, failedFiles } = uploads; const acceptedValue = useText(accept) || 'image/*'; const acceptList = acceptedValue.split(',').map(item => item.trim()); @@ -83,10 +85,29 @@ }, onCompleted: uploadData => { const { uploadFiles } = uploadData; - B.triggerEvent('onSuccess', uploadFiles); + + const [succeededData, failedData] = uploadFiles.reduce( + (result, d) => { + result[d.url.startsWith('http') ? 0 : 1].push(d); + return result; + }, + [[], []], + ); + if (succeededData.length > 0) { + B.triggerEvent('onSuccess', succeededData); + } + if (failedData.length > 0) { + B.triggerEvent( + 'onError', + failedData.map( + d => `File: ${d.name} failed with error: ${d.url}`, + ), + ); + } setUploads({ ...uploads, - data: multiple ? data.concat(uploadFiles) : uploadFiles, + data: multiple ? data.concat(succeededData) : succeededData, + failedFiles: multiple ? failedFiles.concat(failedData) : failedData, }); }, }, @@ -157,7 +178,7 @@ 0} disabled={disabled} margin={margin} > @@ -169,6 +190,14 @@ root: classes.label, }} /> + + {!hideDefaultError && + failedFiles.length > 0 && + failedFiles.map( + file => `File: ${file.name} failed with error: ${file.url}`, + )} + {helper && { helper }} +
{loading && B.triggerEvent('onLoad')} {data.map(file => ( @@ -186,7 +215,6 @@
))} - {helper && {helper}}
); diff --git a/src/prefabs/fileUpload.js b/src/prefabs/fileUpload.js index 27c357961..6280183a9 100644 --- a/src/prefabs/fileUpload.js +++ b/src/prefabs/fileUpload.js @@ -55,8 +55,8 @@ }, { value: false, - label: 'Error', - key: 'error', + label: 'Hide default error', + key: 'hideDefaultError', type: 'TOGGLE', }, {