import * as React from 'react'; import {showModal} from 'core/services/modalService'; import {IAttachment} from 'superdesk-api'; import {attachmentsApi} from './attachmentsService'; import {Modal} from 'core/ui/components/Modal/Modal'; import {ModalBody} from 'core/ui/components/Modal/ModalBody'; import {ModalHeader} from 'core/ui/components/Modal/ModalHeader'; import {ModalFooter} from 'core/ui/components/Modal/ModalFooter'; import {gettext} from 'core/utils'; import {Input, Switch} from 'superdesk-ui-framework/react'; interface IProps { files: Array; closeModal(): void; onUploaded(attachments: Array): void; } interface IUploadItem { file: File; meta: { title: string; description: string; internal: boolean; }; progress: number; } interface IState { saving: boolean; items: Array; } export class UploadAttachmentsModal extends React.PureComponent { constructor(props: IProps) { super(props); this.state = { saving: false, items: this.props.files.map((file) => ({ file: file, meta: { title: '', description: '', internal: false, }, progress: 0, })), }; this.save = this.save.bind(this); this.uploadFile = this.uploadFile.bind(this); } uploadFile(item: IUploadItem, index: number) { return attachmentsApi.upload( { title: item.meta.title, description: item.meta.description, internal: item.meta.internal, }, item.file, (progress) => { this.updateItemProgress( index, (progress.loaded / progress.total) * 100.0, ); }, ); } updateItemProgress(index, progress) { this.setState((prevState: IState) => { const items = [...prevState.items]; items[index].progress = progress; return {items: items}; }); } updateItemMeta(index: number, field: K, value: IUploadItem['meta'][K]) { this.setState((prevState: IState) => { const items: Array = [...prevState.items]; items[index].meta[field] = value; return {items: items}; }); } save() { this.setState({saving: true}); return Promise.all(this.state.items.map(this.uploadFile)) .then((items) => { this.props.onUploaded(items); this.props.closeModal(); }); } cancelItem(index) { this.setState((prevState: IState) => { const items = {...prevState.items}; items.splice(index, 1); return {items: items}; }); } render() { const ulClass = 'upload-thumbs flex-grid flex-grid--boxed flex-grid--wrap-items flex-grid--small-4'; return ( {gettext('Attach files')}
    {this.state.items.map((item, index) => (
  • this.cancelItem(index)}>
    {item.progress === 0 ? null : (
    )}
    { this.updateItemMeta(index, 'internal', value); }} disabled={this.state.saving} />
    { this.updateItemMeta(index, 'title', value); }} disabled={this.state.saving} />
    { this.updateItemMeta(index, 'description', value); }} disabled={this.state.saving} />
    false} disabled={true} />
    false} disabled={true} />
  • ))}
{gettext('* fields are required')}
); } } export function showUploadAttachmentsModal(props: Omit) { showModal(({closeModal}) => ( )); }