import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { t } from 'plugin-api/beta/client/services'; import { Button } from 'plugin-api/beta/client/components/ui'; import styles from './DownloadCommentHistory.css'; import { getErrorMessages } from 'coral-framework/utils'; import { downloadRateLimitDays } from '../../config'; export const readableDuration = durAsHours => { const durAsDays = Math.ceil(durAsHours / 24); return durAsHours > 23 ? durAsDays > 1 ? t('download_request.days', durAsDays) : t('download_request.day', durAsDays) : durAsHours > 1 ? t('download_request.hours', durAsHours) : t('download_request.hour', durAsHours); }; class DownloadCommentHistory extends Component { static propTypes = { requestDownloadLink: PropTypes.func.isRequired, notify: PropTypes.func.isRequired, root: PropTypes.object.isRequired, }; requestDownloadLink = async () => { const { requestDownloadLink, notify } = this.props; try { await requestDownloadLink(); notify('success', t('download_request.download_preparing')); } catch (err) { notify('error', getErrorMessages(err)); } }; render() { const { root: { me: { lastAccountDownload }, }, } = this.props; const now = new Date(); const lastAccountDownloadDate = lastAccountDownload && new Date(lastAccountDownload); const hoursLeft = lastAccountDownloadDate ? Math.ceil( downloadRateLimitDays * 24 - (now.getTime() - lastAccountDownloadDate.getTime()) / 3.6e6 ) : 0; const canRequestDownload = !lastAccountDownloadDate || hoursLeft <= 0; return (

{t('download_request.section_title')}

{t('download_request.you_will_get_a_copy')}{' '} {t('download_request.download_rate', downloadRateLimitDays)}.

{lastAccountDownloadDate && (

{t('download_request.most_recent_request')}:{' '} {lastAccountDownloadDate.toLocaleString()}

)} {canRequestDownload ? ( ) : ( )}
); } } export default DownloadCommentHistory;