From 9738f4178c46a0f1bc4fe5ebe136fe802b7182e4 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Fri, 16 Jun 2017 16:51:35 +0200 Subject: [PATCH] Converts NotificationBar and NotificationItem into redux components Resolves #9451 Auditors: @bsclifton @bridiver Test Plan: --- app/renderer/components/main/main.js | 11 ++- .../components/main/notificationBar.js | 42 ++++++---- .../components/main/notificationItem.js | 82 +++++++++++++------ 3 files changed, 87 insertions(+), 48 deletions(-) diff --git a/app/renderer/components/main/main.js b/app/renderer/components/main/main.js index 22a8540d79d..4f6dcd26155 100644 --- a/app/renderer/components/main/main.js +++ b/app/renderer/components/main/main.js @@ -679,12 +679,12 @@ class Main extends ImmutableComponent { const customTitlebar = this.customTitlebar const contextMenuDetail = this.props.windowState.get('contextMenuDetail') const shouldAllowWindowDrag = windowState.shouldAllowWindowDrag(this.props.appState, this.props.windowState, activeFrame, isFocused()) + const activeOrigin = activeFrame ? siteUtil.getOrigin(activeFrame.get('location')) : null + const notificationBarIsVisible = activeOrigin && this.props.appState.get('notifications').filter((item) => + item.get('frameOrigin') ? activeOrigin === item.get('frameOrigin') : true).size > 0 const appStateSites = this.props.appState.get('sites') - const notifications = this.props.appState.get('notifications') - const hasNotifications = notifications && notifications.size - return
{ - hasNotifications && activeFrame - ? + notificationBarIsVisible + ? : null } - { activeFrame && activeFrame.get('findbarShown') && !activeFrame.get('isFullScreen') ? - item.get('frameOrigin') ? activeOrigin === item.get('frameOrigin') : true) +class NotificationBar extends React.Component { + mergeProps (state, ownProps) { + const currentWindow = state.get('currentWindow') + const activeFrame = frameStateUtil.getActiveFrame(currentWindow) || Immutable.Map() + const activeOrigin = getOrigin(activeFrame.get('location')) + const notifications = state.get('notifications') - if (!activeNotifications.size) { - return null - } + const props = {} + props.activeNotifications = notifications + .filter((item) => { + return item.get('frameOrigin') + ? activeOrigin === item.get('frameOrigin') + : true + }) + .takeLast(3) + .map((notification) => notification.get('message')) + return props + } + + render () { return
{ - activeNotifications.takeLast(3).map((notificationDetail) => - + this.props.activeNotifications.map((message) => + ) }
} } -class NotificationBarCaret extends ImmutableComponent { +class NotificationBarCaret extends React.Component { render () { return
@@ -75,6 +85,6 @@ const styles = StyleSheet.create({ }) module.exports = { - NotificationBar, + NotificationBar: ReduxComponent.connect(NotificationBar), NotificationBarCaret } diff --git a/app/renderer/components/main/notificationItem.js b/app/renderer/components/main/notificationItem.js index 89286d948a0..3ba54190f8b 100644 --- a/app/renderer/components/main/notificationItem.js +++ b/app/renderer/components/main/notificationItem.js @@ -3,11 +3,12 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ const React = require('react') +const Immutable = require('immutable') const ipc = require('electron').ipcRenderer const {StyleSheet, css} = require('aphrodite/no-important') // Components -const ImmutableComponent = require('../immutableComponent') +const ReduxComponent = require('../reduxComponent') const BrowserButton = require('../common/browserButton') // Constants @@ -22,22 +23,35 @@ const cx = require('../../../../js/lib/classSet') // Styles const commonStyles = require('../styles/commonStyles') -class NotificationItem extends ImmutableComponent { - clickHandler (buttonIndex, e) { - const nonce = this.props.detail.get('options').get('nonce') - if (nonce) { - ipc.emit(messages.NOTIFICATION_RESPONSE + nonce, {}, - this.props.detail.get('message'), - buttonIndex, this.checkbox ? this.checkbox.checked : false) +class NotificationItem extends React.Component { + constructor (props) { + super(props) + this.openAdvanced = this.openAdvanced.bind(this) + this.toggleCheckbox = this.toggleCheckbox.bind(this) + } + + clickHandler (buttonIndex) { + if (this.props.nonce) { + ipc.emit( + messages.NOTIFICATION_RESPONSE + this.props.nonce, + {}, + this.props.message, + buttonIndex, + this.checkbox ? this.checkbox.checked : false + ) } else { - ipc.send(messages.NOTIFICATION_RESPONSE, this.props.detail.get('message'), - buttonIndex, this.checkbox ? this.checkbox.checked : false) + ipc.send( + messages.NOTIFICATION_RESPONSE, + this.props.message, + buttonIndex, + this.checkbox ? this.checkbox.checked : false + ) } } openAdvanced () { appActions.createTabRequested({ - url: this.props.detail.get('options').get('advancedLink') + url: this.props.advancedLink }) } @@ -45,47 +59,63 @@ class NotificationItem extends ImmutableComponent { this.checkbox.checked = !this.checkbox.checked } + mergeProps (state, ownProps) { + const notification = state.get('notifications') + .find((notification) => { + return notification.get('message') === ownProps.message + }) || Immutable.Map() + + const props = {} + props.message = ownProps.message + props.greeting = notification.get('greeting') + props.buttons = notification.get('buttons') // TODO (nejc) only primitives + props.style = notification.getIn(['options', 'style']) + props.advancedText = notification.getIn(['options', 'advancedText']) + props.advancedLink = notification.getIn(['options', 'advancedLink']) + props.persist = notification.getIn(['options', 'persist']) + props.nonce = notification.getIn(['options', 'nonce']) + + return props + } + render () { - let i = 0 - const options = this.props.detail.get('options') - const greeting = this.props.detail.get('greeting') return
{ - greeting - ? {greeting} + this.props.greeting + ? {this.props.greeting} : null } - {this.props.detail.get('message')} + {this.props.message} { - options.get('advancedText') && options.get('advancedLink') - ? {options.get('advancedText')} + this.props.advancedText && this.props.advancedLink + ? {this.props.advancedText} : null }
{ - options.get('persist') + this.props.persist ? { this.checkbox = node }} /> - : null } { - this.props.detail.get('buttons').map((button) => + this.props.buttons.map((button, i) => ) } @@ -95,6 +125,8 @@ class NotificationItem extends ImmutableComponent { } } +module.exports = ReduxComponent.connect(NotificationItem) + const styles = StyleSheet.create({ flexJustifyBetween: { display: 'flex', @@ -124,5 +156,3 @@ const styles = StyleSheet.create({ margin: '5px' } }) - -module.exports = NotificationItem