/* eslint-disable react/no-multi-comp */ import * as React from 'react'; import {AttachmentsList} from './AttachmentsList'; import {AttachmentsEditorModal} from './AttachmentsEditorModal'; import {showModal} from 'core/services/modalService'; import {gettext} from 'core/utils'; import {IAttachment, IAttachmentsWidgetProps} from 'superdesk-api'; import {appConfig} from 'appConfig'; import {attachmentsApi} from './attachmentsService'; import {showUploadAttachmentsModal} from './UploadAttachmentsModal'; export class AttachmentsWidgetComponent extends React.PureComponent { fileInputNode: React.RefObject; constructor(props: IAttachmentsWidgetProps) { super(props); this.fileInputNode = React.createRef(); this.showFileUploadModal = this.showFileUploadModal.bind(this); this.onAddFiles = this.onAddFiles.bind(this); this.onDragFiles = this.onDragFiles.bind(this); this.onDropFiles = this.onDropFiles.bind(this); this.editAttachment = this.editAttachment.bind(this); } showFileUploadModal() { if (this.fileInputNode.current != null) { this.fileInputNode.current.click(); } } onAddFiles(event: React.ChangeEvent) { event.preventDefault(); this.showUploadModal(Array.from(event.target.files ?? [])); event.target.value = null; // reset to allow selecting same file again } onDragFiles(event: React.DragEvent) { // Prevents Browser default action, such as open in new tab event.preventDefault(); } onDropFiles(event: React.DragEvent) { event.preventDefault(); this.showUploadModal(Array.from(event.dataTransfer.files)); } showUploadModal(files: Array) { if (this.props.isUploadValid(files) === true) { showUploadAttachmentsModal({ files: files, onUploaded: (attachments: Array) => { this.props.addAttachments(attachments); }, }); } } editAttachment(attachment: IAttachment) { showModal(({closeModal}) => ( { attachmentsApi.save(original, updates) .then((updated) => { this.props.updateAttachment(updated); closeModal(); }); }} /> )); } render() { const showUpload = this.props.attachments.length < appConfig.attachments_max_files && this.props.isLockedByMe && !this.props.readOnly; return (
{!(showUpload && this.props.isWidget === true) ? null : (
{gettext('Drag one or more files here to upload them, or just click the button below.')}
)} {!(showUpload && this.props.isWidget === false) ? null : ( )}
); } }