import React from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import dateFnsFormat from 'date-fns/format'; import parse from 'date-fns/parse'; import { isInteger } from 'core-js/fn/number'; import { withUnstatedContainers } from './UnstatedUtils'; import { toastSuccess, toastError } from '../util/apiNotification'; import AppContainer from '../services/AppContainer'; import PageContainer from '../services/PageContainer'; class ShareLinkForm extends React.Component { constructor(props) { super(props); this.state = { expirationType: 'unlimited', numberOfDays: '7', description: '', customExpirationDate: dateFnsFormat(new Date(), 'yyyy-MM-dd'), customExpirationTime: dateFnsFormat(new Date(), 'hh:mm'), }; this.handleChangeExpirationType = this.handleChangeExpirationType.bind(this); this.handleChangeNumberOfDays = this.handleChangeNumberOfDays.bind(this); this.handleChangeDescription = this.handleChangeDescription.bind(this); this.handleIssueShareLink = this.handleIssueShareLink.bind(this); } /** * change expirationType * @param {string} expirationType */ handleChangeExpirationType(expirationType) { this.setState({ expirationType }); } /** * change numberOfDays * @param {string} numberOfDays */ handleChangeNumberOfDays(numberOfDays) { this.setState({ numberOfDays }); } /** * change description * @param {string} description */ handleChangeDescription(description) { this.setState({ description }); } /** * change customExpirationDate * @param {date} customExpirationDate */ handleChangeCustomExpirationDate(customExpirationDate) { this.setState({ customExpirationDate }); } /** * change customExpirationTime * @param {date} customExpirationTime */ handleChangeCustomExpirationTime(customExpirationTime) { this.setState({ customExpirationTime }); } /** * Generate expiredAt by expirationType */ generateExpired() { const { t } = this.props; const { expirationType } = this.state; let expiredAt; if (expirationType === 'unlimited') { return null; } if (expirationType === 'numberOfDays') { if (!isInteger(Number(this.state.numberOfDays))) { throw new Error(t('share_links.Invalid_Number_of_Date')); } const date = new Date(); date.setDate(date.getDate() + Number(this.state.numberOfDays)); expiredAt = date; } if (expirationType === 'custom') { const { customExpirationDate, customExpirationTime } = this.state; expiredAt = parse(`${customExpirationDate}T${customExpirationTime}`, "yyyy-MM-dd'T'HH:mm", new Date()); } return expiredAt; } closeForm() { const { onCloseForm } = this.props; if (onCloseForm == null) { return; } onCloseForm(); } async handleIssueShareLink() { const { t, appContainer, pageContainer, } = this.props; const { pageId } = pageContainer.state; const { description } = this.state; let expiredAt; try { expiredAt = this.generateExpired(); } catch (err) { return toastError(err); } try { await appContainer.apiv3Post('/share-links/', { relatedPage: pageId, expiredAt, description }); this.closeForm(); toastSuccess(t('toaster.issue_share_link')); } catch (err) { toastError(err); } } renderExpirationTypeOptions() { const { expirationType } = this.state; const { t } = this.props; return (