'use strict'; import React, {Component} from 'react'; import PropTypes from 'prop-types'; import {withTranslation} from '../lib/i18n'; import {LinkButton, requiresAuthenticatedUser, Title, withPageHelpers} from '../lib/page'; import {AlignedRow, ButtonRow, CheckBox, DatePicker, Form, InputField, TableSelect, withForm} from '../lib/form'; import {withAsyncErrorHandler, withErrorHandling} from '../lib/error-handling'; import {getCampaignLabels} from './helpers'; import {Table} from "../lib/table"; import {Button, Icon, ModalDialog} from "../lib/bootstrap-components"; import axios from "../lib/axios"; import {getPublicUrl, getUrl} from "../lib/urls"; import interoperableErrors from '../../../shared/interoperable-errors'; import {CampaignStatus, CampaignType} from "../../../shared/campaigns"; import moment from 'moment'; import campaignsStyles from "./styles.scss"; import {withComponentMixins} from "../lib/decorator-helpers"; import {TestSendModalDialog, TestSendModalDialogMode} from "./TestSendModalDialog"; @withComponentMixins([ withTranslation, withForm, withErrorHandling, withPageHelpers, requiresAuthenticatedUser ]) class PreviewForTestUserModalDialog extends Component { constructor(props) { super(props); this.initForm({ leaveConfirmation: false }); } static propTypes = { visible: PropTypes.bool.isRequired, onHide: PropTypes.func.isRequired, entity: PropTypes.object.isRequired, } localValidateFormValues(state) { const t = this.props.t; if (!state.getIn(['testUser', 'value'])) { state.setIn(['testUser', 'error'], t('subscriptionHasToBeSelectedToShowThe')) } else { state.setIn(['testUser', 'error'], null); } } componentDidMount() { this.populateFormValues({ testUser: null, }); } async previewAsync() { if (this.isFormWithoutErrors()) { const campaignCid = this.props.entity.cid; const [listCid, subscriptionCid] = this.getFormValue('testUser').split(':'); window.open(getPublicUrl(`archive/${campaignCid}/${listCid}/${subscriptionCid}`, {withLocale: true}), '_blank'); } else { this.showFormValidation(); } } async hideModal() { this.props.onHide(); } render() { const t = this.props.t; const testUsersColumns = [ { data: 1, title: t('email') }, { data: 2, title: t('subscriptionId'), render: data => {data} }, { data: 3, title: t('listId'), render: data => {data} }, { data: 4, title: t('list') }, { data: 5, title: t('listNamespace') } ]; return ( ); } } @withComponentMixins([ withTranslation, withForm, withErrorHandling, withPageHelpers, requiresAuthenticatedUser ]) class SendControls extends Component { constructor(props) { super(props); this.state = { showTestSendModal: false, previewForTestUserVisible: false }; this.initForm({ leaveConfirmation: false }); } static propTypes = { entity: PropTypes.object.isRequired, refreshEntity: PropTypes.func.isRequired } localValidateFormValues(state) { const t = this.props.t; state.setIn(['date', 'error'], null); state.setIn(['time', 'error'], null); if (state.getIn(['sendLater', 'value'])) { const dateValue = state.getIn(['date', 'value']).trim(); if (!dateValue) { state.setIn(['date', 'error'], t('dateMustNotBeEmpty')); } else if (!moment(dateValue, 'YYYY-MM-DD', true).isValid()) { state.setIn(['date', 'error'], t('dateIsInvalid')); } const timeValue = state.getIn(['time', 'value']).trim(); if (!timeValue) { state.setIn(['time', 'error'], t('timeMustNotBeEmpty')); } else if (!moment(timeValue, 'HH:mm', true).isValid()) { state.setIn(['time', 'error'], t('timeIsInvalid')); } } } componentDidMount() { const entity = this.props.entity; if (entity.scheduled) { const date = moment(entity.scheduled); this.populateFormValues({ sendLater: true, date: date.format('YYYY-MM-DD'), time: date.format('HH:mm') }); } else { this.populateFormValues({ sendLater: false, date: '', time: '' }); } } async refreshEntity() { await this.props.refreshEntity(); } async postAndMaskStateError(url) { try { await axios.post(getUrl(url)); } catch (err) { if (err instanceof interoperableErrors.InvalidStateError) { // Just mask the fact that it's not possible to start anything and refresh instead. } else { throw err; } } } async scheduleAsync() { if (this.isFormWithoutErrors()) { const data = this.getFormValues(); const date = moment(data.date, 'YYYY-MM-DD'); const time = moment(data.time, 'HH:mm'); date.hour(time.hour()); date.minute(time.minute()); date.second(0); date.millisecond(0); date.utcOffset(0, true); // TODO, process offset from user settings await this.postAndMaskStateError(`rest/campaign-start-at/${this.props.entity.id}/${date.valueOf()}`); } else { this.showFormValidation(); } await this.refreshEntity(); } async startAsync() { await this.postAndMaskStateError(`rest/campaign-start/${this.props.entity.id}`); await this.refreshEntity(); } async stopAsync() { await this.postAndMaskStateError(`rest/campaign-stop/${this.props.entity.id}`); await this.refreshEntity(); } async confirmStart() { const t = this.props.t; this.actionDialog( t('confirmLaunch'), t('doYouWantToLaunchTheCampaign?'), async () => { await this.startAsync(); await this.refreshEntity(); } ); } async resetAsync() { const t = this.props.t; this.actionDialog( t('confirmReset'), t('doYouWantToResetTheCampaign?All'), async () => { await this.postAndMaskStateError(`rest/campaign-reset/${this.props.entity.id}`); await this.refreshEntity(); } ); } async enableAsync() { await this.postAndMaskStateError(`rest/campaign-enable/${this.props.entity.id}`); await this.refreshEntity(); } async disableAsync() { await this.postAndMaskStateError(`rest/campaign-disable/${this.props.entity.id}`); await this.refreshEntity(); } actionDialog(title, message, callback) { this.setState({ modalTitle: title, modalMessage: message, modalCallback: callback, modalVisible: true }); } modalAction(isYes) { if (isYes && this.state.modalCallback) { this.state.modalCallback(); } this.setState({ modalTitle: '', modalMessage: '', modalCallback: null, modalVisible: false }); } render() { const t = this.props.t; const entity = this.props.entity; const dialogs = ( <> this.setState({showTestSendModal: false})} campaign={this.props.entity} /> this.setState({previewForTestUserVisible: false})} entity={this.props.entity} /> ); const testButtons = ( <>