2020-04-16 06:56:43 +00:00
|
|
|
import React from "react";
|
2019-11-05 05:09:50 +00:00
|
|
|
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { WidgetType } from "constants/WidgetConstants";
|
2020-04-16 06:56:43 +00:00
|
|
|
import FilePickerComponent from "components/designSystems/appsmith/FilePickerComponent";
|
2019-11-05 05:09:50 +00:00
|
|
|
import Uppy from "@uppy/core";
|
|
|
|
|
import GoogleDrive from "@uppy/google-drive";
|
|
|
|
|
import Webcam from "@uppy/webcam";
|
|
|
|
|
import Url from "@uppy/url";
|
|
|
|
|
import OneDrive from "@uppy/onedrive";
|
2020-03-16 07:59:07 +00:00
|
|
|
import {
|
|
|
|
|
WidgetPropertyValidationType,
|
|
|
|
|
BASE_WIDGET_VALIDATION,
|
|
|
|
|
} from "utils/ValidationFactory";
|
2019-11-22 13:12:39 +00:00
|
|
|
import { VALIDATION_TYPES } from "constants/WidgetValidation";
|
2020-03-13 07:24:03 +00:00
|
|
|
import { EventType, ExecutionResult } from "constants/ActionConstants";
|
2020-03-06 09:45:21 +00:00
|
|
|
import {
|
|
|
|
|
DerivedPropertiesMap,
|
|
|
|
|
TriggerPropertiesMap,
|
|
|
|
|
} from "utils/WidgetFactory";
|
2020-02-22 01:39:28 +00:00
|
|
|
import Dashboard from "@uppy/dashboard";
|
2020-02-24 14:28:20 +00:00
|
|
|
import shallowequal from "shallowequal";
|
2020-03-06 09:20:18 +00:00
|
|
|
import _ from "lodash";
|
2019-11-05 05:09:50 +00:00
|
|
|
|
2020-06-08 11:29:20 +00:00
|
|
|
class FilePickerWidget extends BaseWidget<
|
|
|
|
|
FilePickerWidgetProps,
|
|
|
|
|
FilePickerWidgetState
|
|
|
|
|
> {
|
2019-11-05 05:09:50 +00:00
|
|
|
uppy: any;
|
|
|
|
|
|
|
|
|
|
constructor(props: FilePickerWidgetProps) {
|
|
|
|
|
super(props);
|
2020-06-08 11:29:20 +00:00
|
|
|
this.state = {
|
|
|
|
|
version: 0,
|
|
|
|
|
};
|
2019-11-05 05:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
2019-11-22 13:12:39 +00:00
|
|
|
static getPropertyValidationMap(): WidgetPropertyValidationType {
|
|
|
|
|
return {
|
2020-03-16 07:59:07 +00:00
|
|
|
...BASE_WIDGET_VALIDATION,
|
2019-11-22 13:12:39 +00:00
|
|
|
label: VALIDATION_TYPES.TEXT,
|
|
|
|
|
maxNumFiles: VALIDATION_TYPES.NUMBER,
|
|
|
|
|
allowedFileTypes: VALIDATION_TYPES.ARRAY,
|
2020-03-16 07:59:07 +00:00
|
|
|
files: VALIDATION_TYPES.ARRAY,
|
2020-03-06 09:45:21 +00:00
|
|
|
isRequired: VALIDATION_TYPES.BOOLEAN,
|
2020-07-06 08:16:42 +00:00
|
|
|
// onFilesSelected: VALIDATION_TYPES.ACTION_SELECTOR,
|
2020-03-06 09:45:21 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getDerivedPropertiesMap(): DerivedPropertiesMap {
|
|
|
|
|
return {
|
|
|
|
|
isValid: `{{ this.isRequired ? this.files.length > 0 : true }}`,
|
2020-06-09 13:10:42 +00:00
|
|
|
value: `{{this.files}}`,
|
2019-11-22 13:12:39 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 16:15:09 +00:00
|
|
|
static getMetaPropertiesMap(): Record<string, any> {
|
|
|
|
|
return {
|
|
|
|
|
files: [],
|
|
|
|
|
uploadedFileData: {},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 05:09:50 +00:00
|
|
|
refreshUppy = (props: FilePickerWidgetProps) => {
|
|
|
|
|
this.uppy = Uppy({
|
|
|
|
|
id: this.props.widgetId,
|
2020-02-21 13:17:52 +00:00
|
|
|
autoProceed: false,
|
2019-11-05 05:09:50 +00:00
|
|
|
allowMultipleUploads: true,
|
|
|
|
|
debug: false,
|
|
|
|
|
restrictions: {
|
2020-03-11 14:37:12 +00:00
|
|
|
maxFileSize: props.maxFileSize ? props.maxFileSize * 1024 * 1024 : null,
|
2019-11-05 05:09:50 +00:00
|
|
|
maxNumberOfFiles: props.maxNumFiles,
|
|
|
|
|
minNumberOfFiles: null,
|
2020-03-06 09:20:18 +00:00
|
|
|
allowedFileTypes:
|
|
|
|
|
props.allowedFileTypes &&
|
|
|
|
|
(props.allowedFileTypes.includes("*") ||
|
|
|
|
|
_.isEmpty(props.allowedFileTypes))
|
|
|
|
|
? null
|
|
|
|
|
: props.allowedFileTypes,
|
2019-11-05 05:09:50 +00:00
|
|
|
},
|
|
|
|
|
})
|
2020-02-22 01:39:28 +00:00
|
|
|
.use(Dashboard, {
|
|
|
|
|
target: "body",
|
|
|
|
|
metaFields: [],
|
|
|
|
|
inline: false,
|
|
|
|
|
width: 750,
|
|
|
|
|
height: 550,
|
|
|
|
|
thumbnailWidth: 280,
|
|
|
|
|
showLinkToFileUploadResult: true,
|
|
|
|
|
showProgressDetails: false,
|
|
|
|
|
hideUploadButton: false,
|
|
|
|
|
hideProgressAfterFinish: false,
|
|
|
|
|
note: null,
|
2020-02-24 06:29:39 +00:00
|
|
|
closeAfterFinish: true,
|
2020-02-22 01:39:28 +00:00
|
|
|
closeModalOnClickOutside: true,
|
|
|
|
|
disableStatusBar: false,
|
|
|
|
|
disableInformer: false,
|
|
|
|
|
disableThumbnailGenerator: false,
|
|
|
|
|
disablePageScrollWhenModalOpen: true,
|
|
|
|
|
proudlyDisplayPoweredByUppy: false,
|
|
|
|
|
onRequestCloseModal: () => {
|
|
|
|
|
this.uppy.getPlugin("Dashboard").closeModal();
|
|
|
|
|
},
|
|
|
|
|
locale: {},
|
|
|
|
|
})
|
2019-11-05 05:09:50 +00:00
|
|
|
.use(GoogleDrive, { companionUrl: "https://companion.uppy.io" })
|
|
|
|
|
.use(Url, { companionUrl: "https://companion.uppy.io" })
|
|
|
|
|
.use(OneDrive, {
|
|
|
|
|
companionUrl: "https://companion.uppy.io/",
|
|
|
|
|
})
|
|
|
|
|
.use(Webcam, {
|
|
|
|
|
onBeforeSnapshot: () => Promise.resolve(),
|
|
|
|
|
countdown: false,
|
|
|
|
|
mirror: true,
|
|
|
|
|
facingMode: "user",
|
|
|
|
|
locale: {},
|
|
|
|
|
});
|
2020-02-24 06:29:39 +00:00
|
|
|
this.uppy.on("file-removed", (file: any) => {
|
2020-03-13 07:24:03 +00:00
|
|
|
const updatedFiles = this.props.files
|
|
|
|
|
? this.props.files.filter(dslFile => {
|
|
|
|
|
return file.id !== dslFile.id;
|
|
|
|
|
})
|
|
|
|
|
: [];
|
2020-02-24 06:29:39 +00:00
|
|
|
this.updateWidgetMetaProperty("files", updatedFiles);
|
|
|
|
|
});
|
2020-02-21 13:17:52 +00:00
|
|
|
this.uppy.on("file-added", (file: any) => {
|
2020-03-13 07:24:03 +00:00
|
|
|
const dslFiles = this.props.files || [];
|
2020-02-24 06:29:39 +00:00
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.readAsDataURL(file.data);
|
|
|
|
|
reader.onloadend = () => {
|
|
|
|
|
const base64data = reader.result;
|
|
|
|
|
const newFile = {
|
|
|
|
|
id: file.id,
|
|
|
|
|
base64: base64data,
|
|
|
|
|
blob: file.data,
|
|
|
|
|
};
|
|
|
|
|
dslFiles.push(newFile);
|
|
|
|
|
this.updateWidgetMetaProperty("files", dslFiles);
|
|
|
|
|
};
|
2020-02-21 13:17:52 +00:00
|
|
|
});
|
2020-02-22 01:39:28 +00:00
|
|
|
this.uppy.on("upload", () => {
|
2020-02-24 06:29:39 +00:00
|
|
|
this.onFilesSelected();
|
2020-02-21 13:17:52 +00:00
|
|
|
});
|
2020-06-08 11:29:20 +00:00
|
|
|
this.setState({ version: this.state.version + 1 });
|
2019-11-05 05:09:50 +00:00
|
|
|
};
|
|
|
|
|
|
2020-02-21 13:17:52 +00:00
|
|
|
static getTriggerPropertyMap(): TriggerPropertiesMap {
|
|
|
|
|
return {
|
|
|
|
|
onFilesSelected: true,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onFilesSelected() {
|
|
|
|
|
if (this.props.onFilesSelected) {
|
|
|
|
|
this.executeAction({
|
|
|
|
|
dynamicString: this.props.onFilesSelected,
|
|
|
|
|
event: {
|
|
|
|
|
type: EventType.ON_FILES_SELECTED,
|
2020-03-13 07:24:03 +00:00
|
|
|
callback: this.handleFileUploaded,
|
2020-02-21 13:17:52 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 07:24:03 +00:00
|
|
|
handleFileUploaded = (result: ExecutionResult) => {
|
|
|
|
|
if (result.success) {
|
|
|
|
|
this.updateWidgetMetaProperty(
|
2020-06-08 11:09:13 +00:00
|
|
|
"uploadedFileUrls",
|
|
|
|
|
this.props.uploadedFileUrlPaths,
|
2020-03-13 07:24:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-05 05:09:50 +00:00
|
|
|
componentDidUpdate(prevProps: FilePickerWidgetProps) {
|
|
|
|
|
super.componentDidUpdate(prevProps);
|
|
|
|
|
if (
|
2020-03-13 07:24:03 +00:00
|
|
|
prevProps.files &&
|
|
|
|
|
prevProps.files.length > 0 &&
|
|
|
|
|
this.props.files === undefined
|
|
|
|
|
) {
|
|
|
|
|
this.uppy.reset();
|
|
|
|
|
} else if (
|
2020-02-24 14:28:20 +00:00
|
|
|
!shallowequal(prevProps.allowedFileTypes, this.props.allowedFileTypes) ||
|
2020-03-06 09:20:18 +00:00
|
|
|
prevProps.maxNumFiles !== this.props.maxNumFiles ||
|
2020-03-10 10:42:04 +00:00
|
|
|
prevProps.maxFileSize !== this.props.maxFileSize
|
2019-11-05 05:09:50 +00:00
|
|
|
) {
|
|
|
|
|
this.refreshUppy(this.props);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
super.componentDidMount();
|
|
|
|
|
this.refreshUppy(this.props);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
this.uppy.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPageView() {
|
|
|
|
|
return (
|
2020-04-16 06:56:43 +00:00
|
|
|
<FilePickerComponent
|
|
|
|
|
uppy={this.uppy}
|
|
|
|
|
widgetId={this.props.widgetId}
|
|
|
|
|
key={this.props.widgetId}
|
|
|
|
|
label={this.props.label}
|
|
|
|
|
files={this.props.files || []}
|
|
|
|
|
isLoading={this.props.isLoading}
|
|
|
|
|
/>
|
2019-11-05 05:09:50 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getWidgetType(): WidgetType {
|
|
|
|
|
return "FILE_PICKER_WIDGET";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 11:29:20 +00:00
|
|
|
export interface FilePickerWidgetState extends WidgetState {
|
|
|
|
|
version: number;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 05:09:50 +00:00
|
|
|
export interface FilePickerWidgetProps extends WidgetProps {
|
|
|
|
|
label: string;
|
|
|
|
|
maxNumFiles?: number;
|
2020-03-06 09:20:18 +00:00
|
|
|
maxFileSize?: number;
|
2020-03-13 07:24:03 +00:00
|
|
|
files?: any[];
|
2019-11-05 05:09:50 +00:00
|
|
|
allowedFileTypes: string[];
|
2020-02-21 13:17:52 +00:00
|
|
|
onFilesSelected?: string;
|
2020-03-06 09:45:21 +00:00
|
|
|
isRequired?: boolean;
|
2020-06-08 11:09:13 +00:00
|
|
|
uploadedFileUrlPaths?: string;
|
2019-11-05 05:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default FilePickerWidget;
|