Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-4980 cancel file upload #2573

Merged
merged 6 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions webapp/packages/core-blocks/src/Snackbars/ProcessSnackbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import { SnackbarWrapper } from './SnackbarMarkups/SnackbarWrapper';
export interface ProcessSnackbarProps extends INotificationProcessExtraProps {
closeDelay?: number;
displayDelay?: number;
onCancel?: () => void | Promise<void>;
}

export const ProcessSnackbar: NotificationComponent<ProcessSnackbarProps> = observer(function ProcessSnackbar({
closeDelay = 3000,
displayDelay = 750,
notification,
state,
onCancel,
}) {
const { error, title, message, status } = state!;

Expand All @@ -52,12 +54,13 @@ export const ProcessSnackbar: NotificationComponent<ProcessSnackbarProps> = obse
return null;
}

function close() {
onCancel?.();
notification.close(false);
}
Wroud marked this conversation as resolved.
Show resolved Hide resolved

return (
<SnackbarWrapper
closing={!!notification.state.deleteDelay}
persistent={status === ENotificationType.Loading}
onClose={() => notification.close(false)}
>
<SnackbarWrapper closing={!!notification.state.deleteDelay} persistent={status === ENotificationType.Loading} onClose={close}>
<SnackbarStatus status={status} />
<SnackbarContent>
<SnackbarBody title={translate(title)}>{message && translate(message)}</SnackbarBody>
Expand All @@ -67,6 +70,12 @@ export const ProcessSnackbar: NotificationComponent<ProcessSnackbarProps> = obse
{translate('ui_errors_details')}
</Button>
)}

{onCancel && status === ENotificationType.Loading && (
<Button mod={['unelevated']} onClick={onCancel}>
{translate('ui_processing_cancel')}
</Button>
)}
</SnackbarFooter>
</SnackbarContent>
</SnackbarWrapper>
Expand Down
1 change: 1 addition & 0 deletions webapp/packages/core-localization/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default [
['ui_processing_loading', 'Loading...'],
['ui_processing_cancel', 'Cancel'],
['ui_processing_canceling', 'Cancelling...'],
['ui_processing_canceled', 'Canceled'],
['ui_processing_reload', 'Reload'],
['ui_processing_retry', 'Retry'],
['ui_processing_ok', 'Ok'],
Expand Down
1 change: 1 addition & 0 deletions webapp/packages/core-localization/src/locales/it.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default [
['ui_processing_loading', 'Caricamento...'],
['ui_processing_cancel', 'Annulla'],
['ui_processing_canceling', 'Annullamento...'],
['ui_processing_canceled', 'Canceled'],
['ui_processing_retry', 'Riprova'],
['ui_processing_ok', 'Ok'],
['ui_processing_create', 'Crea'],
Expand Down
1 change: 1 addition & 0 deletions webapp/packages/core-localization/src/locales/ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default [
['ui_processing_loading', 'Загрузка...'],
['ui_processing_cancel', 'Отменить'],
['ui_processing_canceling', 'Отмена...'],
['ui_processing_canceled', 'Отменено'],
['ui_processing_reload', 'Перезагрузить'],
['ui_processing_retry', 'Повторить'],
['ui_processing_ok', 'Принять'],
Expand Down
1 change: 1 addition & 0 deletions webapp/packages/core-localization/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default [
['ui_processing_loading', '加载中...'],
['ui_processing_cancel', '取消'],
['ui_processing_canceling', '取消中...'],
['ui_processing_canceled', 'Canceled'],
['ui_processing_retry', '重试'],
['ui_processing_ok', '好'],
['ui_processing_create', '创建'],
Expand Down
11 changes: 9 additions & 2 deletions webapp/packages/core-sdk/src/CustomGraphQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import axios, { AxiosProgressEvent, AxiosResponse, isAxiosError } from 'axios';
import axios, { AxiosProgressEvent, AxiosResponse, CanceledError, isAxiosError, isCancel } from 'axios';
import { ClientError, GraphQLClient, RequestDocument, RequestOptions, resolveRequestDocument, Variables } from 'graphql-request';

import { GQLError } from './GQLError';
Expand Down Expand Up @@ -36,10 +36,11 @@ export class CustomGraphQLClient extends GraphQLClient {
query?: string,
variables?: V,
onUploadProgress?: (event: UploadProgressEvent) => void,
signal?: AbortSignal,
): Promise<T> {
return this.interceptors.reduce(
(accumulator, interceptor) => interceptor(accumulator),
this.overrideFilesUpload<T, V>(url, file, query, variables, onUploadProgress),
this.overrideFilesUpload<T, V>(url, file, query, variables, onUploadProgress, signal),
);
}

Expand Down Expand Up @@ -126,6 +127,7 @@ export class CustomGraphQLClient extends GraphQLClient {
query?: string,
variables?: V,
onUploadProgress?: (event: UploadProgressEvent) => void,
signal?: AbortSignal,
): Promise<T> {
this.blockRequestsReasonHandler();
try {
Expand All @@ -146,6 +148,7 @@ export class CustomGraphQLClient extends GraphQLClient {
}

const response = await axios.postForm/*<GqlResponse>*/ <T>(url, data, {
signal,
onUploadProgress,
responseType: 'json',
});
Expand All @@ -156,6 +159,10 @@ export class CustomGraphQLClient extends GraphQLClient {

return response.data;
} catch (error: any) {
if (isCancel(error)) {
throw new CanceledError('ui_processing_canceled');
}

if (isAxiosError(error) && error.response?.data.message) {
throw new ServerInternalError({ ...error, message: error.response.data.message });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface IUploadResultDataExtension {
processorId: string,
file: File,
onUploadProgress?: (event: UploadProgressEvent) => void,
signal?: AbortSignal,
) => Promise<AsyncTaskInfo>;
}

Expand All @@ -32,13 +33,15 @@ export function uploadResultDataExtension(client: CustomGraphQLClient): IUploadR
processorId: string,
file: File,
onUploadProgress?: (event: UploadProgressEvent) => void,
signal?: AbortSignal,
): Promise<AsyncTaskInfo> {
return client.uploadFile(
GlobalConstants.absoluteServiceUrl('data', 'import'),
file,
undefined,
{ connectionId, contextId, projectId, resultsId, processorId },
onUploadProgress,
signal,
);
},
};
Expand Down
39 changes: 31 additions & 8 deletions webapp/packages/plugin-data-import/src/DataImportService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,49 @@ export class DataImportService {
}

async importData(connectionId: string, contextId: string, projectId: string, resultsId: string, processorId: string, file: File) {
const abortController = new AbortController();
let cancelImplementation: (() => void | Promise<void>) | null;

function cancel() {
cancelImplementation?.();
}

const { controller, notification } = this.notificationService.processNotification(
() => ProcessSnackbar,
{},
{
onCancel: cancel,
},
{ title: 'plugin_data_import_process_title', message: file.name },
Wroud marked this conversation as resolved.
Show resolved Hide resolved
);

try {
const result = await this.graphQLService.sdk.uploadResultData(connectionId, contextId, projectId, resultsId, processorId, file, event => {
if (event.total !== undefined) {
const percentCompleted = getProgressPercent(event.loaded, event.total);
cancelImplementation = () => abortController.abort();

if (notification.message) {
controller.setMessage(`${percentCompleted}%\n${notification.message}`);
const result = await this.graphQLService.sdk.uploadResultData(
connectionId,
contextId,
projectId,
resultsId,
processorId,
file,
event => {
if (event.total !== undefined) {
const percentCompleted = getProgressPercent(event.loaded, event.total);

if (notification.message) {
controller.setMessage(`${percentCompleted}%\n${notification.message}`);
}
}
}
});
},
abortController.signal,
);

const task = this.asyncTaskInfoService.create(async () => {
const { taskInfo } = await this.graphQLService.sdk.getAsyncTaskInfo({ taskId: result.id, removeOnFinish: false });
return taskInfo;
});

cancelImplementation = () => this.asyncTaskInfoService.cancel(task.id);
controller.setMessage('plugin_data_import_process_file_processing_step_message');
await this.asyncTaskInfoService.run(task);

Expand All @@ -69,6 +90,8 @@ export class DataImportService {
} catch (exception: any) {
controller.reject(exception, 'plugin_data_import_process_fail');
return false;
} finally {
cancelImplementation = null;
}
}
}
Loading