Commit 7b2e7582 authored by Ben Wheeler's avatar Ben Wheeler

revise function names and handling of registration errors

parent e3d3d97c
...@@ -43,12 +43,12 @@ class JoinFlow extends React.Component { ...@@ -43,12 +43,12 @@ class JoinFlow extends React.Component {
this.state = this.initialState; this.state = this.initialState;
} }
canTryAgain () { canTryAgain () {
return (this.state.registrationError.canTryAgain && this.state.numAttempts <= 1); return (this.state.registrationError.errorAllowsTryAgain && this.state.numAttempts <= 1);
} }
handleCaptchaError () { handleCaptchaError () {
this.setState({ this.setState({
registrationError: { registrationError: {
canTryAgain: false, errorAllowsTryAgain: false,
errorMsg: this.props.intl.formatMessage({ errorMsg: this.props.intl.formatMessage({
id: 'registration.errorCaptcha' id: 'registration.errorCaptcha'
}) })
...@@ -64,36 +64,34 @@ class JoinFlow extends React.Component { ...@@ -64,36 +64,34 @@ class JoinFlow extends React.Component {
this.handleSubmitRegistration(this.state.formData); this.handleSubmitRegistration(this.state.formData);
}); });
} }
getBodyErrors (err, body, res) { getErrorsFromResponse (err, body, res) {
return (!err && res.statusCode === 200 && body && body[0] && body[0].errors); const errorsFromResponse = [];
} if (!err && res.statusCode === 200 && body && body[0]) {
getSingleError (bodyErrors) { const responseBodyErrors = body[0].errors;
if (Object.keys(bodyErrors).length === 1) { if (responseBodyErrors) {
const fieldName = Object.keys(bodyErrors)[0]; Object.keys(responseBodyErrors).forEach(fieldName => {
if (bodyErrors[fieldName].length === 1) { const errorStrs = responseBodyErrors[fieldName];
return {fieldName: fieldName, errorStr: bodyErrors[fieldName][0]}; errorStrs.forEach(errorStr => {
errorsFromResponse.push({fieldName: fieldName, errorStr: errorStr});
});
});
} }
} }
return null; return errorsFromResponse;
} }
getCustomErrMsg (bodyErrors) { getCustomErrMsg (errorsFromResponse) {
if (!errorsFromResponse || errorsFromResponse.length === 0) return null;
let customErrMsg = ''; let customErrMsg = '';
// body can include zero or more error objects, each // body can include zero or more error objects. Here we assemble
// with its own key and description. Here we assemble
// all of them into a single string, customErrMsg. // all of them into a single string, customErrMsg.
const errorKeys = Object.keys(bodyErrors); errorsFromResponse.forEach(errorFromResponse => {
errorKeys.forEach(key => {
const vals = bodyErrors[key];
vals.forEach(val => {
if (customErrMsg.length) customErrMsg += '; '; if (customErrMsg.length) customErrMsg += '; ';
customErrMsg += `${key}: ${val}`; customErrMsg += `${errorFromResponse.fieldName}: ${errorFromResponse.errorStr}`;
});
}); });
if (!customErrMsg) return null; // if no key-val pairs
const problemsStr = this.props.intl.formatMessage({id: 'registration.problemsAre'}); const problemsStr = this.props.intl.formatMessage({id: 'registration.problemsAre'});
return `${problemsStr} "${customErrMsg}"`; return `${problemsStr}: "${customErrMsg}"`;
} }
getRegistrationSuccess (err, body, res) { registrationIsSuccessful (err, body, res) {
return !!(!err && res.statusCode === 200 && body && body[0] && body[0].success); return !!(!err && res.statusCode === 200 && body && body[0] && body[0].success);
} }
// example of failing response: // example of failing response:
...@@ -126,41 +124,45 @@ class JoinFlow extends React.Component { ...@@ -126,41 +124,45 @@ class JoinFlow extends React.Component {
numAttempts: this.state.numAttempts + 1, numAttempts: this.state.numAttempts + 1,
waiting: false waiting: false
}, () => { }, () => {
const success = this.getRegistrationSuccess(err, body, res); const success = this.registrationIsSuccessful(err, body, res);
if (success) { if (success) {
this.props.refreshSession(); this.props.refreshSession();
this.setState({step: this.state.step + 1}); this.setState({step: this.state.step + 1});
return; return;
} }
// now we know there was some error. // now we know something went wrong -- either an actual error (client-side
// if the error was client-side, prompt user to try again // or server-side), or just a problem with the registration content.
if (err || (res.statusCode >= 400 && res.statusCode < 500)) {
this.setState({registrationError: {canTryAgain: true}}); // if an actual error, prompt user to try again.
if (err || res.statusCode !== 200) {
this.setState({registrationError: {errorAllowsTryAgain: true}});
return; return;
} }
// now we know error was server-side.
// if server provided us info on why registration failed, // now we know there was a problem with the registration content.
// If the server provided us info on why registration failed,
// build a summary explanation string // build a summary explanation string
let errorMsg = null; let errorMsg = null;
const bodyErrors = this.getBodyErrors(err, body, res); const errorsFromResponse = this.getErrorsFromResponse(err, body, res);
if (bodyErrors) { // if there was exactly one error, check if we have a pre-written message
const singleError = this.getSingleError(bodyErrors); // about that precise error
let singleErrMsgId = null; if (errorsFromResponse.length === 1) {
if (singleError) { const singleErrMsgId = validate.responseErrorMsg(
singleErrMsgId = validate.responseErrorMsg( errorsFromResponse[0].fieldName,
singleError.fieldName, errorsFromResponse[0].errorStr
singleError.errorStr
); );
}
if (singleErrMsgId) { // one error that we have a predefined explanation string for if (singleErrMsgId) { // one error that we have a predefined explanation string for
errorMsg = this.props.intl.formatMessage({id: singleErrMsgId}); errorMsg = this.props.intl.formatMessage({id: singleErrMsgId});
} else { // multiple errors, or unusual error; need custom message
errorMsg = this.getCustomErrMsg(bodyErrors);
} }
} }
// if we have more than one error, build a custom message with all of the
// server-provided error messages
if (!errorMsg && errorsFromResponse.length > 0) {
errorMsg = this.getCustomErrMsg(errorsFromResponse);
}
this.setState({ this.setState({
registrationError: { registrationError: {
canTryAgain: false, errorAllowsTryAgain: false,
errorMsg: errorMsg errorMsg: errorMsg
} }
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment