From c415babb83867d123a87426052697ad96ee6fb9a Mon Sep 17 00:00:00 2001 From: Vesa Luusua Date: Mon, 16 Apr 2018 21:20:26 +0300 Subject: [PATCH] Refactor PasswordChangeForm: use Form connector from Final Form --- .../PasswordChangeForm/PasswordChangeForm.js | 329 +++++++++--------- .../PasswordChangePage.test.js | 3 +- .../PasswordChangePage.test.js.snap | 27 ++ 3 files changed, 200 insertions(+), 159 deletions(-) diff --git a/src/containers/PasswordChangeForm/PasswordChangeForm.js b/src/containers/PasswordChangeForm/PasswordChangeForm.js index 83e76089..55711e92 100644 --- a/src/containers/PasswordChangeForm/PasswordChangeForm.js +++ b/src/containers/PasswordChangeForm/PasswordChangeForm.js @@ -2,13 +2,13 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { compose } from 'redux'; import { FormattedMessage, injectIntl, intlShape } from 'react-intl'; -import { reduxForm, propTypes as formPropTypes } from 'redux-form'; +import { Form as FinalForm } from 'react-final-form'; import classNames from 'classnames'; import { propTypes } from '../../util/types'; import * as validators from '../../util/validators'; import { ensureCurrentUser } from '../../util/data'; import { isChangePasswordWrongPassword } from '../../util/errors'; -import { Form, PrimaryButton, TextInputField } from '../../components'; +import { Form, PrimaryButton, FieldTextInput } from '../../components'; import css from './PasswordChangeForm.css'; @@ -23,158 +23,173 @@ class PasswordChangeFormComponent extends Component { window.clearTimeout(this.resetTimeoutId); } render() { - const { - rootClassName, - className, - changePasswordError, - currentUser, - form, - handleSubmit, - submitting, - inProgress, - intl, - invalid, - pristine, - ready, - reset, - } = this.props; - - const user = ensureCurrentUser(currentUser); - - if (!user.id) { - return null; - } - - // New password - const newPasswordLabel = intl.formatMessage({ - id: 'PasswordChangeForm.newPasswordLabel', - }); - const newPasswordPlaceholder = intl.formatMessage({ - id: 'PasswordChangeForm.newPasswordPlaceholder', - }); - const newPasswordRequiredMessage = intl.formatMessage({ - id: 'PasswordChangeForm.newPasswordRequired', - }); - const newPasswordRequired = validators.requiredStringNoTrim(newPasswordRequiredMessage); - - const passwordMinLengthMessage = intl.formatMessage( - { - id: 'PasswordChangeForm.passwordTooShort', - }, - { - minLength: validators.PASSWORD_MIN_LENGTH, - } - ); - const passwordMaxLengthMessage = intl.formatMessage( - { - id: 'PasswordChangeForm.passwordTooLong', - }, - { - maxLength: validators.PASSWORD_MAX_LENGTH, - } - ); - - const passwordMinLength = validators.minLength( - passwordMinLengthMessage, - validators.PASSWORD_MIN_LENGTH - ); - const passwordMaxLength = validators.maxLength( - passwordMaxLengthMessage, - validators.PASSWORD_MAX_LENGTH - ); - - // password - const passwordLabel = intl.formatMessage({ - id: 'PasswordChangeForm.passwordLabel', - }); - const passwordPlaceholder = intl.formatMessage({ - id: 'PasswordChangeForm.passwordPlaceholder', - }); - const passwordRequiredMessage = intl.formatMessage({ - id: 'PasswordChangeForm.passwordRequired', - }); - - const passwordRequired = validators.requiredStringNoTrim(passwordRequiredMessage); - - const passwordFailedMessage = intl.formatMessage({ - id: 'PasswordChangeForm.passwordFailed', - }); - const passwordErrorText = isChangePasswordWrongPassword(changePasswordError) - ? passwordFailedMessage - : null; - - const confirmClasses = classNames(css.confirmChangesSection, { - [css.confirmChangesSectionVisible]: !pristine, - }); - - const genericFailure = - changePasswordError && !passwordErrorText ? ( - - - - ) : null; - - const classes = classNames(rootClassName || css.root, className); - const submitDisabled = invalid || submitting || inProgress; - return ( -
{ - handleSubmit(values) - .then(() => { - this.resetTimeoutId = window.setTimeout(reset, RESET_TIMEOUT); - }) - .catch(() => { - // Error is handled in duck file already. - }); + { + const { + rootClassName, + className, + formId, + changePasswordError, + currentUser, + handleSubmit, + submitting, + inProgress, + intl, + invalid, + pristine, + ready, + reset, + } = fieldRenderProps; + + const user = ensureCurrentUser(currentUser); + + if (!user.id) { + return null; + } + + // New password + const newPasswordLabel = intl.formatMessage({ + id: 'PasswordChangeForm.newPasswordLabel', + }); + const newPasswordPlaceholder = intl.formatMessage({ + id: 'PasswordChangeForm.newPasswordPlaceholder', + }); + const newPasswordRequiredMessage = intl.formatMessage({ + id: 'PasswordChangeForm.newPasswordRequired', + }); + const newPasswordRequired = validators.requiredStringNoTrim(newPasswordRequiredMessage); + + const passwordMinLengthMessage = intl.formatMessage( + { + id: 'PasswordChangeForm.passwordTooShort', + }, + { + minLength: validators.PASSWORD_MIN_LENGTH, + } + ); + const passwordMaxLengthMessage = intl.formatMessage( + { + id: 'PasswordChangeForm.passwordTooLong', + }, + { + maxLength: validators.PASSWORD_MAX_LENGTH, + } + ); + + const passwordMinLength = validators.minLength( + passwordMinLengthMessage, + validators.PASSWORD_MIN_LENGTH + ); + const passwordMaxLength = validators.maxLength( + passwordMaxLengthMessage, + validators.PASSWORD_MAX_LENGTH + ); + + // password + const passwordLabel = intl.formatMessage({ + id: 'PasswordChangeForm.passwordLabel', + }); + const passwordPlaceholder = intl.formatMessage({ + id: 'PasswordChangeForm.passwordPlaceholder', + }); + const passwordRequiredMessage = intl.formatMessage({ + id: 'PasswordChangeForm.passwordRequired', + }); + + const passwordRequired = validators.requiredStringNoTrim(passwordRequiredMessage); + + const passwordFailedMessage = intl.formatMessage({ + id: 'PasswordChangeForm.passwordFailed', + }); + const passwordErrorText = isChangePasswordWrongPassword(changePasswordError) + ? passwordFailedMessage + : null; + + const confirmClasses = classNames(css.confirmChangesSection, { + [css.confirmChangesSectionVisible]: !pristine, + }); + + const genericFailure = + changePasswordError && !passwordErrorText ? ( + + + + ) : null; + + const classes = classNames(rootClassName || css.root, className); + const submitDisabled = invalid || submitting || inProgress; + + return ( + { + handleSubmit(values) + .then(() => { + this.resetTimeoutId = window.setTimeout(reset, RESET_TIMEOUT); + }) + .catch(() => { + // Error is handled in duck file already. + }); + }} + > +
+ +
+ +
+

+ +

+

+ +

+ + +
+
+ {genericFailure} + + + +
+ + ); }} - > -
- -
- -
-

- -

-

- -

- - -
-
- {genericFailure} - - - -
- + /> ); } } @@ -184,24 +199,22 @@ PasswordChangeFormComponent.defaultProps = { className: null, changePasswordError: null, inProgress: false, + formId: null, }; const { bool, string } = PropTypes; PasswordChangeFormComponent.propTypes = { - ...formPropTypes, rootClassName: string, className: string, changePasswordError: propTypes.error, inProgress: bool, intl: intlShape.isRequired, ready: bool.isRequired, + formId: string, }; -const defaultFormName = 'PasswordChangeForm'; - -const PasswordChangeForm = compose(reduxForm({ form: defaultFormName }), injectIntl)( - PasswordChangeFormComponent -); +const PasswordChangeForm = compose(injectIntl)(PasswordChangeFormComponent); +PasswordChangeForm.displayName = 'PasswordChangeForm'; export default PasswordChangeForm; diff --git a/src/containers/PasswordChangePage/PasswordChangePage.test.js b/src/containers/PasswordChangePage/PasswordChangePage.test.js index 3677dfb5..ff6049ac 100644 --- a/src/containers/PasswordChangePage/PasswordChangePage.test.js +++ b/src/containers/PasswordChangePage/PasswordChangePage.test.js @@ -1,6 +1,6 @@ import React from 'react'; import { renderShallow } from '../../util/test-helpers'; -import { fakeIntl } from '../../util/test-data'; +import { fakeIntl, createCurrentUser } from '../../util/test-data'; import { PasswordChangePageComponent } from './PasswordChangePage'; const noop = () => null; @@ -14,6 +14,7 @@ describe('PasswordChangePage', () => { location={{ search: '' }} scrollingDisabled={false} authInProgress={false} + currentUser={createCurrentUser('user1')} currentUserHasListings={false} isAuthenticated={false} onChange={noop} diff --git a/src/containers/PasswordChangePage/__snapshots__/PasswordChangePage.test.js.snap b/src/containers/PasswordChangePage/__snapshots__/PasswordChangePage.test.js.snap index a018c746..58a3625f 100644 --- a/src/containers/PasswordChangePage/__snapshots__/PasswordChangePage.test.js.snap +++ b/src/containers/PasswordChangePage/__snapshots__/PasswordChangePage.test.js.snap @@ -72,6 +72,33 @@ exports[`PasswordChangePage matches snapshot 1`] = ` values={Object {}} /> +