From 7827594c4603f0931e80287d545d8b61525baedf Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Thu, 15 Jun 2017 13:38:17 +0200 Subject: [PATCH] Converts AutofillAddressPanel into redux component Resolves #9444 Auditors: @bsclifton @bridiver Test Plan: --- .../autofill/autofillAddressPanel.js | 233 +++++++++++------- app/renderer/components/main/main.js | 10 +- 2 files changed, 139 insertions(+), 104 deletions(-) diff --git a/app/renderer/components/autofill/autofillAddressPanel.js b/app/renderer/components/autofill/autofillAddressPanel.js index bb20fa9d2e1..79bf48724d6 100644 --- a/app/renderer/components/autofill/autofillAddressPanel.js +++ b/app/renderer/components/autofill/autofillAddressPanel.js @@ -3,16 +3,13 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ const React = require('react') -const ImmutableComponent = require('../immutableComponent') -const Dialog = require('../common/dialog') -const Button = require('../common/button') -const windowActions = require('../../../../js/actions/windowActions') -const appActions = require('../../../../js/actions/appActions') -const KeyCodes = require('../../../common/constants/keyCodes') - +const Immutable = require('immutable') const {css} = require('aphrodite/no-important') -const commonStyles = require('../styles/commonStyles') +// Components +const ReduxComponent = require('../reduxComponent') +const Dialog = require('../common/dialog') +const Button = require('../common/button') const { CommonFormLarge, CommonFormSection, @@ -21,6 +18,15 @@ const { commonFormStyles } = require('../common/commonForm') +// Actions +const windowActions = require('../../../../js/actions/windowActions') +const appActions = require('../../../../js/actions/appActions') + +// Constants +const KeyCodes = require('../../../common/constants/keyCodes') + +// Styles +const commonStyles = require('../styles/commonStyles') const commonForm = css( commonStyles.formControl, commonStyles.textbox, @@ -29,9 +35,9 @@ const commonForm = css( commonFormStyles.input__box ) -class AutofillAddressPanel extends ImmutableComponent { - constructor () { - super() +class AutofillAddressPanel extends React.Component { + constructor (props) { + super(props) this.onNameChange = this.onNameChange.bind(this) this.onOrganizationChange = this.onOrganizationChange.bind(this) this.onStreetAddressChange = this.onStreetAddressChange.bind(this) @@ -45,118 +51,118 @@ class AutofillAddressPanel extends ImmutableComponent { this.onSave = this.onSave.bind(this) this.onClick = this.onClick.bind(this) } + onNameChange (e) { let currentDetail = this.props.currentDetail currentDetail = currentDetail.set('name', e.target.value) - if (this.nameOnAddress.value !== e.target.value) { - this.nameOnAddress.value = e.target.value - } windowActions.setAutofillAddressDetail(currentDetail, this.props.originalDetail) } onOrganizationChange (e) { let currentDetail = this.props.currentDetail currentDetail = currentDetail.set('organization', e.target.value) - if (this.organization.value !== e.target.value) { - this.organization.value = e.target.value - } windowActions.setAutofillAddressDetail(currentDetail, this.props.originalDetail) } onStreetAddressChange (e) { let currentDetail = this.props.currentDetail currentDetail = currentDetail.set('streetAddress', e.target.value) - if (this.streetAddress.value !== e.target.value) { - this.streetAddress.value = e.target.value - } windowActions.setAutofillAddressDetail(currentDetail, this.props.originalDetail) } onCityChange (e) { let currentDetail = this.props.currentDetail currentDetail = currentDetail.set('city', e.target.value) windowActions.setAutofillAddressDetail(currentDetail, this.props.originalDetail) - if (this.city.value !== e.target.value) { - this.city.value = e.target.value - } } onStateChange (e) { let currentDetail = this.props.currentDetail currentDetail = currentDetail.set('state', e.target.value) - if (this.state.value !== e.target.value) { - this.state.value = e.target.value - } windowActions.setAutofillAddressDetail(currentDetail, this.props.originalDetail) } onPostalCodeChange (e) { let currentDetail = this.props.currentDetail currentDetail = currentDetail.set('postalCode', e.target.value) - if (this.postalCode.value !== e.target.value) { - this.postalCode.value = e.target.value - } windowActions.setAutofillAddressDetail(currentDetail, this.props.originalDetail) } onCountryChange (e) { let currentDetail = this.props.currentDetail currentDetail = currentDetail.set('country', e.target.value) - if (this.country.value !== e.target.value) { - this.country.value = e.target.value - } windowActions.setAutofillAddressDetail(currentDetail, this.props.originalDetail) } onPhoneChange (e) { let currentDetail = this.props.currentDetail currentDetail = currentDetail.set('phone', e.target.value) - if (this.phone.value !== e.target.value) { - this.phone.value = e.target.value - } windowActions.setAutofillAddressDetail(currentDetail, this.props.originalDetail) } onEmailChange (e) { let currentDetail = this.props.currentDetail currentDetail = currentDetail.set('email', e.target.value) - if (this.email.value !== e.target.value) { - this.email.value = e.target.value - } windowActions.setAutofillAddressDetail(currentDetail, this.props.originalDetail) } + onKeyDown (e) { switch (e.keyCode) { case KeyCodes.ENTER: this.onSave() break case KeyCodes.ESC: - this.props.onHide() + this.onHide() break } } + onSave () { - appActions.addAutofillAddress(this.props.currentDetail, this.props.originalDetail) - this.props.onHide() + appActions.addAutofillAddress({}, this.props.originalDetail) + this.onHide() } + onClick (e) { e.stopPropagation() } - get disableSaveButton () { - let currentDetail = this.props.currentDetail - if (!currentDetail.size) return true - if (!currentDetail.get('name') && !currentDetail.get('organization') && - !currentDetail.get('streetAddress') && !currentDetail.get('city') && - !currentDetail.get('state') && !currentDetail.get('country') && - !currentDetail.get('phone') && !currentDetail.get('email')) return true - return false - } + componentDidMount () { this.nameOnAddress.focus() - this.nameOnAddress.value = this.props.currentDetail.get('name') || '' - this.organization.value = this.props.currentDetail.get('organization') || '' - this.streetAddress.value = this.props.currentDetail.get('streetAddress') || '' - this.city.value = this.props.currentDetail.get('city') || '' - this.state.value = this.props.currentDetail.get('state') || '' - this.postalCode.value = this.props.currentDetail.get('postalCode') || '' - this.country.value = this.props.currentDetail.get('country') || '' - this.phone.value = this.props.currentDetail.get('phone') || '' - this.email.value = this.props.currentDetail.get('email') || '' } + + onHide () { + windowActions.setAutofillAddressDetail() + } + + mergeProps (state, ownProps) { + const currentWindow = state.get('currentWindow') + const currentDetail = currentWindow.getIn(['autofillAddressDetail', 'currentDetail'], Immutable.Map()) + const originalDetail = currentWindow.getIn(['autofillAddressDetail', 'originalDetail'], Immutable.Map()) + + const props = {} + // Used in renderer + props.name = currentDetail.get('name', '') + props.organization = currentDetail.get('organization', '') + props.streetAddress = currentDetail.get('streetAddress', '') + props.city = currentDetail.get('city', '') + props.state = currentDetail.get('state', '') + props.postalCode = currentDetail.get('postalCode', '') + props.country = currentDetail.get('country', '') + props.phone = currentDetail.get('phone', '') + props.email = currentDetail.get('email', '') + props.disableSaveButton = !currentDetail.get('name') && !currentDetail.get('organization') && + !currentDetail.get('streetAddress') && !currentDetail.get('city') && + !currentDetail.get('state') && !currentDetail.get('country') && + !currentDetail.get('phone') && !currentDetail.get('email') + + // Used in other function + props.originalName = originalDetail.get('name') + props.originalOrganization = originalDetail.get('organization') + props.originalStreetAddress = originalDetail.get('streetAddress') + props.originalCity = originalDetail.get('city') + props.originalState = originalDetail.get('state') + props.originalPostalCode = originalDetail.get('postalCode') + props.originalCountry = originalDetail.get('country') + props.originalPhone = originalDetail.get('phone') + props.originalEmail = originalDetail.get('email') + + return props + } + render () { - return + return @@ -173,62 +179,99 @@ class AutofillAddressPanel extends ImmutableComponent {