mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-30 02:56:20 -05:00
added simple memoizing cache for email step
This commit is contained in:
parent
3bc6e7ff6f
commit
0656ab6a9e
1 changed files with 18 additions and 2 deletions
|
@ -21,6 +21,7 @@ class EmailStep extends React.Component {
|
||||||
'handleSetEmailRef',
|
'handleSetEmailRef',
|
||||||
'handleValidSubmit',
|
'handleValidSubmit',
|
||||||
'validateEmail',
|
'validateEmail',
|
||||||
|
'validateEmailRemotelyWithCache',
|
||||||
'validateForm',
|
'validateForm',
|
||||||
'setCaptchaRef',
|
'setCaptchaRef',
|
||||||
'captchaSolved',
|
'captchaSolved',
|
||||||
|
@ -30,8 +31,10 @@ class EmailStep extends React.Component {
|
||||||
this.state = {
|
this.state = {
|
||||||
captchaIsLoading: true
|
captchaIsLoading: true
|
||||||
};
|
};
|
||||||
|
// simple object to memoize remote requests for email addresses.
|
||||||
|
// keeps us from submitting multiple requests for same data.
|
||||||
|
this.emailRemoteCache = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount () {
|
componentDidMount () {
|
||||||
// automatically start with focus on username field
|
// automatically start with focus on username field
|
||||||
if (this.emailInput) this.emailInput.focus();
|
if (this.emailInput) this.emailInput.focus();
|
||||||
|
@ -78,11 +81,24 @@ class EmailStep extends React.Component {
|
||||||
},
|
},
|
||||||
true);
|
true);
|
||||||
}
|
}
|
||||||
|
// simple function to memoize remote requests for usernames
|
||||||
|
validateEmailRemotelyWithCache (email) {
|
||||||
|
if (this.emailRemoteCache.hasOwnProperty(email)) {
|
||||||
|
return Promise.resolve(this.emailRemoteCache[email]);
|
||||||
|
}
|
||||||
|
// email is not in our cache
|
||||||
|
return validate.validateEmailRemotely(email).then(
|
||||||
|
remoteResult => {
|
||||||
|
this.emailRemoteCache[email] = remoteResult;
|
||||||
|
return remoteResult;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
validateEmail (email) {
|
validateEmail (email) {
|
||||||
if (!email) return this.props.intl.formatMessage({id: 'general.required'});
|
if (!email) return this.props.intl.formatMessage({id: 'general.required'});
|
||||||
const localResult = validate.validateEmailLocally(email);
|
const localResult = validate.validateEmailLocally(email);
|
||||||
if (!localResult.valid) return this.props.intl.formatMessage({id: localResult.errMsgId});
|
if (!localResult.valid) return this.props.intl.formatMessage({id: localResult.errMsgId});
|
||||||
return validate.validateEmailRemotely(email).then(
|
return this.validateEmailRemotelyWithCache(email).then(
|
||||||
remoteResult => {
|
remoteResult => {
|
||||||
if (remoteResult.valid === true) {
|
if (remoteResult.valid === true) {
|
||||||
return null;
|
return null;
|
||||||
|
|
Loading…
Reference in a new issue