From 49521ea610c79ae9a2db6f6f29bfacb5ed23cc7a Mon Sep 17 00:00:00 2001 From: mike cullerton Date: Fri, 9 Dec 2022 17:44:25 -0500 Subject: [PATCH 1/2] Confirm before overwriting file when uploading file with same name --- .../src/routes/ProcessModelShow.tsx | 74 ++++++++++++++++--- 1 file changed, 62 insertions(+), 12 deletions(-) diff --git a/spiffworkflow-frontend/src/routes/ProcessModelShow.tsx b/spiffworkflow-frontend/src/routes/ProcessModelShow.tsx index ef0c65c1..c5765d89 100644 --- a/spiffworkflow-frontend/src/routes/ProcessModelShow.tsx +++ b/spiffworkflow-frontend/src/routes/ProcessModelShow.tsx @@ -360,27 +360,76 @@ export default function ProcessModelShow() { ); }; + const [fileUploadEvent, setFileUploadEvent] = useState(null); + const [duplicateFilename, setDuplicateFilename] = useState(''); + const [showOverwriteConfirmationPrompt, setShowOverwriteConfirmationPrompt] = + useState(false); + + const doFileUpload = (event: any) => { + event.preventDefault(); + const url = `/process-models/${modifiedProcessModelId}/files`; + const formData = new FormData(); + formData.append('file', filesToUpload[0]); + formData.append('fileName', filesToUpload[0].name); + HttpService.makeCallToBackend({ + path: url, + successCallback: onUploadedCallback, + httpMethod: 'POST', + postBody: formData, + }); + setFilesToUpload(null); + }; + const handleFileUploadCancel = () => { setShowFileUploadModal(false); setFilesToUpload(null); }; + const handleOverwriteFileConfirm = () => { + setShowOverwriteConfirmationPrompt(false); + doFileUpload(fileUploadEvent); + }; + const handleOverwriteFileCancel = () => { + setShowOverwriteConfirmationPrompt(false); + setFilesToUpload(null); + }; + + const confirmOverwriteFileDialog = () => { + return ( + + ); + }; + const displayOverwriteConfirmation = (filename: String) => { + setDuplicateFilename(filename); + setShowOverwriteConfirmationPrompt(true); + }; + + const checkDuplicateFile = (event: any) => { + if (processModel && processModel.files.length > 0) { + for (const file in processModel.files) { + if (processModel.files[file].name === filesToUpload[0].name) { + displayOverwriteConfirmation(processModel.files[file].name); + setFileUploadEvent(event); + } + } + } + }; const handleFileUpload = (event: any) => { if (processModel) { - event.preventDefault(); - const url = `/process-models/${modifiedProcessModelId}/files`; - const formData = new FormData(); - formData.append('file', filesToUpload[0]); - formData.append('fileName', filesToUpload[0].name); - HttpService.makeCallToBackend({ - path: url, - successCallback: onUploadedCallback, - httpMethod: 'POST', - postBody: formData, - }); + checkDuplicateFile(event); } setShowFileUploadModal(false); - setFilesToUpload(null); }; const fileUploadModal = () => { @@ -548,6 +597,7 @@ export default function ProcessModelShow() { return ( <> {fileUploadModal()} + {confirmOverwriteFileDialog()} Date: Tue, 13 Dec 2022 15:57:46 -0500 Subject: [PATCH 2/2] use forEach --- spiffworkflow-frontend/src/routes/ProcessModelShow.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spiffworkflow-frontend/src/routes/ProcessModelShow.tsx b/spiffworkflow-frontend/src/routes/ProcessModelShow.tsx index c5765d89..dbe91409 100644 --- a/spiffworkflow-frontend/src/routes/ProcessModelShow.tsx +++ b/spiffworkflow-frontend/src/routes/ProcessModelShow.tsx @@ -416,12 +416,12 @@ export default function ProcessModelShow() { const checkDuplicateFile = (event: any) => { if (processModel && processModel.files.length > 0) { - for (const file in processModel.files) { - if (processModel.files[file].name === filesToUpload[0].name) { - displayOverwriteConfirmation(processModel.files[file].name); + processModel.files.forEach((file) => { + if (file.name === filesToUpload[0].name) { + displayOverwriteConfirmation(file.name); setFileUploadEvent(event); } - } + }); } };