import React from 'react' import PropTypes from 'prop-types' import { translate } from 'react-i18next' import moment from 'moment' import Select from 'react-select' import { Button, Modal, ModalHeader, ModalBody, Label, Input, ModalFooter, FormGroup, Col, Container } from 'reactstrap' import QuillEditor from '../../../../common/QuillEditor' const replyToEmail = 'support@socialhose.io' export class EmailArticlesPopup extends React.Component { static propTypes = { articlesToEmail: PropTypes.array.isRequired, emailArticles: PropTypes.func.isRequired, hidePopup: PropTypes.func.isRequired, recipients: PropTypes.object.isRequired, loadRecipients: PropTypes.func.isRequired, children: PropTypes.any, t: PropTypes.func.isRequired } constructor(props) { super(props) this.state = { selectedRecipients: '' } this.editorRef = React.createRef() } componentWillMount = () => { !this.props.recipients.all.length && this.props.loadRecipients() } componentDidMount = () => { this.props.loadRecipients() } hidePopup = () => { this.props.hidePopup() } collectParams = () => { // need to change with states const recipients = this.state.selectedRecipients if (!recipients) return false return { emailTo: recipients.map((r) => r.value), emailReplyTo: document.getElementById('email-reply-to').value, subject: document.getElementById('email-subject').value, content: this.editorRef.current && this.editorRef.current.root.innerHTML } } onSubmit = () => { const params = this.collectParams() if (params) { this.props.emailArticles(params) } } changeRecipient = (value) => { this.setState({ selectedRecipients: value }) } validEmails = (str) => { const re = /\S+@\S+\.\S+/ const arr = str.split(',') for (let s of arr) { if (!re.test(s)) { return false } } return true } emailRe = /\S+@\S+\.\S+/ isValidNewOption = ({ label }) => { return this.emailRe.test(label) } promptTextCreator = (label) => { return label } render() { const { t, articlesToEmail, recipients } = this.props const { selectedRecipients } = this.state const recipientsAll = recipients.all.map((recipient) => ({ value: recipient, label: recipient })) return ( {t('searchTab.emailPopup.header')} {recipients.pending && } {!recipients.pending && ( )}
{articlesToEmail.map((article) => { return (

{article.title}

{article.source.title} {' '} | {article.author.name} {' '} | {moment(article.published).format('LLL')}

{article.content}

) })}
{this.props.children}
) } } export default translate(['tabsContent', 'common'], { wait: true })( EmailArticlesPopup )