import React from 'react'; import PropTypes from 'prop-types'; import { isValidHost } from './onboarding-helpers'; import CreatePageForForm from './decorators/create-page-for-form'; import FormField from './form-field'; const StandardIMAPPorts = [143, 993]; const StandardSMTPPorts = [25, 465, 587]; class AccountIMAPSettingsForm extends React.Component { static displayName = 'AccountIMAPSettingsForm'; static propTypes = { account: PropTypes.object, errorFieldNames: PropTypes.array, submitting: PropTypes.bool, onConnect: PropTypes.func, onFieldChange: PropTypes.func, onFieldKeyPress: PropTypes.func, }; static submitLabel = () => { return 'Connect Account'; }; static titleLabel = () => { return 'Set up your account'; }; static subtitleLabel = () => { return 'Complete the IMAP and SMTP settings below to connect your account.'; }; static validateAccount = account => { let errorMessage = null; const errorFieldNames = []; if (!account.settings[`imap_username`] || !account.settings[`imap_password`]) { return { errorMessage, errorFieldNames, populated: false }; } // Note: we explicitly don't check that an SMTP username / password // is provided because occasionally those gateways don't require them! for (const type of ['imap', 'smtp']) { if (!account.settings[`${type}_host`]) { return { errorMessage, errorFieldNames, populated: false }; } if (!isValidHost(account.settings[`${type}_host`])) { errorMessage = 'Please provide a valid hostname or IP adddress.'; errorFieldNames.push(`${type}_host`); } if (!Number.isInteger(account.settings[`${type}_port`] / 1)) { errorMessage = 'Please provide a valid port number.'; errorFieldNames.push(`${type}_port`); } } return { errorMessage, errorFieldNames, populated: true }; }; submit() { const { settings } = this.props.account; if (settings.imap_host && settings.imap_host.includes('imap.gmail.com')) { AppEnv.showErrorDialog({ title: 'Are you sure?', message: `This looks like a Gmail account! While it's possible to setup an App ` + `Password and connect to Gmail via IMAP, Mailspring also supports Google OAuth. Go ` + `back and select "Gmail & Google Apps" from the provider screen.`, }); } this.props.onConnect(); } renderPortDropdown(protocol) { if (!['imap', 'smtp'].includes(protocol)) { throw new Error(`Can't render port dropdown for protocol '${protocol}'`); } const { account: { settings }, submitting, onFieldKeyPress, onFieldChange } = this.props; const field = `${protocol}_port`; const values = protocol === 'imap' ? StandardIMAPPorts : StandardSMTPPorts; const isStandard = values.includes(settings[field] / 1); const customValue = isStandard ? '0' : settings[field]; return ( {!isStandard && ( )} ); } renderSecurityDropdown(protocol) { const { account: { settings }, submitting, onFieldKeyPress, onFieldChange } = this.props; return (
); } renderFieldsForType(type) { return (
{this.renderPortDropdown(type)} {this.renderSecurityDropdown(type)}
); } render() { return (
Incoming Mail (IMAP):
{this.renderFieldsForType('imap')}
Outgoing Mail (SMTP):
{this.renderFieldsForType('smtp')}
); } } export default CreatePageForForm(AccountIMAPSettingsForm);