18169: Removed cancel disable when uploading
authorDaniel Kutyła <daniel.kutyla@contractors.roche.com>
Mon, 18 Oct 2021 12:09:13 +0000 (14:09 +0200)
committerDaniel Kutyła <daniel.kutyla@contractors.roche.com>
Mon, 18 Oct 2021 12:09:13 +0000 (14:09 +0200)
Arvados-DCO-1.1-Signed-off-by: Daniel Kutyła <daniel.kutyla@contractors.roche.com>

src/common/webdav.ts
src/components/form-dialog/form-dialog.tsx
src/services/collection-service/collection-service.ts
src/store/collections/collection-upload-actions.ts
src/store/file-uploader/file-uploader-actions.ts
src/store/file-uploader/file-uploader-reducer.ts
src/views-components/dialog-upload/dialog-collection-files-upload.tsx
src/views-components/file-uploader/file-uploader.tsx

index 758a5e18e1e9ad44015b9a802d0e532e70ba3d76..93ec21cb724f26d2ede4cf9be4b211defaf85b9c 100644 (file)
@@ -84,6 +84,15 @@ export class WebDAV {
                 .keys(headers)
                 .forEach(key => r.setRequestHeader(key, headers[key]));
 
+            if (!(window as any).cancelTokens) {
+                Object.assign(window, { cancelTokens: {} });
+            }
+
+            (window as any).cancelTokens[config.url] = () => { 
+                resolve(r);
+                r.abort();
+            }
+
             if (config.onUploadProgress) {
                 r.upload.addEventListener('progress', config.onUploadProgress);
             }
index 19145cea34ae12b62813742e8c20acfb419b5f13..0fc799dee92b62eb0ab58a6ce100403dc7477193 100644 (file)
@@ -42,7 +42,9 @@ interface DialogProjectDataProps {
     dialogTitle: string;
     formFields: React.ComponentType<InjectedFormProps<any> & WithDialogProps<any>>;
     submitLabel?: string;
+    cancelCallback?: Function;
     enableWhenPristine?: boolean;
+    doNotDisableCancel?: boolean;
 }
 
 type DialogProjectProps = DialogProjectDataProps & WithDialogProps<{}> & InjectedFormProps<any> & WithStyles<CssRules>;
@@ -65,10 +67,18 @@ export const FormDialog = withStyles(styles)((props: DialogProjectProps) =>
             <DialogActions className={props.classes.dialogActions}>
                 <Button
                     data-cy='form-cancel-btn'
-                    onClick={props.closeDialog}
+                    onClick={() => {
+                        props.closeDialog();
+
+                        if (props.cancelCallback) {
+                            props.cancelCallback();
+                            props.reset();
+                            props.initialize({});
+                        }
+                    }}
                     className={props.classes.button}
                     color="primary"
-                    disabled={props.submitting}>
+                    disabled={props.doNotDisableCancel ? false : props.submitting}>
                     {props.cancelLabel || 'Cancel'}
                 </Button>
                 <Button
index 92437806818eb33a77c23c8bd2c1183a453cf3d9..52fbf1a5c50b8cf8e198b0b88b6739934e206651 100644 (file)
@@ -107,7 +107,7 @@ export class CollectionService extends TrashableResourceService<CollectionResour
             },
             onUploadProgress: (e: ProgressEvent) => {
                 onProgress(fileId, e.loaded, e.total, Date.now());
-            }
+            },
         };
         return this.webdavClient.upload(fileURL, [file], requestConfig);
     }
index 8f85ea18a6c4b081238801d5394788f950c81c9e..0ca681b98ed2fcc9d8550165b477231f58fa0287 100644 (file)
@@ -14,12 +14,14 @@ import { progressIndicatorActions } from "store/progress-indicator/progress-indi
 import { collectionPanelFilesAction } from 'store/collection-panel/collection-panel-files/collection-panel-files-actions';
 import { createTree } from 'models/tree';
 import { loadCollectionPanel } from '../collection-panel/collection-panel-action';
+import * as WorkbenchActions from 'store/workbench/workbench-actions';
 
 export const uploadCollectionFiles = (collectionUuid: string) =>
-    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+    async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
         dispatch(fileUploaderActions.START_UPLOAD());
         const files = getState().fileUploader.map(file => file.file);
         await services.collectionService.uploadFiles(collectionUuid, files, handleUploadProgress(dispatch));
+        dispatch(WorkbenchActions.loadCollection(collectionUuid));
         dispatch(fileUploaderActions.CLEAR_UPLOAD());
     };
 
index 8436c485f9ba098efbe0fa880c44c8df7f0c4cb9..a397bbd825f23d7f936fa63470b2a091f27a2642 100644 (file)
@@ -24,6 +24,7 @@ export const fileUploaderActions = unionize({
     SET_UPLOAD_PROGRESS: ofType<{ fileId: number, loaded: number, total: number, currentTime: number }>(),
     START_UPLOAD: ofType(),
     DELETE_UPLOAD_FILE: ofType<UploadFile>(),
+    CANCEL_FILES_UPLOAD: ofType(),
 });
 
 export type FileUploaderAction = UnionOf<typeof fileUploaderActions>;
index c1f9c6810958467629ecdddb0d74fb00035f35fd..bade4c8f88082aa6057d6e8f7c1d07f2cc134979 100644 (file)
@@ -41,8 +41,25 @@ export const fileUploaderReducer = (state: UploaderState = initialState, action:
             const idToDelete: number = file.id;
             const updatedState = state.filter(file => file.id !== idToDelete);
 
+            const key: string | undefined = Object.keys((window as any).cancelTokens)
+                .find(key => key.indexOf(file.file.name) > -1);
+
+            if (key) {
+                (window as any).cancelTokens[key]();
+                delete (window as any).cancelTokens[key];
+            }
+
             return updatedState;
         },
+        CANCEL_FILES_UPLOAD: () => {
+            Object.keys((window as any).cancelTokens)
+                .forEach((key) => {
+                    (window as any).cancelTokens[key]();
+                    delete (window as any).cancelTokens[key];
+                });
+
+            return state;
+        },
         START_UPLOAD: () => {
             const startTime = Date.now();
             return state.map(f => ({ ...f, startTime, prevTime: startTime }));
index 2f662bfae06ffa20982f8f23a409ef6bf8dc9ac5..f65bdabfeb935ef79a93609a5beed7f3988a66d6 100644 (file)
@@ -10,16 +10,30 @@ import { FormDialog } from 'components/form-dialog/form-dialog';
 import { require } from 'validators/require';
 import { FileUploaderField } from 'views-components/file-uploader/file-uploader';
 import { WarningCollection } from 'components/warning-collection/warning-collection';
+import { fileUploaderActions } from 'store/file-uploader/file-uploader-actions';
+import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
 
 type DialogCollectionFilesUploadProps = WithDialogProps<{}> & InjectedFormProps<CollectionCreateFormDialogData>;
 
-export const DialogCollectionFilesUpload = (props: DialogCollectionFilesUploadProps) =>
-    <FormDialog
+export const DialogCollectionFilesUpload = (props: DialogCollectionFilesUploadProps) => {
+
+    return <FormDialog
         dialogTitle='Upload data'
         formFields={UploadCollectionFilesFields}
         submitLabel='Upload data'
+        doNotDisableCancel
+        cancelCallback={() => {
+            const { submitting, dispatch } = (props as any);
+
+            if (submitting) {
+                dispatch(progressIndicatorActions.STOP_WORKING('uploadCollectionFilesDialog'));
+                dispatch(fileUploaderActions.CANCEL_FILES_UPLOAD());
+                dispatch(fileUploaderActions.CLEAR_UPLOAD());
+            }
+        }}
         {...props}
     />;
+}
 
 const UploadCollectionFilesFields = () => <>
     <Field
index 82e400f78a241a114f72999f3e5fae735a7c0fc9..cde286c450c4764be139b288c65ab3ab55f9d4e2 100644 (file)
@@ -30,7 +30,7 @@ const mapDispatchToProps = (dispatch: Dispatch, { onDrop }: FileUploaderProps):
             onDrop(files);
         }
     },
-    onDelete: file => dispatch(fileUploaderActions.DELETE_UPLOAD_FILE(file)),
+    onDelete: file => dispatch(fileUploaderActions.DELETE_UPLOAD_FILE(file))
 });
 
 export const FileUploader = connect(mapStateToProps, mapDispatchToProps)(FileUpload);
@@ -38,5 +38,5 @@ export const FileUploader = connect(mapStateToProps, mapDispatchToProps)(FileUpl
 export const FileUploaderField = (props: WrappedFieldProps & { label?: string }) =>
     <div>
         <Typography variant='caption'>{props.label}</Typography>
-        <FileUploader disabled={props.meta.submitting} onDrop={props.input.onChange} />
+        <FileUploader disabled={false} onDrop={props.input.onChange} />
     </div>;