React = require 'react'
_ = require 'underscore'
{Actions,
AccountStore,
MailRulesStore,
MailRulesTemplates,
TaskQueueStatusStore,
ReprocessMailRulesTask} = require 'nylas-exports'
{Flexbox,
EditableList,
RetinaImg,
ScrollRegion,
ScenarioEditor} = require 'nylas-component-kit'
{ActionTemplatesForAccount,
ConditionTemplatesForAccount} = MailRulesTemplates
class PreferencesMailRules extends React.Component
@displayName: 'PreferencesMailRules'
@propTypes:
accountId: React.PropTypes.string.isRequired
constructor: (@props) ->
@state = @stateForAccount(@props.accountId)
componentDidMount: =>
@_unsubscribers = []
@_unsubscribers.push MailRulesStore.listen @_onRulesChanged
@_unsubscribers.push TaskQueueStatusStore.listen @_onTasksChanged
componentWillUnmount: =>
unsubscribe() for unsubscribe in @_unsubscribers
componentWillReceiveProps: (newProps) =>
newState = @stateForAccount(newProps.accountId)
@setState(newState)
stateForAccount: (accountId) =>
account = AccountStore.accountForId(accountId)
rules = MailRulesStore.rulesForAccountId(accountId)
return {
account: account
rules: rules
selectedRule: _.findWhere(rules, {id: @state?.selectedRule?.id}) ? rules[0]
tasks: TaskQueueStatusStore.tasksMatching(ReprocessMailRulesTask, {})
actionTemplates: ActionTemplatesForAccount(account)
ConditionTemplates: ConditionTemplatesForAccount(account)
}
render: =>
Mail Rules
{@state.account?.emailAddress}
{@_renderList()}
{@_renderDetail()}
By default, mail rules are only applied to new mail as it arrives.
Applying rules to your entire mailbox may take quite a while and
temporarily degrade performance.
Process all Mail
{@_renderTasks()}
_renderList: =>
_renderListItemContent: (rule) ->
if rule.disabled
return {rule.name}
else
return rule.name
_renderDetail: =>
rule = @state.selectedRule
if rule
{@_renderDetailDisabledNotice()}
If
Any
All
of the following conditions are met:
Actions.updateMailRule(rule.id, {conditions}) }
className="well well-matchers"/>
Perform the following actions:
Actions.updateMailRule(rule.id, {actions}) }
className="well well-actions"/>
else
Create a rule or select one to get started
_renderDetailDisabledNotice: =>
return false unless @state.selectedRule.disabled
Enable
This rule has been disabled. Make sure the actions below are valid
and re-enable the rule.
({@state.selectedRule.disabledReason})
_renderTasks: =>
return false if @state.tasks.length is 0
{ @state.tasks.map (task) ->
{AccountStore.accountForId(task.accountId).emailAddress}
{" — #{new Number(task.numberOfImpactedItems()).toLocaleString()} processed..."}
Actions.dequeueTask(task.id) }>Cancel
}
_onReprocessRules: =>
needsMessageBodies = =>
for rule in @state.rules
for condition in rule.conditions
if condition.templateKey is 'body'
return true
return false
if needsMessageBodies()
NylasEnv.showErrorDialog("One or more of your mail rules requires the bodies of messages being processed. These rules can't be run on your entire mailbox.")
task = new ReprocessMailRulesTask(@state.account.id)
Actions.queueTask(task)
_onAddRule: =>
Actions.addMailRule({accountId: @state.account.id})
_onSelectRule: (rule, idx) =>
@setState(selectedRule: rule)
_onReorderRule: (rule, idx, newIdx) =>
Actions.reorderMailRule(rule.id, newIdx)
_onDeleteRule: (rule, idx) =>
Actions.deleteMailRule(rule.id)
_onRuleNameEdited: (newName, rule, idx) =>
Actions.updateMailRule(rule.id, {name: newName})
_onRuleConditionModeEdited: (event) =>
Actions.updateMailRule(@state.selectedRule.id, {conditionMode: event.target.value})
_onRuleEnabled: =>
Actions.updateMailRule(@state.selectedRule.id, {disabled: false, disabledReason: null})
_onRulesChanged: =>
next = @stateForAccount(@props.accountId)
nextRules = next.rules
prevRules = @state?.rules || []
added = _.difference(nextRules, prevRules)
if added.length is 1
next.selectedRule = added[0]
@setState(next)
_onTasksChanged: =>
@setState(tasks: TaskQueueStatusStore.tasksMatching(ReprocessMailRulesTask, {}))
module.exports = PreferencesMailRules