handle error when teacher country selection fails to resolve to country name

This commit is contained in:
Ben Wheeler 2019-08-06 15:00:18 -04:00
parent 51aa38fb34
commit 564baf2ebc
2 changed files with 28 additions and 7 deletions

View file

@ -408,10 +408,14 @@ class DemographicsStep extends React.Component {
constructor (props) { constructor (props) {
super(props); super(props);
bindAll(this, [ bindAll(this, [
'birthDateValidator',
'countryValidator',
'getCountryName',
'getMonthOptions', 'getMonthOptions',
'getYearOptions', 'getYearOptions',
'handleChooseGender', 'handleChooseGender',
'handleValidSubmit' 'handleValidSubmit',
'isValidBirthdate'
]); ]);
this.state = { this.state = {
otherDisabled: true otherDisabled: true
@ -435,16 +439,24 @@ class DemographicsStep extends React.Component {
handleChooseGender (name, gender) { handleChooseGender (name, gender) {
this.setState({otherDisabled: gender !== 'other'}); this.setState({otherDisabled: gender !== 'other'});
} }
handleValidSubmit (formData) {
// look up country name using user's country code selection // look up country name using user's country code selection
if (formData.countryCode) { getCountryName (values) {
const countryInfo = countryData.lookupCountryInfo(formData.countryCode); if (values.countryCode) {
const countryInfo = countryData.lookupCountryInfo(values.countryCode);
if (countryInfo) { if (countryInfo) {
formData.user.country = countryInfo.name; return countryInfo.name;
} }
} }
return null;
}
handleValidSubmit (formData) {
const countryName = this.getCountryName(formData);
if (countryName && formData.user) {
formData.user.country = countryName;
return this.props.onNextStep(formData); return this.props.onNextStep(formData);
} }
return false;
}
isValidBirthdate (year, month) { isValidBirthdate (year, month) {
const birthdate = new Date( const birthdate = new Date(
year, year,
@ -457,6 +469,11 @@ class DemographicsStep extends React.Component {
const isValid = this.isValidBirthdate(values['user.birth.year'], values['user.birth.month']); const isValid = this.isValidBirthdate(values['user.birth.year'], values['user.birth.month']);
return isValid ? true : this.props.intl.formatMessage({id: 'teacherRegistration.validationAge'}); return isValid ? true : this.props.intl.formatMessage({id: 'teacherRegistration.validationAge'});
} }
countryValidator (values) {
const countryName = this.getCountryName(values);
if (countryName) return true;
return this.props.intl.formatMessage({id: 'general.invalidSelection'});
}
render () { render () {
const countryOptions = getCountryOptions(this.props.intl); const countryOptions = getCountryOptions(this.props.intl);
return ( return (
@ -539,6 +556,9 @@ class DemographicsStep extends React.Component {
label={this.props.intl.formatMessage({id: 'general.country'})} label={this.props.intl.formatMessage({id: 'general.country'})}
name="countryCode" name="countryCode"
options={countryOptions} options={countryOptions}
validations={{
countryVal: values => this.countryValidator(values)
}}
value={countryOptions[0].value} value={countryOptions[0].value}
/> />
<Checkbox <Checkbox

View file

@ -33,6 +33,7 @@
"general.getStarted": "Get Started", "general.getStarted": "Get Started",
"general.gender": "Gender", "general.gender": "Gender",
"general.guidelines": "Community Guidelines", "general.guidelines": "Community Guidelines",
"general.invalidSelection": "Invalid selection",
"general.jobs": "Jobs", "general.jobs": "Jobs",
"general.joinScratch": "Join Scratch", "general.joinScratch": "Join Scratch",
"general.legal": "Legal", "general.legal": "Legal",