mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-27 01:25:52 -05:00
refactor handleRegistrationResponse
This commit is contained in:
parent
643b4a611b
commit
1d976c7cdf
1 changed files with 38 additions and 34 deletions
|
@ -27,6 +27,7 @@ class JoinFlow extends React.Component {
|
||||||
bindAll(this, [
|
bindAll(this, [
|
||||||
'handleAdvanceStep',
|
'handleAdvanceStep',
|
||||||
'handlePrepareToRegister',
|
'handlePrepareToRegister',
|
||||||
|
'handleRegistrationResponse',
|
||||||
'handleSubmitRegistration'
|
'handleSubmitRegistration'
|
||||||
]);
|
]);
|
||||||
this.state = {
|
this.state = {
|
||||||
|
@ -45,6 +46,42 @@ class JoinFlow extends React.Component {
|
||||||
this.handleSubmitRegistration(this.state.formData);
|
this.handleSubmitRegistration(this.state.formData);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
handleRegistrationResponse (err, body, res) {
|
||||||
|
this.setState({waiting: false}, () => {
|
||||||
|
let errStr = '';
|
||||||
|
if (!err && res.statusCode === 200) {
|
||||||
|
if (body && body[0]) {
|
||||||
|
if (body[0].success) {
|
||||||
|
this.props.refreshSession();
|
||||||
|
this.setState({
|
||||||
|
step: this.state.step + 1
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (body[0].errors) {
|
||||||
|
// body can include zero or more error objects, each
|
||||||
|
// with its own key and description. Here we assemble
|
||||||
|
// all of them into a single string, errStr.
|
||||||
|
const errorKeys = Object.keys(body[0].errors);
|
||||||
|
errorKeys.forEach(key => {
|
||||||
|
const val = body[0].errors[key];
|
||||||
|
if (val && val[0]) {
|
||||||
|
if (errStr.length) errStr += '; ';
|
||||||
|
errStr += `${key}: ${val[0]}`;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (!errStr.length && body[0].msg) errStr = body[0].msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.setState({
|
||||||
|
registrationError: errStr ||
|
||||||
|
`${this.props.intl.formatMessage({
|
||||||
|
id: 'registration.generalError'
|
||||||
|
})} (${res.statusCode})`
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
handleSubmitRegistration (formData) {
|
handleSubmitRegistration (formData) {
|
||||||
this.setState({waiting: true}, () => {
|
this.setState({waiting: true}, () => {
|
||||||
api({
|
api({
|
||||||
|
@ -68,40 +105,7 @@ class JoinFlow extends React.Component {
|
||||||
// scratchr2/middleware/csrf.py line 237.
|
// scratchr2/middleware/csrf.py line 237.
|
||||||
}
|
}
|
||||||
}, (err, body, res) => {
|
}, (err, body, res) => {
|
||||||
this.setState({waiting: false}, () => {
|
this.handleRegistrationResponse(err, body, res);
|
||||||
let errStr = '';
|
|
||||||
if (!err && res.statusCode === 200) {
|
|
||||||
if (body && body[0]) {
|
|
||||||
if (body[0].success) {
|
|
||||||
this.props.refreshSession();
|
|
||||||
this.setState({
|
|
||||||
step: this.state.step + 1
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (body[0].errors) {
|
|
||||||
// body can include zero or more error objects, each
|
|
||||||
// with its own key and description. Here we assemble
|
|
||||||
// all of them into a single string, errStr.
|
|
||||||
const errorKeys = Object.keys(body[0].errors);
|
|
||||||
errorKeys.forEach(key => {
|
|
||||||
const val = body[0].errors[key];
|
|
||||||
if (val && val[0]) {
|
|
||||||
if (errStr.length) errStr += '; ';
|
|
||||||
errStr += `${key}: ${val[0]}`;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (!errStr.length && body[0].msg) errStr = body[0].msg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.setState({
|
|
||||||
registrationError: errStr ||
|
|
||||||
`${this.props.intl.formatMessage({
|
|
||||||
id: 'registration.generalError'
|
|
||||||
})} (${res.statusCode})`
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue