Skip to content
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

Pwa 3139 v2 #4124

Merged
merged 10 commits into from
Sep 29, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,13 @@ export const useBillingAddress = props => {
* shipping address.
*/
const setShippingAddressAsBillingAddress = useCallback(() => {
const shippingAddress = shippingAddressData
var shippingAddress = shippingAddressData
? mapAddressData(shippingAddressData.cart.shippingAddresses[0])
: {};

shippingAddress.region =
shippingAddress.region == null ? '' : shippingAddress.region;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use null coalescing

updateBillingAddress({
variables: {
cartId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,7 @@ describe('missing data', () => {
Object {
"variables": Object {
"cartId": "123",
"region": "",
"sameAsShipping": true,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,13 @@ export const useCreditCard = props => {
* shipping address.
*/
const setShippingAddressAsBillingAddress = useCallback(() => {
const shippingAddress = shippingAddressData
var shippingAddress = shippingAddressData
? mapAddressData(shippingAddressData.cart.shippingAddresses[0])
: {};

shippingAddress.region =
shippingAddress.region == null ? '' : shippingAddress.region;

updateBillingAddress({
variables: {
cartId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ Object {
"firstname": "Philip",
"lastname": "Fry",
"postcode": "10019",
"region": 12,
"region": Object {
"region": "New York",
"region_code": "NY",
"region_id": 12,
},
"street": Array [
"3000 57th Street",
"Suite 200",
Expand All @@ -33,7 +37,11 @@ Object {
"firstname": "Philip",
"lastname": "Fry",
"postcode": "10019",
"region": 12,
"region": Object {
"region": "New York",
"region_code": "NY",
"region_id": 12,
},
"street": Array [
"3000 57th Street",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const useGuestForm = props => {

const handleSubmit = useCallback(
async formValues => {
const { country, email, region, ...address } = formValues;
const { country, email, ...address } = formValues;
try {
await setGuestShipping({
variables: {
Expand All @@ -70,8 +70,6 @@ export const useGuestForm = props => {
...address,
// Cleans up the street array when values are null or undefined
street: address.street.filter(e => e),
// region_id is used for field select and region is used for field input
region: region.region_id || region.region,
country_code: country
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ Array [
fieldInput="region[region]"
fieldSelect="region[region_id]"
optionValueKey="id"
validate={[Function]}
/>

</div>
<div
className="postcode"
Expand Down Expand Up @@ -827,8 +827,8 @@ Array [
fieldInput="region[region]"
fieldSelect="region[region_id]"
optionValueKey="id"
validate={[Function]}
/>

</div>
<div
className="postcode"
Expand Down Expand Up @@ -1364,8 +1364,8 @@ Array [
fieldInput="region[region]"
fieldSelect="region[region_id]"
optionValueKey="id"
validate={[Function]}
/>

</div>
<div
className="postcode"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ Array [
fieldInput="region[region]"
fieldSelect="region[region_id]"
optionValueKey="id"
validate={[Function]}
/>

</div>
<div
className="postcode"
Expand Down Expand Up @@ -800,8 +800,8 @@ Array [
fieldInput="region[region]"
fieldSelect="region[region_id]"
optionValueKey="id"
validate={[Function]}
/>

</div>
<div
className="postcode"
Expand Down Expand Up @@ -1255,8 +1255,8 @@ Array [
fieldInput="region[region]"
fieldSelect="region[region_id]"
optionValueKey="id"
validate={[Function]}
/>

</div>
<div
className="postcode"
Expand Down Expand Up @@ -1736,8 +1736,8 @@ Array [
fieldInput="region[region]"
fieldSelect="region[region_id]"
optionValueKey="id"
validate={[Function]}
/>

</div>
<div
className="postcode"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ const CustomerForm = props => {
<Text type="hidden" field="default_shipping" initialValue={true} />
);

const createErrorMessage = JSON.stringify(
errors.get('createCustomerAddressMutation')
);
const updateErrorMessage = JSON.stringify(
errors.get('updateCustomerAddressMutation')
);
const errorMessage = 'region_id is required for the specified country code';
const regionError =
createErrorMessage?.includes(errorMessage) ||
updateErrorMessage?.includes(errorMessage);

// errors
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

irrelevant information

return (
<Fragment>
<FormError errors={Array.from(errors.values())} />
Expand Down Expand Up @@ -257,9 +269,10 @@ const CustomerForm = props => {
/>
</Field>
</div>

<div className={classes.region}>
<Region
validate={isRequired}
regionError={regionError}
data-cy="CustomerForm-region"
fieldInput={'region[region]'}
fieldSelect={'region[region_id]'}
Expand All @@ -269,7 +282,20 @@ const CustomerForm = props => {
defaultMessage: 'State Required'
})}
/>
{regionError ? (
<Message>
<div className={classes.regionError}>
<FormattedMessage
id={'validation.isRequired'}
defaultMessage={'isRequired'}
/>
</div>
</Message>
) : (
''
)}
</div>

<div className={classes.postcode}>
<Postcode
validate={isRequired}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
composes: p-xs from global;
}

.regionError {
color: brown;
font-weight: 600;
}

@media (max-width: 959px) {
.firstname {
grid-column: 1 / span 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ const GuestForm = props => {
}
}, [addToast, formatMessage, showSignInToast, handleToastAction]);

const shippingAddressError = JSON.stringify(
errors.get('setGuestShippingMutation')
);

const errorMessage = 'Region is required';
const regionError = shippingAddressError?.includes(errorMessage);

return (
<Fragment>
<FormError errors={Array.from(errors.values())} />
Expand Down Expand Up @@ -304,11 +311,11 @@ const GuestForm = props => {
</div>
<div className={classes.region}>
<Region
regionError={regionError}
autoComplete={formatMessage({
id: 'region.label',
defaultMessage: 'State'
})}
validate={isRequired}
fieldInput={'region[region]'}
fieldSelect={'region[region_id]'}
optionValueKey={'id'}
Expand All @@ -318,6 +325,18 @@ const GuestForm = props => {
defaultMessage: 'State Required'
})}
/>
{regionError ? (
<Message>
<div className={classes.regionError}>
<FormattedMessage
id={'validation.isRequired'}
defaultMessage={'isRequired'}
/>
</div>
</Message>
) : (
''
)}
</div>
<div className={classes.postcode}>
<Postcode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
composes: p-xs from global;
}

.regionError {
color: brown;
font-weight: 600;
}

@media (max-width: 959px) {
.firstname {
grid-column: 1 / span 2;
Expand Down
9 changes: 8 additions & 1 deletion packages/venia-ui/lib/components/Region/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { GET_REGIONS_QUERY } from './region.gql';
const Region = props => {
const {
classes: propClasses,
regionError,
countryCodeField,
fieldInput,
fieldSelect,
Expand Down Expand Up @@ -47,13 +48,19 @@ const Region = props => {
const regionField =
regions.length || loading ? (
<Select
regionError={regionError}
{...regionProps}
field={fieldSelect}
id={classes.root}
items={regions}
/>
) : (
<TextInput {...regionProps} field={fieldInput} id={classes.root} />
<TextInput
{...regionProps}
field={fieldInput}
id={classes.root}
regionError={regionError}
/>
);

return (
Expand Down
4 changes: 3 additions & 1 deletion packages/venia-ui/lib/components/Select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ const Select = props => {
const {
before,
classes: propClasses,
regionError,
field,
items,
message,
...rest
} = props;
const fieldState = useFieldState(field);
const classes = useStyle(defaultClasses, propClasses);
const inputClass = fieldState.error ? classes.input_error : classes.input;
const inputClass =
fieldState.error || regionError ? classes.input_error : classes.input;

const options = items.map(
({ disabled = null, hidden = null, label, value, key = value }) => (
Expand Down
4 changes: 3 additions & 1 deletion packages/venia-ui/lib/components/TextInput/textInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ const TextInput = props => {
after,
before,
classes: propClasses,
regionError,
field,
message,
...rest
} = props;
const fieldState = useFieldState(field);
const classes = useStyle(defaultClasses, propClasses);
const inputClass = fieldState.error ? classes.input_error : classes.input;
var inputClass =
fieldState.error || regionError ? classes.input_error : classes.input;

return (
<Fragment>
Expand Down