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 (