/* eslint-disable react/no-multi-comp */ // External Modules import * as React from 'react'; import {Dispatch, Store} from 'redux'; import classNames from 'classnames'; import {connect} from 'react-redux'; // Types import {IAttachment, IAttachmentsWidgetProps} from 'superdesk-api'; import {ASSET_STATE, IAssetItem, IAssetSearchParams, ISetItem, IUploadAssetModalProps} from '../interfaces'; import {IApplicationState} from '../store'; import {superdeskApi, samsApi} from '../apis'; // Redux Actions & Selectors import {loadStorageDestinations} from '../store/storageDestinations/actions'; import {loadSets} from '../store/sets/actions'; import {pushAssetSearchParams, popAssetSearchParams} from '../store/assets/actions'; import {getActiveSets, getSetsById} from '../store/sets/selectors'; // UI import {Button} from 'superdesk-ui-framework/react'; import {SamsApp} from './samsApp'; import {SamsAttachmentsList} from '../components/attachments/samsAttachmentsList'; import {showEditAttachmentModal} from '../components/attachments/editAttachmentModal'; import {showSelectAssetModal} from '../components/assets/selectAssetModal'; export class SamsAttachmentsWidget extends React.PureComponent { onStoreInit(store: Store) { return Promise.all([ store.dispatch(loadStorageDestinations()), store.dispatch(loadSets()), ]); } render() { return ( ); } } interface IProps extends IAttachmentsWidgetProps { activeSets: Array; setsById: Dictionary; pushSearchParams(params: Partial): void; popSearchParams(): void; } const mapStateToProps = (state: IApplicationState) => ({ activeSets: getActiveSets(state), setsById: getSetsById(state), }); const mapDispatchToProps = (dispatch: Dispatch) => ({ pushSearchParams: (params: Partial) => dispatch(pushAssetSearchParams(params)), popSearchParams: () => dispatch(popAssetSearchParams()), }); class SamsAttachmentsWidgetComponent extends React.PureComponent { fileInputNode: React.RefObject; constructor(props: IProps) { super(props); this.fileInputNode = React.createRef(); this.onAddFiles = this.onAddFiles.bind(this); this.onDragFiles = this.onDragFiles.bind(this); this.onDropFiles = this.onDropFiles.bind(this); this.showUploadAssetModal = this.showUploadAssetModal.bind(this); this.showEditAssetModal = this.showEditAssetModal.bind(this); this.showSelectAssetModal = this.showSelectAssetModal.bind(this); } onAddFiles(event: React.ChangeEvent) { event.preventDefault(); } onDragFiles(event: React.DragEvent) { // Prevents Browser default action, such as open in new tab event.preventDefault(); } showUploadAssetModal(props: Partial = {}) { return samsApi.assets.showUploadModal({ defaultAssetState: ASSET_STATE.PUBLIC, allowedStates: [ASSET_STATE.INTERNAL, ASSET_STATE.PUBLIC], onAssetUploaded: (asset: IAssetItem) => { return superdeskApi.entities.attachment.create({ media: asset._id, title: asset.name, description: asset.description, internal: asset.state !== ASSET_STATE.PUBLIC, }) .then((attachment) => { this.props.addAttachments([attachment]); }); }, ...props, }); } showEditAssetModal(attachment: IAttachment) { showEditAttachmentModal(attachment) .then(([updatedAttachment, _updatedAsset]) => { this.props.updateAttachment(updatedAttachment); }); } showSelectAssetModal() { this.props.pushSearchParams({ sizeTo: superdeskApi.instance.config.attachments_max_size / 1048576, // bytes -> MB states: [ASSET_STATE.PUBLIC, ASSET_STATE.INTERNAL], setIds: this.props.activeSets.map((set) => set._id), }); showSelectAssetModal() .then((selectedAssets: Dictionary) => { Promise.all(Object.keys(selectedAssets).map((assetId) => { const asset = selectedAssets[assetId]; return superdeskApi.entities.attachment.create({ media: asset._id, title: asset.name, description: asset.description, internal: asset.state !== ASSET_STATE.PUBLIC, }); })) .then((attachments: Array) => { this.props.addAttachments(attachments); }); }) .finally(() => { this.props.popSearchParams(); }); } onDropFiles(event: React.DragEvent) { event.preventDefault(); if (!this.props.isUploadValid(Array.from(event.dataTransfer.files))) { return; } this.showUploadAssetModal({ initialFiles: Array.from(event.dataTransfer.files) .map((file) => ({ id: Math.random().toString(36).substr(1), file: file, })), }); } render() { const {gettext} = superdeskApi.localization; const {download} = superdeskApi.entities.attachment; const containerClasses = classNames({ 'widget-content__main': this.props.isWidget === true, 'sd-padding--2': this.props.isWidget === true, }); return (
{this.props.isWidget === true ? (
{(this.props.readOnly || this.props.editable === false) ? null : ( {gettext('Drag files here or')} this.showUploadAssetModal()}>  {gettext('browse')}
)}
) : (
{(this.props.readOnly || this.props.editable === false) ? null : (
)}
)}
); } } const SamsAttachmentsWidgetComponentConnected = connect( mapStateToProps, mapDispatchToProps, )(SamsAttachmentsWidgetComponent);