Skip to content

Commit

Permalink
PWA-3139::Region is required in PWA although this option is disabled …
Browse files Browse the repository at this point in the history
…in Magento , and is not required on normal storefront
  • Loading branch information
glo80771 committed May 16, 2023
1 parent 99925fa commit e3a97e2
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 12 deletions.
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
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,17 @@ const GuestForm = props => {
}
}, [addToast, formatMessage, showSignInToast, handleToastAction]);

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);

return (
<Fragment>
<FormError errors={Array.from(errors.values())} />
Expand Down Expand Up @@ -308,7 +319,6 @@ const GuestForm = props => {
id: 'region.label',
defaultMessage: 'State'
})}
validate={isRequired}
fieldInput={'region[region]'}
fieldSelect={'region[region_id]'}
optionValueKey={'id'}
Expand All @@ -318,6 +328,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

0 comments on commit e3a97e2

Please sign in to comment.