aefis-core-ui 2.3.0-rc5 → 2.3.0-rc6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -84,6 +84,7 @@ import FilePondPluginImageExifOrientation from 'filepond-plugin-image-exif-orien
84
84
  import FilePondPluginImagePreview from 'filepond-plugin-image-preview';
85
85
  import { registerPlugin, FilePond } from 'react-filepond';
86
86
  import FilePondPluginGetFile from 'filepond-plugin-get-file';
87
+ import FilePondPluginFileValidateSize from 'filepond-plugin-file-validate-size';
87
88
  import MaskedInput from 'react-text-mask';
88
89
  import StylesProvider from '@mui/styles/StylesProvider';
89
90
 
@@ -16566,14 +16567,6 @@ const render = (value, formState) => {
16566
16567
  }
16567
16568
  return mustache.render(value, formState);
16568
16569
  };
16569
- const renderUrl = (value, formState) => {
16570
- if (value === undefined || value === null) {
16571
- return value;
16572
- }
16573
- return decodeHtml$2(mustache.render(value, formState));
16574
- };
16575
- const useRenderUrl = template => useSelectFormState(state => renderUrl(template, state));
16576
- const useRender = template => useSelectFormState(state => render(template, state));
16577
16570
 
16578
16571
  const useQueryTemplateParam = queryTemplate => {
16579
16572
  const selector = useCallback(state => {
@@ -17069,49 +17062,6 @@ const DisableWrapper = ({
17069
17062
  }));
17070
17063
  };
17071
17064
 
17072
- const ImageUploadReadOnly = ({
17073
- formData,
17074
- schema,
17075
- uiSchema: _uiSchema = {
17076
- "ui:options": {}
17077
- }
17078
- }) => {
17079
- const {
17080
- downloadUrl: downloadUrlTemplate,
17081
- extension: extensionTemplate
17082
- } = _uiSchema["ui:options"];
17083
- const downloadUrl = useRenderUrl(downloadUrlTemplate);
17084
- const extension = useRender(extensionTemplate);
17085
- return /*#__PURE__*/jsxs(Fragment, {
17086
- children: [/*#__PURE__*/jsx(Typography, {
17087
- gutterBottom: true,
17088
- variant: "subtitle2",
17089
- children: schema.title
17090
- }), /*#__PURE__*/jsx(Paper, {
17091
- sx: {
17092
- height: 185,
17093
- justifyContent: "center",
17094
- display: "flex",
17095
- padding: 2,
17096
- overflow: "hidden"
17097
- },
17098
- variant: "outlined",
17099
- children: /*#__PURE__*/jsx(Image, {
17100
- src: downloadUrl,
17101
- style: {
17102
- height: "100%",
17103
- width: "auto"
17104
- },
17105
- extension: extension
17106
- })
17107
- })]
17108
- });
17109
- };
17110
- ImageUploadReadOnly.propTypes = {
17111
- schema: PropTypes.any,
17112
- renderedValues: PropTypes.any
17113
- };
17114
-
17115
17065
  const FieldContainer = props => {
17116
17066
  return /*#__PURE__*/jsx(Box$2, _extends({}, props, {
17117
17067
  sx: _extends({}, props.sx, {
@@ -17123,7 +17073,30 @@ const FieldContainer = props => {
17123
17073
  };
17124
17074
  FieldContainer.propTypes = Box$2.propTypes;
17125
17075
 
17126
- registerPlugin(FilePondPluginFileValidateType, FilePondPluginImageExifOrientation, FilePondPluginImagePreview, FilePondPluginGetFile);
17076
+ registerPlugin(FilePondPluginFileValidateType, FilePondPluginFileValidateSize, FilePondPluginImageExifOrientation, FilePondPluginImagePreview, FilePondPluginGetFile);
17077
+
17078
+ // Check enums below
17079
+ // source:https://github.com/pqina/filepond-docs/blob/master/content/patterns/API/filepond-object.md#filepond-status
17080
+ const FILE_STATUS = {
17081
+ INIT: 1,
17082
+ IDLE: 2,
17083
+ PROCESSING_QUEUED: 9,
17084
+ PROCESSING: 3,
17085
+ PROCESSING_COMPLETE: 5,
17086
+ // upload complete
17087
+ PROCESSING_ERROR: 6,
17088
+ PROCESSING_REVERT_ERROR: 10,
17089
+ LOADING: 7,
17090
+ LOAD_ERROR: 8
17091
+ };
17092
+ const FILE_ORIGIN = {
17093
+ INPUT: 1,
17094
+ // add by user
17095
+ LIMBO: 2,
17096
+ // temp file
17097
+ LOCAL: 3 // pre-loaded files
17098
+ };
17099
+
17127
17100
  const Upload = props => {
17128
17101
  const {
17129
17102
  schema,
@@ -17132,67 +17105,108 @@ const Upload = props => {
17132
17105
  "ui:options": {}
17133
17106
  },
17134
17107
  onChange,
17135
- disabled,
17136
- required
17108
+ disabled
17137
17109
  } = props;
17110
+ const emptyValue = uiSchema["ui:emptyValue"] || null;
17138
17111
  const isReadOnly = useReadOnly();
17139
17112
  const {
17140
- documentName: documentNameTemplate,
17141
- downloadUrl: downloadUrlTemplate,
17142
- extension: extensionTemplate,
17143
17113
  uploadDataSource,
17144
- mapUploadResponse,
17145
17114
  acceptedFileTypes,
17146
17115
  message,
17147
- isImage
17116
+ // TODO: can be handled generic way with HOC
17117
+ isImage = true,
17118
+ // multiple = true, // change to false
17119
+
17120
+ isPreviewHidden = false,
17121
+ idTemplate,
17122
+ downloadUrlTemplate,
17123
+ nameTemplate,
17124
+ mapUploadResponseToIdTemplate,
17125
+ maxFiles = 1,
17126
+ maxFileSize = null,
17127
+ maxTotalFileSize = null
17148
17128
  } = uiSchema["ui:options"];
17149
- const inputProps = uiSchema == null ? void 0 : uiSchema["ui:props"];
17150
- const documentName = useRender(documentNameTemplate);
17151
- const downloadUrl = useRenderUrl(downloadUrlTemplate);
17152
- useRender(extensionTemplate);
17153
- const [files, setFiles] = useState([]);
17154
- const uploadUrl = useMemo(() => getDataSourceUrl(uploadDataSource), [uploadDataSource]);
17155
17129
 
17156
- // INFO: this state is needed to prevent re-load of file during submit
17157
- const [currentFileId, setCurrentFileId] = useState();
17158
-
17159
- // TODO: change this to useMemo with help of useLastestDeps
17160
- useEffect(() => {
17161
- if (formData === currentFileId) return;
17162
- const files = [{
17163
- source: documentName || "File",
17164
- options: {
17165
- type: "local",
17166
- metadata: {
17167
- url: downloadUrl
17130
+ // TODO: implement single vs multipe mode, currently only multiple is active
17131
+ // let _formData = formData; if (multiple && (formData === null ||
17132
+ // formData === undefined)) { _formData = [];
17133
+ // }
17134
+ const _formData = formData || [];
17135
+ const initialFiles = useMemo(() => {
17136
+ return _formData.map(({
17137
+ data
17138
+ }) => {
17139
+ const id = mustache.render(idTemplate, data);
17140
+ const downloadUrl = downloadUrlTemplate ? decodeHtml$2(mustache.render(downloadUrlTemplate, data)) : downloadUrlTemplate;
17141
+ const name = mustache.render(nameTemplate, data);
17142
+ return {
17143
+ // INFO: source appears in preview but also needed to identify
17144
+ // the download URL. Check issue:
17145
+ // https://github.com/pqina/filepond/issues/284
17146
+ source: `${name} id:${id}`,
17147
+ options: {
17148
+ type: "local",
17149
+ origin: downloadUrl,
17150
+ // INFO: id is used in getFileIds function
17151
+ metadata: {
17152
+ url: downloadUrl,
17153
+ id
17154
+ }
17168
17155
  }
17169
- }
17170
- }];
17171
- if (formData != undefined) setFiles(files);
17172
- }, [formData]);
17173
- const handleLoad = (source, load, error) => {
17174
- fetch(downloadUrl).then(res => res.blob()).then(res => {
17156
+ };
17157
+ });
17158
+ }, []);
17159
+ const inputProps = uiSchema == null ? void 0 : uiSchema["ui:props"];
17160
+ const filePondRef = useRef();
17161
+ const getFileIds = () => {
17162
+ var _filePondRef$current;
17163
+ return ((_filePondRef$current = filePondRef.current) == null ? void 0 : _filePondRef$current.getFiles().filter(file => {
17164
+ const {
17165
+ origin,
17166
+ status
17167
+ } = file;
17168
+ if (origin === FILE_ORIGIN.LOCAL) return true;
17169
+ if (origin === FILE_ORIGIN.INPUT && status === FILE_STATUS.PROCESSING_COMPLETE) return true;
17170
+ }).map(file => {
17171
+ if (file.origin === FILE_ORIGIN.LOCAL) return file.getMetadata().id;
17172
+ return file.serverId;
17173
+ }).map(i => i.toString())) || [];
17174
+ };
17175
+ const uploadUrl = useMemo(() => getDataSourceUrl(uploadDataSource), [uploadDataSource]);
17176
+ const handleLoad = (source, load, error, progress, abort, headers) => {
17177
+ // INFO: source appears in preview but also needed to identify
17178
+ // the download URL. Check issue:
17179
+ // https://github.com/pqina/filepond/issues/284
17180
+ const file = initialFiles.find(item => item.source === source);
17181
+ const loadUrl = file.options.origin;
17182
+ fetch(loadUrl).then(res => res.blob()).then(res => {
17175
17183
  const file = res.type === "jpeg/jfif" || !res.type ? new Blob([res], {
17176
17184
  type: "image/jpeg"
17177
17185
  }) : res;
17178
- load(file);
17186
+ const preview = isPreviewHidden ? res : file;
17187
+ load(preview);
17179
17188
  }).catch(error);
17189
+
17190
+ // Should expose an abort method so the request can be cancelled
17191
+ return {
17192
+ abort: () => abort()
17193
+ };
17180
17194
  };
17181
17195
  const handleUploadResponse = res => {
17182
17196
  try {
17183
17197
  const jsonRes = JSON.parse(res);
17184
- const fileUuid = mapUploadResponse ? mustache.render(mapUploadResponse.fileUuid, jsonRes) : jsonRes.DATA.fileUuid;
17185
- setCurrentFileId(fileUuid);
17186
- onChange(fileUuid);
17198
+ const id = mustache.render(mapUploadResponseToIdTemplate, jsonRes);
17199
+ return id;
17187
17200
  } catch (err) {
17188
- console.log(err);
17201
+ var _window$logger;
17202
+ (_window$logger = window.logger) == null ? void 0 : _window$logger.sendLog({
17203
+ name: `Upload response could not be parsed. ${err}`,
17204
+ data: res
17205
+ });
17189
17206
  }
17190
17207
  };
17191
- const handleDelete = () => {
17192
- setCurrentFileId();
17193
- setFiles([]);
17194
- onChange();
17195
- };
17208
+
17209
+ // TODO: move to message HOC
17196
17210
  const messageProps = {
17197
17211
  title: (message == null ? void 0 : message.title) || undefined,
17198
17212
  description: (message == null ? void 0 : message.description) || undefined,
@@ -17202,48 +17216,91 @@ const Upload = props => {
17202
17216
  const {
17203
17217
  error
17204
17218
  } = useNotify();
17205
- const filePondProps = {
17206
- acceptedFileTypes: acceptedFileTypes ? acceptedFileTypes : isImage ? ["image/*"] : null,
17207
- files,
17208
- allowMultiple: false,
17209
- server: {
17210
- url: uploadUrl,
17211
- process: {
17212
- method: "POST",
17213
- headers: {
17214
- Methods: "POST"
17215
- },
17216
- onload: handleUploadResponse,
17217
- onerror: e => {
17218
- var _window$logger;
17219
- const message = `The image could not be uploaded.`;
17220
- error(message);
17221
- (_window$logger = window.logger) == null ? void 0 : _window$logger.sendLog({
17222
- name: message,
17223
- data: e
17224
- });
17225
- }
17219
+ const handleFileChange = () => {
17220
+ const ids = getFileIds();
17221
+
17222
+ // TODO: fill the data
17223
+ const value = ids.map(id => ({
17224
+ value: id,
17225
+ data: {}
17226
+ }));
17227
+ if (value.length === 0) onChange(emptyValue);else onChange(value);
17228
+ };
17229
+
17230
+ // TODO: move to memo component
17231
+ const FileUpload = useMemo(() => {
17232
+ const styles = isReadOnly ? {
17233
+ // INFO: hide drop panel and drop panel
17234
+ "div>div>div.filepond--drop-label": {
17235
+ display: "none"
17226
17236
  },
17227
- load: handleLoad,
17228
- revert: handleDelete
17229
- },
17230
- onupdatefiles: files => {
17231
- if (files.length === 0) {
17232
- onChange(undefined);
17237
+ "button.filepond--action-remove-item": {
17238
+ display: "none"
17233
17239
  }
17234
- setFiles(files);
17235
- },
17236
- allowProcess: false,
17237
- disabled: isReadOnly,
17238
- allowDownloadByUrl: true
17239
- };
17240
- const overrideProps = typeof inputProps === "function" ? inputProps(filePondProps) : inputProps;
17240
+ } : null;
17241
+ const getAcceptedFileTypes = () => {
17242
+ if (acceptedFileTypes) return acceptedFileTypes;
17243
+ if (isImage) return ["image/*"];
17244
+ return null;
17245
+ };
17246
+ const filePondProps = {
17247
+ acceptedFileTypes: getAcceptedFileTypes(),
17248
+ files: initialFiles,
17249
+ // files: undefined,
17250
+ allowMultiple: true,
17251
+ server: {
17252
+ url: uploadUrl,
17253
+ process: {
17254
+ method: "POST",
17255
+ headers: {
17256
+ Methods: "POST"
17257
+ },
17258
+ onload: handleUploadResponse,
17259
+ onerror: e => {
17260
+ var _window$logger2;
17261
+ const message = `The image could not be uploaded.`;
17262
+ error(message);
17263
+ (_window$logger2 = window.logger) == null ? void 0 : _window$logger2.sendLog({
17264
+ name: message,
17265
+ data: e
17266
+ });
17267
+ }
17268
+ },
17269
+ load: handleLoad
17270
+ },
17271
+ onprocessfile: () => handleFileChange(),
17272
+ onremovefile: () => handleFileChange(),
17273
+ maxParallelUploads: 3,
17274
+ allowProcess: false,
17275
+ disabled: isReadOnly,
17276
+ allowDownloadByUrl: true,
17277
+ maxFiles: maxFiles,
17278
+ maxFileSize,
17279
+ maxTotalFileSize,
17280
+ // INFO: allowDrop create problem on file type validation //
17281
+ // (check badge form), for preview image allowDrop: false, //
17282
+ // labelIdle: "Click Here to Browse File(s)" //
17283
+ allowFileTypeValidation: false
17284
+ };
17285
+ const overrideProps = typeof inputProps === "function" ? inputProps(filePondProps) : inputProps;
17286
+ return /*#__PURE__*/jsx(Box$1, {
17287
+ sx: styles,
17288
+ children: /*#__PURE__*/jsx(FilePond, _extends({
17289
+ ref: filePondRef
17290
+ }, filePondProps, overrideProps))
17291
+ });
17292
+ }, []);
17293
+
17294
+ // TODO: better implementation
17241
17295
  if (isReadOnly) {
17242
17296
  if (formData == undefined) return /*#__PURE__*/jsx(ReadOnlyView, {
17243
17297
  label: schema.title,
17244
17298
  emptyViewText: "No file uploaded"
17245
17299
  });
17246
- if (isImage) return /*#__PURE__*/jsx(ImageUploadReadOnly, _extends({}, props));
17300
+ return /*#__PURE__*/jsx(ReadOnlyView, {
17301
+ label: schema.title,
17302
+ renderValue: () => FileUpload
17303
+ });
17247
17304
  }
17248
17305
  return /*#__PURE__*/jsxs(FieldContainer, {
17249
17306
  children: [/*#__PURE__*/jsxs(DisableWrapper, {
@@ -17260,7 +17317,7 @@ const Upload = props => {
17260
17317
  sx: {
17261
17318
  marginTop: 1
17262
17319
  },
17263
- children: /*#__PURE__*/jsx(FilePond, _extends({}, filePondProps, overrideProps))
17320
+ children: FileUpload
17264
17321
  })]
17265
17322
  }), message && /*#__PURE__*/jsx(Alert, _extends({}, messageProps))]
17266
17323
  });