2015-12-10 04:35:40 +08:00
|
|
|
import _ from 'underscore';
|
|
|
|
import React, {Component, PropTypes} from 'react';
|
2016-01-08 10:44:40 +08:00
|
|
|
import {EditableList, NewsletterSignup} from 'nylas-component-kit';
|
2015-12-10 04:35:40 +08:00
|
|
|
import {RegExpUtils} from 'nylas-exports';
|
|
|
|
|
|
|
|
class PreferencesAccountDetails extends Component {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
account: PropTypes.object,
|
|
|
|
onAccountUpdated: PropTypes.func.isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = _.clone(props.account);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
this.setState(_.clone(nextProps.account));
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._saveChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
|
2015-12-11 09:59:16 +08:00
|
|
|
/**
|
2015-12-12 02:01:13 +08:00
|
|
|
* @private Will transform any user input into alias format.
|
2015-12-11 09:59:16 +08:00
|
|
|
* It will ignore any text after an email, if one is entered.
|
|
|
|
* If no email is entered, it will use the account's email.
|
|
|
|
* It will treat the text before the email as the name for the alias.
|
|
|
|
* If no name is entered, it will use the account's name value.
|
2015-12-12 02:01:13 +08:00
|
|
|
* @param {string} str - The string the user entered on the alias input
|
|
|
|
* @param {object} [account=this.props.account] - The account object
|
2015-12-11 09:59:16 +08:00
|
|
|
*/
|
2015-12-10 04:35:40 +08:00
|
|
|
_makeAlias(str, account = this.props.account) {
|
|
|
|
const emailRegex = RegExpUtils.emailRegex();
|
|
|
|
const match = emailRegex.exec(str);
|
|
|
|
if (!match) {
|
2016-02-02 04:45:52 +08:00
|
|
|
return `${str || account.name} <${account.emailAddress}>`;
|
2015-12-10 04:35:40 +08:00
|
|
|
}
|
|
|
|
const email = match[0];
|
|
|
|
let name = str.slice(0, Math.max(0, match.index - 1));
|
|
|
|
if (!name) {
|
|
|
|
name = account.name || 'No name provided';
|
|
|
|
}
|
2015-12-15 06:29:45 +08:00
|
|
|
name = name.trim();
|
2015-12-10 04:35:40 +08:00
|
|
|
// TODO Sanitize the name string
|
|
|
|
return `${name} <${email}>`;
|
|
|
|
}
|
|
|
|
|
2015-12-15 06:34:13 +08:00
|
|
|
_updatedDefaultAlias(originalAlias, newAlias, defaultAlias) {
|
|
|
|
if (originalAlias === defaultAlias) {
|
|
|
|
return newAlias;
|
|
|
|
}
|
|
|
|
return defaultAlias;
|
|
|
|
}
|
|
|
|
|
2015-12-10 04:35:40 +08:00
|
|
|
_saveChanges = ()=> {
|
|
|
|
this.props.onAccountUpdated(this.props.account, this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Handlers
|
|
|
|
|
|
|
|
_onAccountLabelUpdated = (event)=> {
|
|
|
|
this.setState({label: event.target.value});
|
|
|
|
}
|
|
|
|
|
|
|
|
_onAccountAliasCreated = (newAlias)=> {
|
|
|
|
const coercedAlias = this._makeAlias(newAlias);
|
|
|
|
const aliases = this.state.aliases.concat([coercedAlias]);
|
|
|
|
this.setState({aliases}, ()=> {
|
|
|
|
this._saveChanges();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_onAccountAliasUpdated = (newAlias, alias, idx)=> {
|
|
|
|
const coercedAlias = this._makeAlias(newAlias);
|
|
|
|
const aliases = this.state.aliases.slice();
|
2015-12-15 06:34:13 +08:00
|
|
|
let defaultAlias = this.state.defaultAlias;
|
|
|
|
if (defaultAlias === alias) {
|
|
|
|
defaultAlias = coercedAlias;
|
|
|
|
}
|
2015-12-10 04:35:40 +08:00
|
|
|
aliases[idx] = coercedAlias;
|
2015-12-15 06:34:13 +08:00
|
|
|
this.setState({aliases, defaultAlias}, ()=> {
|
2015-12-10 04:35:40 +08:00
|
|
|
this._saveChanges();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_onAccountAliasRemoved = (alias, idx)=> {
|
|
|
|
const aliases = this.state.aliases.slice();
|
2015-12-15 06:34:13 +08:00
|
|
|
let defaultAlias = this.state.defaultAlias;
|
|
|
|
if (defaultAlias === alias) {
|
|
|
|
defaultAlias = null;
|
|
|
|
}
|
2015-12-10 04:35:40 +08:00
|
|
|
aliases.splice(idx, 1);
|
2015-12-15 06:34:13 +08:00
|
|
|
this.setState({aliases, defaultAlias}, ()=> {
|
2015-12-10 04:35:40 +08:00
|
|
|
this._saveChanges();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-15 06:34:13 +08:00
|
|
|
_onDefaultAliasSelected = (event)=> {
|
|
|
|
const defaultAlias = event.target.value === 'None' ? null : event.target.value;
|
|
|
|
this.setState({defaultAlias}, ()=> {
|
|
|
|
this._saveChanges();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Renderers
|
|
|
|
|
|
|
|
_renderDefaultAliasSelector(account) {
|
|
|
|
const aliases = account.aliases;
|
|
|
|
const defaultAlias = account.defaultAlias || 'None';
|
|
|
|
if (aliases.length > 0) {
|
|
|
|
return (
|
|
|
|
<div className="default-alias-selector">
|
|
|
|
<span>Default alias: </span>
|
|
|
|
<select value={defaultAlias} onChange={this._onDefaultAliasSelected}>
|
|
|
|
<option>None</option>
|
|
|
|
{aliases.map((alias, idx)=> <option key={`alias-${idx}`} value={alias}>{alias}</option>)}
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 04:35:40 +08:00
|
|
|
render() {
|
|
|
|
const account = this.state;
|
|
|
|
const aliasPlaceholder = this._makeAlias(
|
|
|
|
`alias@${account.emailAddress.split('@')[1]}`
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="account-details">
|
|
|
|
<h3>Account Label</h3>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
value={account.label}
|
|
|
|
onBlur={this._saveChanges}
|
|
|
|
onChange={this._onAccountLabelUpdated} />
|
2015-12-16 03:56:31 +08:00
|
|
|
|
2015-12-10 04:35:40 +08:00
|
|
|
<h3>Aliases</h3>
|
2015-12-16 03:56:31 +08:00
|
|
|
|
|
|
|
<div className="platform-note">
|
|
|
|
You may need to configure aliases with your
|
|
|
|
mail provider (Outlook, Gmail) before using them.
|
|
|
|
</div>
|
|
|
|
|
2015-12-10 04:35:40 +08:00
|
|
|
<EditableList
|
|
|
|
showEditIcon
|
feat(mail-rules): Per-account mail rules filter incoming, existing mail
Summary:
Originally, this was going to be a totally independent package, but
I wasn't able to isolate the functionality and get it tied in to
the delta-stream consumption. Here's how it currently works:
- The preferences package has a new tab which allows you to edit
mail filters. Filters are saved in a new core store, and a new
stock component (ScenarioEditor) renders the editor. The editor
takes a set of templates that define a value space, and outputs
a valid set of values.
- A new MailFilterProcessor takes messages and creates tasks to
apply the actions from the MailFiltersStore.
- The worker-sync package now uses the MailFilterProcessor to
apply filters /before/ it calls didPassivelyReceiveNewModels,
so filtrs are applied before any notifications are created.
- A new task, ReprocessMailFiltersTask allows you to run filters
on all of your existing mail. It leverages the existing TaskQueue
architecture to: a) resume where it left off if you quit midway,
b) be queryable (for status) from all windows and c) cancelable.
The TaskQueue is a bit strange because it runs performLocal and
performRemote very differently, and I had to use `performRemote`.
(todo refactor soon.)
This diff also changes the EditableList a bit to behave like a
controlled component and render focused / unfocused states.
Test Plan: Run tests, only for actual filter processing atm.
Reviewers: juan, evan
Reviewed By: evan
Differential Revision: https://phab.nylas.com/D2379
2015-12-23 15:19:32 +08:00
|
|
|
items={account.aliases}
|
2015-12-10 04:35:40 +08:00
|
|
|
createInputProps={{placeholder: aliasPlaceholder}}
|
|
|
|
onItemCreated={this._onAccountAliasCreated}
|
|
|
|
onItemEdited={this._onAccountAliasUpdated}
|
feat(mail-rules): Per-account mail rules filter incoming, existing mail
Summary:
Originally, this was going to be a totally independent package, but
I wasn't able to isolate the functionality and get it tied in to
the delta-stream consumption. Here's how it currently works:
- The preferences package has a new tab which allows you to edit
mail filters. Filters are saved in a new core store, and a new
stock component (ScenarioEditor) renders the editor. The editor
takes a set of templates that define a value space, and outputs
a valid set of values.
- A new MailFilterProcessor takes messages and creates tasks to
apply the actions from the MailFiltersStore.
- The worker-sync package now uses the MailFilterProcessor to
apply filters /before/ it calls didPassivelyReceiveNewModels,
so filtrs are applied before any notifications are created.
- A new task, ReprocessMailFiltersTask allows you to run filters
on all of your existing mail. It leverages the existing TaskQueue
architecture to: a) resume where it left off if you quit midway,
b) be queryable (for status) from all windows and c) cancelable.
The TaskQueue is a bit strange because it runs performLocal and
performRemote very differently, and I had to use `performRemote`.
(todo refactor soon.)
This diff also changes the EditableList a bit to behave like a
controlled component and render focused / unfocused states.
Test Plan: Run tests, only for actual filter processing atm.
Reviewers: juan, evan
Reviewed By: evan
Differential Revision: https://phab.nylas.com/D2379
2015-12-23 15:19:32 +08:00
|
|
|
onDeleteItem={this._onAccountAliasRemoved} />
|
|
|
|
|
2015-12-15 06:34:13 +08:00
|
|
|
{this._renderDefaultAliasSelector(account)}
|
2016-01-08 10:44:40 +08:00
|
|
|
|
|
|
|
<div className="newsletter">
|
|
|
|
<NewsletterSignup emailAddress={account.emailAddress} name={account.name} />
|
|
|
|
</div>
|
2015-12-10 04:35:40 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PreferencesAccountDetails;
|