diff --git a/internal_packages/composer/lib/send-action-button.cjsx b/internal_packages/composer/lib/send-action-button.cjsx index 4f3e59121..1ab5e3be1 100644 --- a/internal_packages/composer/lib/send-action-button.cjsx +++ b/internal_packages/composer/lib/send-action-button.cjsx @@ -15,32 +15,31 @@ class SendActionButton extends React.Component constructor: (@props) -> @state = actionConfigs: @_actionConfigs(@props) - selectedSendType: NylasEnv.config.get(SendActionButton.CONFIG_KEY) ? @_defaultActionConfig().configKey - componentDidMount: -> + componentDidMount: => @unsub = ExtensionRegistry.Composer.listen(@_onExtensionsChanged) - componentWillReceiveProps: (newProps) -> + componentWillReceiveProps: (newProps) => @setState actionConfigs: @_actionConfigs(newProps) - componentWillUnmount: -> + componentWillUnmount: => @unsub() primaryClick: => @_onPrimaryClick() - _configKeyFromTitle: (title) -> + _configKeyFromTitle: (title) => return _str.dasherize(title.toLowerCase()) _onExtensionsChanged: => @setState actionConfigs: @_actionConfigs(@props) - _defaultActionConfig: -> + _defaultActionConfig: => title: "Send" iconUrl: null onSend: ({draft}) -> Actions.sendDraft(draft.clientId) configKey: "send" - _actionConfigs: (props) -> + _actionConfigs: (props) => return [] unless props.draft actionConfigs = [@_defaultActionConfig()] @@ -56,7 +55,7 @@ class SendActionButton extends React.Component return actionConfigs - _verifyConfig: (config={}, extension) -> + _verifyConfig: (config={}, extension) => name = extension.name if not _.isString(config.title) throw new Error("#{name}.sendActionConfig must return a string `title`") @@ -66,49 +65,50 @@ class SendActionButton extends React.Component return true - render: -> - return false if not @props.draft + render: => + return false unless @props.draft if @state.actionConfigs.length is 1 @_renderSingleDefaultButton() else @_renderSendDropdown() _onPrimaryClick: => - actionConfigs = @_orderedActionConfigs() - @_sendWithAction(actionConfigs[0].onSend) + {preferred} = @_orderedActionConfigs() + @_sendWithAction(preferred) - _renderSingleDefaultButton: -> - classes = "btn btn-toolbar btn-normal btn-emphasis btn-text btn-send" - iconUrl = @state.actionConfigs[0].iconUrl - + _renderSingleDefaultButton: => + + + _renderSendDropdown: => + {preferred, rest} = @_orderedActionConfigs() - _renderSendDropdown: -> - actionConfigs = @_orderedActionConfigs() + menu={@_dropdownMenu(rest)}/> - _orderedActionConfigs: -> + _orderedActionConfigs: => configKeys = _.pluck(@state.actionConfigs, "configKey") - if @state.selectedSendType not in configKeys - selectedSendType = @_defaultActionConfig().configKey - else - selectedSendType = @state.selectedSendType + preferredKey = NylasEnv.config.get(SendActionButton.CONFIG_KEY) - primary = _.findWhere(@state.actionConfigs, configKey: selectedSendType) - rest = _.reject @state.actionConfigs, (config) -> - config.configKey is selectedSendType + if not preferredKey? or preferredKey not in configKeys + preferredKey = @_defaultActionConfig().configKey - return [primary].concat(rest) + preferred = _.findWhere(@state.actionConfigs, configKey: preferredKey) + rest = _.without(@state.actionConfigs, preferred) - _sendWithAction: (onSend) -> + {preferred, rest} + + _sendWithAction: ({onSend}) => isValidDraft = @props.isValidDraft() if isValidDraft try @@ -116,19 +116,14 @@ class SendActionButton extends React.Component catch err NylasEnv.reportError(err) - _dropdownMenu: (actionConfigs) -> + _dropdownMenu: (actionConfigs) => actionConfig.configKey } - itemContent={ (actionConfig) => @_sendContent(actionConfig.iconUrl) } - onSelect={@_menuItemSelect} + itemContent={@_contentForAction} + onSelect={@_sendWithAction} /> - _menuItemSelect: (actionConfig) => - @setState selectedSendType: actionConfig.configKey - - _sendContent: (iconUrl) -> - sendIcon = "icon-composer-send.png" - + _contentForAction: ({iconUrl}) => if iconUrl plusHTML =  +  additionalImg = - + Send{plusHTML}{additionalImg} diff --git a/internal_packages/composer/spec/send-actions-spec.cjsx b/internal_packages/composer/spec/send-actions-spec.cjsx index 5d36741ef..655dcb042 100644 --- a/internal_packages/composer/spec/send-actions-spec.cjsx +++ b/internal_packages/composer/spec/send-actions-spec.cjsx @@ -60,15 +60,15 @@ describe "SendActionButton", -> it "has the correct primary item", -> spyOn(ExtensionRegistry.Composer, "extensions").andReturn [GoodExtension, SecondExtension] + spyOn(NylasEnv.config, 'get').andReturn('second-extension') @sendActionButton = render(@draft) - @sendActionButton.setState(selectedSendType: 'second-extension') dropdown = ReactTestUtils.findRenderedComponentWithType(@sendActionButton, ButtonDropdown) expect(dropdown.props.primaryTitle).toBe "Second Extension" it "falls back to a default if the primary item can't be found", -> spyOn(ExtensionRegistry.Composer, "extensions").andReturn [GoodExtension, SecondExtension] + spyOn(NylasEnv.config, 'get').andReturn('does-not-exist') @sendActionButton = render(@draft) - @sendActionButton.setState(selectedSendType: 'does-not-exist') dropdown = ReactTestUtils.findRenderedComponentWithType(@sendActionButton, ButtonDropdown) expect(dropdown.props.primaryTitle).toBe "Send" @@ -104,8 +104,8 @@ describe "SendActionButton", -> onSend: -> spyOn(ExtensionRegistry.Composer, "extensions").andReturn [NoIconUrl] + spyOn(NylasEnv.config, 'get').andReturn('some-title') @sendActionButton = render(@draft) - @sendActionButton.setState(selectedSendType: 'some-title') dropdowns = ReactTestUtils.scryRenderedComponentsWithType(@sendActionButton, ButtonDropdown) icons = ReactTestUtils.scryRenderedComponentsWithType(@sendActionButton, RetinaImg) buttons = ReactTestUtils.scryRenderedDOMComponentsWithTag(@sendActionButton, "button") @@ -152,9 +152,9 @@ describe "SendActionButton", -> iconUrl: "nylas://foo/bar/baz" onSend: -> clicked = "onSend fired" spyOn(ExtensionRegistry.Composer, "extensions").andReturn [Click] + spyOn(NylasEnv.config, 'get').andReturn('click') @sendActionButton = render(@draft) - @sendActionButton.setState(selectedSendType: 'click') button = React.findDOMNode(ReactTestUtils.findRenderedDOMComponentWithClass(@sendActionButton, "primary-item")) ReactTestUtils.Simulate.click(button) @@ -168,9 +168,9 @@ describe "SendActionButton", -> iconUrl: "nylas://foo/bar/baz" onSend: -> throw new Error("BOO") spyOn(ExtensionRegistry.Composer, "extensions").andReturn [Click] + spyOn(NylasEnv.config, 'get').andReturn('click') @sendActionButton = render(@draft) - @sendActionButton.setState(selectedSendType: 'click') button = React.findDOMNode(ReactTestUtils.findRenderedDOMComponentWithClass(@sendActionButton, "primary-item")) ReactTestUtils.Simulate.click(button) @@ -178,14 +178,8 @@ describe "SendActionButton", -> expect(NylasEnv.reportError).toHaveBeenCalled() expect(NylasEnv.reportError.calls[0].args[0].message).toMatch /BOO/ - it "initializes with the correct config item", -> - spyOn(NylasEnv.config, "get").andReturn "test-state" + it "initializes with the default and shows the standard Send option", -> + spyOn(NylasEnv.config, 'get').andReturn(null) @sendActionButton = render(@draft) - expect(@sendActionButton.state.selectedSendType).toBe "test-state" - - it "initializes with the default key", -> - spyOn(NylasEnv.config, "get").andReturn null - @sendActionButton = render(@draft) - expect(@sendActionButton.state.selectedSendType).toBe "send" - - + button = React.findDOMNode(@sendActionButton) + expect(button.innerText).toEqual('Send')