import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { gettext } from '../../utils/constants'; import { Utils } from '../../utils/utils'; const propTypes = { resumableFile: PropTypes.object.isRequired, onUploadCancel: PropTypes.func.isRequired, onUploadRetry: PropTypes.func.isRequired, }; const UPLOAD_UPLOADING = 'uploading'; const UPLOAD_ERROR = 'error'; const UPLOAD_ISSAVING = 'isSaving'; const UPLOAD_UPLOADED = 'uploaded'; class UploadListItem extends React.Component { constructor(props) { super(props); this.state = { uploadState: UPLOAD_UPLOADING }; } componentWillReceiveProps(nextProps) { let { resumableFile } = nextProps; let uploadState = UPLOAD_UPLOADING; if (resumableFile.error) { uploadState = UPLOAD_ERROR; } else { if (resumableFile.remainingTime === 0 && !resumableFile.isSaved) { uploadState = UPLOAD_ISSAVING; } if (resumableFile.isSaved) { uploadState = UPLOAD_UPLOADED; } } this.setState({uploadState: uploadState}); } onUploadCancel = (e) => { e.preventDefault(); this.props.onUploadCancel(this.props.resumableFile); } onUploadRetry = (e) => { e.preventDefault(); this.props.onUploadRetry(this.props.resumableFile); } formatFileSize = (size) => { if (typeof size !== 'number') { return ''; } if (size >= 1000 * 1000 * 1000) { return (size / (1000 * 1000 * 1000)).toFixed(1) + ' G'; } if (size >= 1000 * 1000) { return (size / (1000 * 1000)).toFixed(1) + ' M'; } if (size >= 1000) { return (size / 1000).toFixed(1) + ' K'; } return size.toFixed(1) + ' B'; } render() { let { resumableFile } = this.props; let progress = Math.round(resumableFile.progress() * 100); let error = resumableFile.error; return (
{resumableFile.newFileName}
{this.formatFileSize(resumableFile.size)} {(this.state.uploadState === UPLOAD_UPLOADING || this.state.uploadState === UPLOAD_ISSAVING) && {resumableFile.size >= (100 * 1000 * 1000) && {resumableFile.isUploading() && (
{(resumableFile.remainingTime === -1) &&
{gettext('Preparing to upload...')}
} {(resumableFile.remainingTime > 0) &&
{gettext('Remaining')}{' '}{Utils.formatTime(resumableFile.remainingTime)}
} {(resumableFile.remainingTime === 0) &&
{gettext('Indexing...')}
}
)} {!resumableFile.isUploading() && (
)}
} {(resumableFile.size < (100 * 1000 * 1000)) &&
}
} {this.state.uploadState === UPLOAD_ERROR && (
)} {this.state.uploadState === UPLOAD_UPLOADING && ( {gettext('Cancel')} )} {this.state.uploadState === UPLOAD_ERROR && ( {gettext('Retry')} )} {this.state.uploadState === UPLOAD_ISSAVING && ( {gettext('Saving...')} )} {this.state.uploadState === UPLOAD_UPLOADED && ( {gettext('Uploaded')} )} ); } } UploadListItem.propTypes = propTypes; export default UploadListItem;