From eab3abd7db149eece4a4d298c5518f089fda0b61 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Tue, 1 Mar 2022 19:21:14 -0500 Subject: [PATCH] fix(striker-ui): show spinner after clicking purge/update in edit file --- striker-ui/components/Files/FileEditForm.tsx | 75 ++++++++++++++------ striker-ui/components/Files/Files.tsx | 42 +++++------ 2 files changed, 73 insertions(+), 44 deletions(-) diff --git a/striker-ui/components/Files/FileEditForm.tsx b/striker-ui/components/Files/FileEditForm.tsx index c80e5761..bf7cc13a 100644 --- a/striker-ui/components/Files/FileEditForm.tsx +++ b/striker-ui/components/Files/FileEditForm.tsx @@ -19,6 +19,8 @@ import mainAxiosInstance from '../../lib/singletons/mainAxiosInstance'; type FileEditProps = { filesOverview: FileOverviewMetadata[]; + onEditFilesComplete?: () => void; + onPurgeFilesComplete?: () => void; }; type FileToEdit = FileDetailMetadata & { @@ -26,7 +28,18 @@ type FileToEdit = FileDetailMetadata & { isSelected?: boolean; }; -const FileEditForm = ({ filesOverview }: FileEditProps): JSX.Element => { +const FILE_EDIT_FORM_DEFAULT_PROPS = { + onEditFilesComplete: undefined, + onPurgeFilesComplete: undefined, +}; + +const FileEditForm = ( + { + filesOverview, + onEditFilesComplete, + onPurgeFilesComplete, + }: FileEditProps = FILE_EDIT_FORM_DEFAULT_PROPS as FileEditProps, +): JSX.Element => { const [filesToEdit, setFilesToEdit] = useState([]); const [isLoadingFilesToEdit, setIsLoadingFilesToEdit] = useState(false); @@ -60,34 +73,48 @@ const FileEditForm = ({ filesOverview }: FileEditProps): JSX.Element => { const editFiles: FormEventHandler = (event) => { event.preventDefault(); - filesToEdit.forEach(({ fileLocations, fileName, fileType, fileUUID }) => { - mainAxiosInstance.put( - `/files/${fileUUID}`, - JSON.stringify({ - fileName, - fileType, - fileLocations: fileLocations.map( - ({ fileLocationUUID, isFileLocationActive }) => ({ - fileLocationUUID, - isFileLocationActive, - }), - ), - }), - { - headers: { 'Content-Type': 'application/json' }, - }, - ); - }); + setIsLoadingFilesToEdit(true); + + const editPromises = filesToEdit.map( + ({ fileLocations, fileName, fileType, fileUUID }) => + mainAxiosInstance.put( + `/files/${fileUUID}`, + JSON.stringify({ + fileName, + fileType, + fileLocations: fileLocations.map( + ({ fileLocationUUID, isFileLocationActive }) => ({ + fileLocationUUID, + isFileLocationActive, + }), + ), + }), + { + headers: { 'Content-Type': 'application/json' }, + }, + ), + ); + + Promise.all(editPromises) + .then(() => { + setIsLoadingFilesToEdit(false); + }) + .then(onEditFilesComplete); }; const purgeFiles: MouseEventHandler = () => { setIsOpenConfirmPurgeDialog(false); + setIsLoadingFilesToEdit(true); - filesToEdit + const purgePromises = filesToEdit .filter(({ isSelected }) => isSelected) - .forEach(({ fileUUID }) => { - mainAxiosInstance.delete(`/files/${fileUUID}`); - }); + .map(({ fileUUID }) => mainAxiosInstance.delete(`/files/${fileUUID}`)); + + Promise.all(purgePromises) + .then(() => { + setIsLoadingFilesToEdit(false); + }) + .then(onPurgeFilesComplete); }; const cancelPurge: MouseEventHandler = () => { @@ -239,4 +266,6 @@ const FileEditForm = ({ filesOverview }: FileEditProps): JSX.Element => { ); }; +FileEditForm.defaultProps = FILE_EDIT_FORM_DEFAULT_PROPS; + export default FileEditForm; diff --git a/striker-ui/components/Files/Files.tsx b/striker-ui/components/Files/Files.tsx index 33706296..f6bb16d7 100644 --- a/striker-ui/components/Files/Files.tsx +++ b/striker-ui/components/Files/Files.tsx @@ -45,10 +45,8 @@ const MessageBox = styled(Box)({ const Files = (): JSX.Element => { const [rawFilesOverview, setRawFilesOverview] = useState([]); const [fetchRawFilesError, setFetchRawFilesError] = useState(); - const [ - isLoadingRawFilesOverview, - setIsLoadingRawFilesOverview, - ] = useState(false); + const [isLoadingRawFilesOverview, setIsLoadingRawFilesOverview] = + useState(false); const [isEditMode, setIsEditMode] = useState(false); const fileUploadFormEventEmitter: EventEmitter = new EventEmitter(); @@ -63,6 +61,19 @@ const Files = (): JSX.Element => { setIsEditMode(!isEditMode); }; + const fetchRawFilesOverview = async () => { + setIsLoadingRawFilesOverview(true); + + try { + const data = await fetchJSON(`${API_BASE_URL}/files`); + setRawFilesOverview(data); + } catch (fetchError) { + setFetchRawFilesError('Failed to get files due to a network issue.'); + } + + setIsLoadingRawFilesOverview(false); + }; + const buildFileList = (): JSX.Element => { let elements: JSX.Element; if (isLoadingRawFilesOverview) { @@ -79,7 +90,11 @@ const Files = (): JSX.Element => { ); elements = isEditMode ? ( - + ) : ( ); @@ -88,19 +103,6 @@ const Files = (): JSX.Element => { return elements; }; - const fetchRawFilesOverview = async () => { - setIsLoadingRawFilesOverview(true); - - try { - const data = await fetchJSON(`${API_BASE_URL}/files`); - setRawFilesOverview(data); - } catch (fetchError) { - setFetchRawFilesError('Failed to get files due to a network issue.'); - } - - setIsLoadingRawFilesOverview(false); - }; - useEffect(() => { if (!isEditMode) { fetchRawFilesOverview(); @@ -139,9 +141,7 @@ const Files = (): JSX.Element => { )} { - fetchRawFilesOverview(); - }} + onFileUploadComplete={fetchRawFilesOverview} /> {buildFileList()}