Skip to content

Commit 44aab83

Browse files
ronlavi2412jeff-phillips-18
authored andcommitted
fix(LoginPage): Add submit error callback handling (#952)
1 parent 2cc0ab9 commit 44aab83

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

packages/patternfly-3/patternfly-react/src/components/LoginPage/LoginPage.stories.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ const createProps = () => {
9696
},
9797
disableSubmit: false,
9898
submitText: card.form.submitText,
99-
onSubmit: e => storyAction(e, 'Form was submitted')
99+
onSubmit: (e, onError) => {
100+
onError(card.form.error);
101+
storyAction(e, 'Form was submitted');
102+
}
100103
},
101104
social: {
102105
links: createLogoList()

packages/patternfly-3/patternfly-react/src/components/LoginPage/LoginPage.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DropdownButton } from 'react-bootstrap';
44
import englishMessages from './mocks/messages.en';
55
import frenchMessages from './mocks/messages.fr';
66
import { LoginPage, LoginCardWithValidation } from './index';
7-
import { KEY_CODES } from '../../common/helpers';
7+
import { KEY_CODES, noop } from '../../common/helpers';
88

99
const { Input, ForgotPassword, SignUp } = LoginPage.Card;
1010
const { FooterLinks } = LoginPage;
@@ -257,6 +257,22 @@ test('Submit while password is too short raises the correct error', () => {
257257
).toEqual(englishMessages.card.passwordField.errors.short);
258258
});
259259

260+
test('valid form should be submitted and raise a "server error"', () => {
261+
const serverError = 'server error';
262+
const props = { ...createProps() };
263+
props.card.form.onSubmit = (e, onError) => onError(serverError);
264+
const component = mount(<LoginPage {...props} />);
265+
const passwordElement = component.find('input[type="password"]').at(0);
266+
const usernameElement = component.find('input[type="email"]').at(0);
267+
passwordElement.simulate('change', { target: { value: 'validPassword' } });
268+
usernameElement.simulate('change', { target: { value: 'validUser@gmail.com' } });
269+
270+
const validator = component.find(LoginCardWithValidation).instance();
271+
validator.onSubmit({ preventDefault: noop, target: { submit: noop } });
272+
component.update();
273+
expect(validator.state.form.submitError).toBe(serverError);
274+
});
275+
260276
test('Submit while username is invalid cause a specific error to be shown and onChange is disappears', () => {
261277
const component = mount(<LoginPage {...createProps()} />);
262278
const usernameElement = component.find('input[type="email"]').at(0);

packages/patternfly-3/patternfly-react/src/components/LoginPage/components/LoginCardComponents/LoginCardWithValidation.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,36 @@ class LoginCardWithValidation extends React.Component {
7777
onSubmit = e => {
7878
e.preventDefault();
7979
if (this.isFormValid()) {
80-
const { onSubmit, submitError } = this.props;
81-
onSubmit && onSubmit(e);
82-
submitError &&
83-
this.setState({
84-
form: {
85-
...this.state.form,
86-
showError: true,
87-
error: submitError,
88-
disableSubmit: true,
89-
isSubmitting: true
90-
}
91-
});
80+
this.onSubmitStart();
81+
this.props.onSubmit(e, this.onSubmitError);
9282
} else {
9383
this.handleOnInputErrors();
9484
}
9585
};
9686

87+
onSubmitStart = () => {
88+
this.setState({
89+
form: {
90+
...this.state.form,
91+
disableSubmit: true,
92+
isSubmitting: true,
93+
showError: false
94+
}
95+
});
96+
};
97+
98+
onSubmitError = submitError => {
99+
this.setState({
100+
form: {
101+
...this.state.form,
102+
showError: true,
103+
submitError,
104+
disableSubmit: false,
105+
isSubmitting: false
106+
}
107+
});
108+
};
109+
97110
getModifiedProps = () => {
98111
const { usernameField, passwordField } = this.props;
99112
const passwordFieldWarningType = this.state.isCapsLock ? 'capsLock' : this.state.passwordField.warningType;
@@ -121,7 +134,8 @@ class LoginCardWithValidation extends React.Component {
121134
onSubmit: e => this.onSubmit(e),
122135
showError: this.state.form.showError,
123136
disableSubmit: this.state.form.disableSubmit,
124-
isSubmitting: this.state.form.isSubmitting
137+
isSubmitting: this.state.form.isSubmitting,
138+
submitError: this.state.form.submitError
125139
};
126140
};
127141

0 commit comments

Comments
 (0)