import React, {Component} from 'react' import PropTypes from 'prop-types' import ReactDOM from 'react-dom' import {Actions} from 'nylas-exports' import {RetinaImg} from 'nylas-component-kit' import moment from 'moment' import SendRemindersPopover from './send-reminders-popover' import {reminderDateFor, updateDraftReminderMetadata} from './send-reminders-utils' export default class SendRemindersComposerButton extends Component { static displayName = 'SendRemindersComposerButton'; static containerRequired = false; static propTypes = { draft: PropTypes.object.isRequired, session: PropTypes.object.isRequired, }; constructor(props) { super(props) this.state = { saving: false, } } componentWillReceiveProps() { if (this.state.saving) { this.setState({saving: false}) } } shouldComponentUpdate(nextProps) { return (reminderDateFor(nextProps.draft) !== reminderDateFor(this.props.draft)); } onSetReminder = (reminderDate) => { const {draft, session} = this.props this.setState({saving: true}); updateDraftReminderMetadata(session, { expiration: reminderDate, sentHeaderMessageId: draft.headerMessageId, }) } onClick = () => { const {draft} = this.props const buttonRect = ReactDOM.findDOMNode(this).getBoundingClientRect() Actions.openPopover( this.onSetReminder(null)} />, {originRect: buttonRect, direction: 'up'} ) }; render() { let className = 'btn btn-toolbar btn-send-reminder'; if (this.state.saving) { return ( ); } const reminderDate = reminderDateFor(this.props.draft); let reminderLabel = 'Set reminder'; if (reminderDate) { className += ' btn-enabled'; reminderLabel = `Reminder set for ${moment(reminderDate).fromNow(true)} from now`; } return ( ); } }