generated from openedx/frontend-template-application
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: registration component refactoring
- Loading branch information
1 parent
c5caaeb
commit 8d6473f
Showing
30 changed files
with
1,096 additions
and
1,049 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/register/RegistrationFields/CountryField/CountryField.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import React, { useEffect } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
|
||
import { useIntl } from '@edx/frontend-platform/i18n'; | ||
import { FormAutosuggest, FormAutosuggestOption, FormControlFeedback } from '@edx/paragon'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { COUNTRY_CODE_KEY, COUNTRY_DISPLAY_KEY, COUNTRY_FIELD_LABEL } from './constants'; | ||
import validateCountryField from './validator'; | ||
import { clearRegistertionBackendError } from '../../data/actions'; | ||
import messages from '../../messages'; | ||
|
||
const CountryField = (props) => { | ||
const { | ||
countryList, | ||
selectedCountry, | ||
onChangeHandler, | ||
handleErrorChange, | ||
onFocusHandler, | ||
} = props; | ||
const { formatMessage } = useIntl(); | ||
const dispatch = useDispatch(); | ||
const backendCountryCode = useSelector(state => state.register.backendCountryCode); | ||
|
||
useEffect(() => { | ||
if (backendCountryCode && backendCountryCode !== selectedCountry?.countryCode) { | ||
let countryCode = ''; | ||
let countryDisplayValue = ''; | ||
|
||
const countryVal = countryList.find( | ||
(country) => (country[COUNTRY_CODE_KEY].toLowerCase() === backendCountryCode.toLowerCase()), | ||
); | ||
if (countryVal) { | ||
countryCode = countryVal[COUNTRY_CODE_KEY]; | ||
countryDisplayValue = countryVal[COUNTRY_DISPLAY_KEY]; | ||
} | ||
onChangeHandler( | ||
{ target: { name: COUNTRY_FIELD_LABEL } }, | ||
{ countryCode, displayValue: countryDisplayValue }, | ||
); | ||
} | ||
}, [backendCountryCode, countryList]); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
const handleOnBlur = (event) => { | ||
// Do not run validations when drop-down arrow is clicked | ||
if (event.relatedTarget && event.relatedTarget.className.includes('pgn__form-autosuggest__icon-button')) { | ||
return; | ||
} | ||
|
||
const { value } = event.target; | ||
|
||
const { countryCode, displayValue, error } = validateCountryField( | ||
value.trim(), countryList, formatMessage(messages['empty.country.field.error']), | ||
); | ||
|
||
onChangeHandler({ target: { name: COUNTRY_FIELD_LABEL } }, { countryCode, displayValue }); | ||
handleErrorChange(COUNTRY_FIELD_LABEL, error); | ||
// onBlurHandler(event); | ||
}; | ||
|
||
const handleSelected = (value) => { | ||
handleOnBlur({ target: { name: COUNTRY_FIELD_LABEL, value } }); | ||
}; | ||
|
||
const handleOnFocus = (event) => { | ||
handleErrorChange(COUNTRY_FIELD_LABEL, ''); | ||
dispatch(clearRegistertionBackendError(COUNTRY_FIELD_LABEL)); | ||
onFocusHandler(event); | ||
}; | ||
|
||
const handleOnChange = (value) => { | ||
onChangeHandler({ target: { name: COUNTRY_FIELD_LABEL } }, { countryCode: '', displayValue: value }); | ||
}; | ||
|
||
const getCountryList = () => countryList.map((country) => ( | ||
<FormAutosuggestOption key={country[COUNTRY_CODE_KEY]}> | ||
{country[COUNTRY_DISPLAY_KEY]} | ||
</FormAutosuggestOption> | ||
)); | ||
|
||
return ( | ||
<div className="mb-4"> | ||
<FormAutosuggest | ||
floatingLabel={formatMessage(messages['registration.country.label'])} | ||
aria-label="form autosuggest" | ||
name="country" | ||
value={selectedCountry.displayValue || ''} | ||
onSelected={(value) => handleSelected(value)} | ||
onFocus={(e) => handleOnFocus(e)} | ||
onBlur={(e) => handleOnBlur(e)} | ||
onChange={(value) => handleOnChange(value)} | ||
> | ||
{getCountryList()} | ||
</FormAutosuggest> | ||
{props.errorMessage !== '' && ( | ||
<FormControlFeedback | ||
key="error" | ||
className="form-text-size" | ||
hasIcon={false} | ||
feedback-for="country" | ||
type="invalid" | ||
> | ||
{props.errorMessage} | ||
</FormControlFeedback> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
CountryField.propTypes = { | ||
countryList: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
code: PropTypes.string, | ||
name: PropTypes.string, | ||
}), | ||
).isRequired, | ||
errorMessage: PropTypes.string, | ||
onChangeHandler: PropTypes.func.isRequired, | ||
handleErrorChange: PropTypes.func.isRequired, | ||
onFocusHandler: PropTypes.func.isRequired, | ||
selectedCountry: PropTypes.shape({ | ||
displayValue: PropTypes.string, | ||
countryCode: PropTypes.string, | ||
}), | ||
}; | ||
|
||
CountryField.defaultProps = { | ||
errorMessage: null, | ||
selectedCountry: { | ||
value: '', | ||
}, | ||
}; | ||
|
||
export default CountryField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const COUNTRY_FIELD_LABEL = 'country'; | ||
export const COUNTRY_CODE_KEY = 'code'; | ||
export const COUNTRY_DISPLAY_KEY = 'name'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { COUNTRY_CODE_KEY, COUNTRY_DISPLAY_KEY } from './constants'; | ||
|
||
const validateCountryField = (value, countryList, errorMessage) => { | ||
let countryCode = ''; | ||
let displayValue = value; | ||
let error = errorMessage; | ||
|
||
if (value) { | ||
const normalizedValue = value.toLowerCase(); | ||
// Handling a case here where user enters a valid country code that needs to be | ||
// evaluated and set its value as a valid value. | ||
const selectedCountry = countryList.find( | ||
(country) => ( | ||
// When translations are applied, extra space added in country value, so we should trim that. | ||
country[COUNTRY_DISPLAY_KEY].toLowerCase().trim() === normalizedValue | ||
|| country[COUNTRY_CODE_KEY].toLowerCase().trim() === normalizedValue | ||
), | ||
); | ||
if (selectedCountry) { | ||
countryCode = selectedCountry[COUNTRY_CODE_KEY]; | ||
displayValue = selectedCountry[COUNTRY_DISPLAY_KEY]; | ||
error = ''; | ||
} | ||
} | ||
return { error, countryCode, displayValue }; | ||
}; | ||
|
||
export default validateCountryField; |
Oops, something went wrong.