-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert AddressForm From Angular to React #969
Open
reldredge71
wants to merge
16
commits into
master
Choose a base branch
from
payment-methods-react
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
db24bf2
remove ts-loader
reldredge71 f72807a
Translate AddressForm Select Components to React
reldredge71 710f548
Create TextInput Component
reldredge71 474b173
Create AddressForm in React
reldredge71 b8750e6
Add Select and Text Inputs to AddressForm
reldredge71 aea7f50
Insert AddressForm into CreditCardForm
reldredge71 0f64054
Install Formik
reldredge71 72d2e73
Update AddressForm Components to Work With Formik
reldredge71 92b865d
For now, replace translation handling with hardcoded text
reldredge71 4945084
Expose address updates to parent view
reldredge71 2fba43c
Introduce Yup
reldredge71 1bf6987
Fix jest.config.js
reldredge71 a07a0e2
Call refreshRegions on selecting country
reldredge71 5cb7503
Make addressDisabled and canRetry optional
reldredge71 aa10dea
Start working on tests
reldredge71 bbabb3c
Adjustments to SelectInput and textInput to be more customizable
reldredge71 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
305 changes: 305 additions & 0 deletions
305
src/common/components/addressForm/addressForm.react.tsx
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,305 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import angular from 'angular'; | ||
import { react2angular } from 'react2angular'; | ||
import { Formik } from 'formik'; | ||
import * as Yup from 'yup'; | ||
import find from 'lodash/find'; | ||
|
||
import CountrySelect from './countrySelect'; | ||
import RegionSelect from './regionSelect'; | ||
import TextInput from '../form/textInput'; | ||
import FormikAutoSave from '../form/formikAutoSave'; | ||
|
||
interface AddressFormProps { | ||
address: Address, | ||
addressDisabled?: boolean, | ||
onAddressChanged: (updatedAddress: Address) => void, | ||
geographiesService: any, | ||
$log: any | ||
} | ||
|
||
export interface Address { | ||
country: string, | ||
locality: string, | ||
region: string, | ||
postalCode: string, | ||
streetAddress: string, | ||
extendedAddress?: string, | ||
intAddressLine3?: string, | ||
intAddressLine4?: string | ||
} | ||
|
||
interface GeographiesLink { | ||
href: string, | ||
rel: string, | ||
type: string, | ||
uri: string, | ||
} | ||
|
||
interface GeographiesItem { | ||
"display-name": string, | ||
links: GeographiesLink[], | ||
name: string, | ||
} | ||
|
||
const componentName = 'reactAddressForm'; | ||
|
||
const AddressForm = ({ | ||
address, | ||
addressDisabled = false, | ||
onAddressChanged, | ||
geographiesService, | ||
$log | ||
}: AddressFormProps) => { | ||
|
||
const [countryName, setCountryName] = useState<string | undefined>(address.country); | ||
const [countries, setCountries] = useState<GeographiesItem[]>([]); | ||
const [regions, setRegions] = useState<GeographiesItem[]>([]); | ||
|
||
const [loadingCountriesError, setLoadingCountriesError] = useState<boolean>(false); | ||
const [loadingRegionsError, setLoadingRegionsError] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
loadCountries(); | ||
}, []); | ||
|
||
const dropdownSortComparator = (a: GeographiesItem, b: GeographiesItem) => { | ||
if(a['display-name'] < b['display-name']) return -1; | ||
if(a['display-name'] > b['display-name']) return 1; | ||
return 0; | ||
}; | ||
|
||
const AddressSchema = Yup.object().shape({ | ||
country: Yup.string() | ||
.required('You must select a country'), | ||
streetAddress: Yup.string() | ||
.max(200, 'This field cannot be longer than 200 characters') | ||
.required('You must enter an address'), | ||
extendedAddress: Yup.string() | ||
.max(100, 'This field cannot be longer than 100 characters'), | ||
intAddressLine3: Yup.string() | ||
.max(100, 'This field cannot be longer than 100 characters'), | ||
intAddressLine4: Yup.string() | ||
.max(100, 'This field cannot be longer than 100 characters'), | ||
locality: Yup.string() | ||
.max(50, 'This field cannot be longer than 100 characters') | ||
.required('You must enter a city'), | ||
region: Yup.string() | ||
.required('You must select a state / region'), | ||
postalCode: Yup.string() | ||
.test( | ||
'is-postal-code', | ||
() => 'You must enter a valid US zip code', | ||
(value) => value == null || /^\d{5}(?:[-\s]\d{4})?$/.test(value) | ||
) | ||
.required('You must enter a zip / postal code') | ||
}); | ||
|
||
const handleAddressChanged = (values: Address) => { | ||
onAddressChanged(values); | ||
}; | ||
|
||
const loadCountries = () => { | ||
setLoadingCountriesError(false); | ||
|
||
geographiesService.getCountries() | ||
.subscribe((data: GeographiesItem[]) => { | ||
const sortedCountries = data.sort(dropdownSortComparator); | ||
|
||
setCountries(sortedCountries); | ||
|
||
const countryContext = countryName && findCountry(sortedCountries, countryName); | ||
countryContext && loadRegions(countryContext); | ||
}, | ||
(error: any) => { | ||
setLoadingCountriesError(true); | ||
$log.error('Error loading countries.', error); | ||
}); | ||
}; | ||
|
||
const loadRegions = (countryContext: GeographiesItem) => { | ||
setLoadingRegionsError(false); | ||
|
||
geographiesService.getRegions(countryContext) | ||
.subscribe((data: GeographiesItem[]) => { | ||
const sortedRegions = data.sort(dropdownSortComparator); | ||
|
||
setRegions(sortedRegions); | ||
}, | ||
(error: any) => { | ||
setLoadingRegionsError(true); | ||
$log.error('Error loading regions.', error); | ||
}); | ||
}; | ||
|
||
const findCountry = (countryOptions: GeographiesItem[], countryName?: string): GeographiesItem | undefined => { | ||
let foundCountry: GeographiesItem | undefined = undefined; | ||
|
||
if (countryOptions.length > 0) { | ||
foundCountry = find(countryOptions, { name: countryName }); | ||
} | ||
|
||
return foundCountry; | ||
}; | ||
|
||
const refreshRegions = () => { | ||
const countryContext = countryName && findCountry(countries, countryName); | ||
countryContext && loadRegions(countryContext); | ||
} | ||
|
||
return ( | ||
<Formik | ||
initialValues={address} | ||
validationSchema={AddressSchema} | ||
onSubmit={handleAddressChanged} | ||
> | ||
{({ | ||
values, | ||
errors, | ||
touched, | ||
handleChange, | ||
handleBlur, | ||
}) => ( | ||
<> | ||
<FormikAutoSave debounceMs={600} /> | ||
<div className="row"> | ||
<div className="col-sm-12"> | ||
<CountrySelect | ||
addressDisabled={addressDisabled} | ||
countries={countries.map(country => ({ name: country.name, displayName: country['display-name']}))} | ||
onChange={handleChange} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that too, I'll push something that should address that. |
||
onBlur={handleBlur} | ||
onSelectCountry={setCountryName} | ||
refreshCountries={loadCountries} | ||
value={values.country} | ||
error={loadingCountriesError | ||
? 'There was an error loading the list of countries. If you continue to see this message, contact <a href="mailto:[email protected]">[email protected]</a> for assistance.' | ||
: touched.country && errors.country | ||
? errors.country | ||
: undefined | ||
} | ||
canRetry={loadingCountriesError} | ||
/> | ||
</div> | ||
</div> | ||
<div className="row"> | ||
<div className="col-sm-12"> | ||
<TextInput | ||
title="Address" | ||
name="streetAddress" | ||
required | ||
maxLength={200} | ||
disabled={addressDisabled} | ||
onChange={handleChange} | ||
onBlur={handleBlur} | ||
value={values.streetAddress} | ||
error={touched.streetAddress && errors.streetAddress || undefined} | ||
/> | ||
</div> | ||
</div> | ||
<div className="row"> | ||
<div className="col-sm-12"> | ||
<TextInput | ||
name="extendedAddress" | ||
maxLength={100} | ||
disabled={addressDisabled} | ||
onChange={handleChange} | ||
onBlur={handleBlur} | ||
value={values.extendedAddress} | ||
error={touched.extendedAddress && errors.extendedAddress || undefined} | ||
/> | ||
</div> | ||
</div> | ||
{ | ||
countryName && countryName !== 'US' | ||
? ( | ||
<> | ||
<div className="row"> | ||
<div className="col-sm-12"> | ||
<TextInput | ||
name="intAddressLine3" | ||
maxLength={100} | ||
disabled={addressDisabled} | ||
onChange={handleChange} | ||
onBlur={handleBlur} | ||
value={values.intAddressLine3} | ||
error={touched.intAddressLine3 && errors.intAddressLine3 || undefined} | ||
/> | ||
</div> | ||
</div> | ||
<div className="row"> | ||
<div className="col-sm-12"> | ||
<TextInput | ||
name="intAddressLine4" | ||
maxLength={100} | ||
disabled={addressDisabled} | ||
onChange={handleChange} | ||
onBlur={handleBlur} | ||
value={values.intAddressLine4} | ||
error={touched.intAddressLine4 && errors.intAddressLine4 || undefined} | ||
/> | ||
</div> | ||
</div> | ||
</> | ||
) : ( | ||
<> | ||
<div className="row"> | ||
<div className="col-sm-12"> | ||
<TextInput | ||
title="City" | ||
name="locality" | ||
required | ||
maxLength={50} | ||
disabled={addressDisabled} | ||
onChange={handleChange} | ||
onBlur={handleBlur} | ||
value={values.locality} | ||
error={touched.locality && errors.locality || undefined} | ||
/> | ||
</div> | ||
</div> | ||
<div className="row"> | ||
<div className="col-sm-6"> | ||
<RegionSelect | ||
addressDisabled={addressDisabled} | ||
regions={regions.map(region => ({ name: region.name, displayName: region['display-name']}))} | ||
onChange={handleChange} | ||
onBlur={handleBlur} | ||
refreshRegions={refreshRegions} | ||
value={values.region} | ||
error={loadingRegionsError | ||
? 'There was an error loading the list of regions/state. If you continue to see this message, contact <a href="mailto:[email protected]">[email protected]</a> for assistance.' | ||
: touched.region && errors.region | ||
? errors.region | ||
: undefined | ||
} | ||
canRetry={loadingRegionsError} | ||
/> | ||
</div> | ||
<div className="col-sm-6"> | ||
<TextInput | ||
title="Zip / Postal Code" | ||
name="postalCode" | ||
required | ||
disabled={addressDisabled} | ||
onChange={handleChange} | ||
onBlur={handleBlur} | ||
value={values.postalCode} | ||
error={touched.postalCode && errors.postalCode || undefined} | ||
/> | ||
</div> | ||
</div> | ||
</> | ||
) | ||
} | ||
</> | ||
)} | ||
</Formik> | ||
); | ||
}; | ||
|
||
export default angular | ||
.module(componentName, []) | ||
.component(componentName, react2angular(AddressForm, ['address', 'addressDisabled', 'onAddressChanged'], ['geographiesService', '$log'])) | ||
|
||
export { AddressForm } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this use the
maxLength
property instead of hard-coded200
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the
maxLength
property on the input itself is accessible in this scope.What I've also noticed is that setting
maxLength
on the input field prevents the user from entering any more characters than that limit. So I don't think this error will even show up at all. I think the input on its own does a good job of preventing the max length from being exceeded.