import React from 'react'; import {Contenteditable} from 'nylas-component-kit'; import {AccountStore} from 'nylas-exports'; import SignatureStore from './signature-store'; import SignatureActions from './signature-actions'; export default class PreferencesSignatures extends React.Component { static displayName = 'PreferencesSignatures'; constructor(props) { super(props); this.state = this._getStateFromStores(); } componentDidMount() { this.usub = AccountStore.listen(this._onChange); } componentWillUnmount() { this.usub(); } _onChange = () => { this.setState(this._getStateFromStores()); } _getStateFromStores() { const accounts = AccountStore.accounts(); const state = this.state || {}; let {currentAccountId} = state; if (!accounts.find(acct => acct.id === currentAccountId)) { currentAccountId = accounts[0].id; } return { accounts, currentAccountId, currentSignature: SignatureStore.signatureForAccountId(currentAccountId), editAsHTML: state.editAsHTML, }; } _renderAccountPicker() { const options = this.state.accounts.map(account => {account.emailAddress} ); return ( {options} ); } _renderEditableSignature() { return ( ); } _renderHTMLSignature() { return ( ); } _onEditSignature = (event) => { const html = event.target.value; this.setState({currentSignature: html}); SignatureActions.setSignatureForAccountId({ accountId: this.state.currentAccountId, signature: html, }); } _onSelectAccount = (event) => { const accountId = event.target.value; this.setState({ currentSignature: SignatureStore.signatureForAccountId(accountId), currentAccountId: accountId, }); } _renderModeToggle() { const label = this.state.editAsHTML ? "Edit live preview" : "Edit raw HTML"; const action = () => { this.setState({editAsHTML: !this.state.editAsHTML}); return; }; return ( {label} ); } render() { const rawText = this.state.editAsHTML ? "Raw HTML " : ""; return ( {rawText}Signature for: {this._renderAccountPicker()} {this.state.editAsHTML ? this._renderHTMLSignature() : this._renderEditableSignature()} {this._renderModeToggle()} ) } }