- updated https url;
This commit is contained in:
167
src/components/FileuploadStandalone/index.js
Normal file
167
src/components/FileuploadStandalone/index.js
Normal file
@@ -0,0 +1,167 @@
|
||||
import React, { useEffect, useState, useRef } from 'react';
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { isEmpty } from 'ramda';
|
||||
|
||||
import FileUploadService from '../../service/file-upload-service';
|
||||
|
||||
import { FileUpload } from 'primereact/fileupload';
|
||||
import { Tag } from 'primereact/tag';
|
||||
import { Button } from 'primereact/button';
|
||||
|
||||
const FileuploadStandalone = ({
|
||||
fieldName,
|
||||
setDataFn,
|
||||
defaultValue = [],
|
||||
accept = ['image/*'],
|
||||
doctype = 'images',
|
||||
maxSize = 100000000,
|
||||
emptyText = __('Trascina qui il tuo file', 'gepafin'),
|
||||
chooseLabel = __('Aggiungi immagine', 'gepafin'),
|
||||
multiple = false,
|
||||
sourceId = 0,
|
||||
source = 'application',
|
||||
disabled = false
|
||||
}) => {
|
||||
const [stateFieldData, setStateFieldData] = useState([]);
|
||||
const [acceptFormats, setAcceptFormats] = useState('');
|
||||
const inputRef = useRef();
|
||||
|
||||
const customBase64Uploader = (event) => {
|
||||
const formData = new FormData()
|
||||
for (const file of event.files) {
|
||||
formData.append('file', file)
|
||||
}
|
||||
FileUploadService.uploadFile(sourceId, formData, callback, errorCallback, [
|
||||
['documentType', doctype.toUpperCase()],
|
||||
['sourceType', source.toUpperCase()]
|
||||
]);
|
||||
};
|
||||
|
||||
const callback = (data) => {
|
||||
if (data.status === 'SUCCESS') {
|
||||
setStateFieldData(data.data);
|
||||
const files = inputRef.current.getFiles();
|
||||
inputRef.current.setUploadedFiles(files);
|
||||
inputRef.current.setFiles([]);
|
||||
}
|
||||
}
|
||||
|
||||
const errorCallback = (err) => {
|
||||
console.log('err', err);
|
||||
}
|
||||
|
||||
const itemTemplate = (file) => {
|
||||
return (
|
||||
<div className="appForm__fileUploadItem">
|
||||
<div>
|
||||
<span className="appForm__fileUploadItemName">
|
||||
{file.name}
|
||||
</span>
|
||||
</div>
|
||||
<div>
|
||||
{file.id ? <Tag value={__('Caricato', 'gepafin')} severity="success"></Tag> : null}
|
||||
{!file.id ? <Tag value={__('In attesa', 'gepafin')} severity="warning"></Tag> : null}
|
||||
</div>
|
||||
<div>
|
||||
<Button
|
||||
type="button"
|
||||
disabled={disabled}
|
||||
icon="pi pi-times"
|
||||
severity="danger"
|
||||
aria-label={__('Anulla', 'gepafin')}
|
||||
onClick={() => onTemplateRemove(file)}/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const onTemplateRemove = (file) => {
|
||||
if (file.id) {
|
||||
FileUploadService.deleteFile(
|
||||
{},
|
||||
(data) => dCallback(data, file.id),
|
||||
dErrorCallback,
|
||||
[['id', file.id]]
|
||||
);
|
||||
} else {
|
||||
const files = inputRef.current.getFiles()
|
||||
const newFiles = files.filter(o => o.lastModified !== file.lastModified && o.name !== file.name);
|
||||
inputRef.current.setFiles(newFiles);
|
||||
}
|
||||
}
|
||||
|
||||
const dCallback = (data, id) => {
|
||||
if (data.status === 'SUCCESS') {
|
||||
setStateFieldData(prevState => {
|
||||
const newFiles = prevState.filter(o => o.id !== id);
|
||||
inputRef.current.setUploadedFiles(newFiles);
|
||||
return newFiles;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const dErrorCallback = (err) => {
|
||||
console.log('err', err);
|
||||
}
|
||||
|
||||
const onBeforeDrop = (e) => {
|
||||
return !isEmpty(e.dataTransfer.files) ? validateFileInputType(e.dataTransfer.files) : false;
|
||||
}
|
||||
|
||||
const onBeforeSelect = (e) => {
|
||||
if (e.originalEvent.target.files) {
|
||||
return !isEmpty(e.originalEvent.target.files)
|
||||
? validateFileInputType(e.originalEvent.target.files)
|
||||
: false;
|
||||
}
|
||||
}
|
||||
|
||||
const validateFileInputType = (files) => {
|
||||
const MIMEtype = new RegExp(acceptFormats);
|
||||
|
||||
return Array.prototype.every.call(files, function passesAcceptedFormat(file) {
|
||||
return MIMEtype.test(file.type);
|
||||
});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
setStateFieldData(defaultValue);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
// eslint-disable-next-line no-useless-escape
|
||||
setAcceptFormats(accept.join(',').replace(/\*/g, '.\*').replace(/,/g, '|'));
|
||||
}, [accept]);
|
||||
|
||||
useEffect(() => {
|
||||
if (inputRef.current) {
|
||||
inputRef.current.setUploadedFiles(stateFieldData);
|
||||
}
|
||||
setDataFn(fieldName, [...stateFieldData], { shouldValidate: true });
|
||||
}, [stateFieldData])
|
||||
|
||||
return (
|
||||
sourceId && sourceId !== 0
|
||||
? <FileUpload
|
||||
ref={inputRef}
|
||||
disabled={disabled}
|
||||
id={fieldName}
|
||||
name={fieldName}
|
||||
url={'/document/uploadFile'}
|
||||
multiple={multiple}
|
||||
accept={accept}
|
||||
maxFileSize={maxSize}
|
||||
emptyTemplate={<p>{emptyText}</p>}
|
||||
chooseLabel={chooseLabel}
|
||||
cancelLabel={__('Cancella', 'gepafin')}
|
||||
uploadLabel={__('Carica', 'gepafin')}
|
||||
itemTemplate={itemTemplate}
|
||||
customUpload
|
||||
onBeforeDrop={onBeforeDrop}
|
||||
onBeforeSelect={onBeforeSelect}
|
||||
uploadHandler={customBase64Uploader}/>
|
||||
: null
|
||||
)
|
||||
}
|
||||
|
||||
export default FileuploadStandalone;
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { useEffect, useState, useRef } from 'react';
|
||||
import { classNames } from 'primereact/utils';
|
||||
import { head } from 'ramda'
|
||||
import { head, isEmpty } from 'ramda'
|
||||
import { __ } from '@wordpress/i18n';
|
||||
|
||||
import FileUploadService from '../../../../service/file-upload-service';
|
||||
@@ -111,7 +111,15 @@ const Fileupload = ({
|
||||
}
|
||||
|
||||
const onBeforeDrop = (e) => {
|
||||
return validateFileInputType(e.dataTransfer.files);
|
||||
return !isEmpty(e.dataTransfer.files) ? validateFileInputType(e.dataTransfer.files) : false;
|
||||
}
|
||||
|
||||
const onBeforeSelect = (e) => {
|
||||
if (e.originalEvent.target.files) {
|
||||
return !isEmpty(e.originalEvent.target.files)
|
||||
? validateFileInputType(e.originalEvent.target.files)
|
||||
: false;
|
||||
}
|
||||
}
|
||||
|
||||
const validateFileInputType = (files) => {
|
||||
@@ -178,6 +186,7 @@ const Fileupload = ({
|
||||
itemTemplate={itemTemplate}
|
||||
customUpload
|
||||
onBeforeDrop={onBeforeDrop}
|
||||
onBeforeSelect={onBeforeSelect}
|
||||
uploadHandler={customBase64Uploader}/>
|
||||
{infoText ? <small>{infoText}</small> : null}
|
||||
</>
|
||||
|
||||
@@ -7,7 +7,7 @@ import FileUploadService from '../../../../service/file-upload-service';
|
||||
import { FileUpload } from 'primereact/fileupload';
|
||||
import { Tag } from 'primereact/tag';
|
||||
import { Button } from 'primereact/button';
|
||||
import { head } from 'ramda';
|
||||
import { head, isEmpty } from 'ramda';
|
||||
import { mimeTypes } from '../../../../configData';
|
||||
|
||||
const FileuploadAsync = ({
|
||||
@@ -112,7 +112,15 @@ const FileuploadAsync = ({
|
||||
}
|
||||
|
||||
const onBeforeDrop = (e) => {
|
||||
return validateFileInputType(e.dataTransfer.files);
|
||||
return !isEmpty(e.dataTransfer.files) ? validateFileInputType(e.dataTransfer.files) : false;
|
||||
}
|
||||
|
||||
const onBeforeSelect = (e) => {
|
||||
if (e.originalEvent.target.files) {
|
||||
return !isEmpty(e.originalEvent.target.files)
|
||||
? validateFileInputType(e.originalEvent.target.files)
|
||||
: false;
|
||||
}
|
||||
}
|
||||
|
||||
const validateFileInputType = (files) => {
|
||||
@@ -179,6 +187,7 @@ const FileuploadAsync = ({
|
||||
itemTemplate={itemTemplate}
|
||||
customUpload
|
||||
onBeforeDrop={onBeforeDrop}
|
||||
onBeforeSelect={onBeforeSelect}
|
||||
uploadHandler={customBase64Uploader}/>
|
||||
{infoText ? <small>{infoText}</small> : null}
|
||||
</>
|
||||
|
||||
@@ -20,7 +20,7 @@ const NumberInput = ({
|
||||
min,
|
||||
max,
|
||||
disabled = false,
|
||||
useGrouping = false
|
||||
useGrouping = true
|
||||
}) => {
|
||||
const input = <Controller
|
||||
name={fieldName}
|
||||
|
||||
@@ -20,7 +20,7 @@ const TopBarProfileMenu = ({ menuLeftRef }) => {
|
||||
const renderCompanyItem = (item) => (
|
||||
<span className="topBar__menuCompanyItem"
|
||||
onClick={chosenCompanyId === item.companyId ? () => {
|
||||
} : switchCompany(item.companyId)}
|
||||
} : () => switchCompany(item.companyId)}
|
||||
data-id={item.companyId}
|
||||
data-active={chosenCompanyId === item.companyId}>
|
||||
<i className="pi pi-building-columns"></i>
|
||||
|
||||
Reference in New Issue
Block a user