Change validation of store information data
This commit is contained in:
parent
0374a0218b
commit
4b11d29de1
3 changed files with 42 additions and 25 deletions
|
@ -4,8 +4,8 @@ export interface AddressTypeInput {
|
||||||
companyName?: string;
|
companyName?: string;
|
||||||
country: string;
|
country: string;
|
||||||
countryArea?: string;
|
countryArea?: string;
|
||||||
firstName: string;
|
firstName?: string;
|
||||||
lastName: string;
|
lastName?: string;
|
||||||
phone: string;
|
phone: string;
|
||||||
postalCode: string;
|
postalCode: string;
|
||||||
streetAddress1: string;
|
streetAddress1: string;
|
||||||
|
|
|
@ -11,6 +11,7 @@ import Grid from "@saleor/components/Grid";
|
||||||
import Hr from "@saleor/components/Hr";
|
import Hr from "@saleor/components/Hr";
|
||||||
import PageHeader from "@saleor/components/PageHeader";
|
import PageHeader from "@saleor/components/PageHeader";
|
||||||
import SaveButtonBar from "@saleor/components/SaveButtonBar";
|
import SaveButtonBar from "@saleor/components/SaveButtonBar";
|
||||||
|
import useAddressValidation from "@saleor/hooks/useAddressValidation";
|
||||||
import useStateFromProps from "@saleor/hooks/useStateFromProps";
|
import useStateFromProps from "@saleor/hooks/useStateFromProps";
|
||||||
import { commonMessages, sectionNames } from "@saleor/intl";
|
import { commonMessages, sectionNames } from "@saleor/intl";
|
||||||
import { UserError } from "@saleor/types";
|
import { UserError } from "@saleor/types";
|
||||||
|
@ -56,6 +57,22 @@ export interface SiteSettingsPageProps {
|
||||||
onSubmit: (data: SiteSettingsPageFormData) => void;
|
onSubmit: (data: SiteSettingsPageFormData) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function areAddressInputFieldsModified(
|
||||||
|
data: SiteSettingsPageAddressFormData
|
||||||
|
): boolean {
|
||||||
|
return ([
|
||||||
|
"city",
|
||||||
|
"country",
|
||||||
|
"countryArea",
|
||||||
|
"phone",
|
||||||
|
"postalCode",
|
||||||
|
"streetAddress1",
|
||||||
|
"streetAddress2"
|
||||||
|
] as Array<keyof SiteSettingsPageAddressFormData>)
|
||||||
|
.map(key => data[key])
|
||||||
|
.some(field => field !== "");
|
||||||
|
}
|
||||||
|
|
||||||
const useStyles = makeStyles(
|
const useStyles = makeStyles(
|
||||||
theme => ({
|
theme => ({
|
||||||
hr: {
|
hr: {
|
||||||
|
@ -85,6 +102,11 @@ const SiteSettingsPage: React.FC<SiteSettingsPageProps> = props => {
|
||||||
maybe(() => shop.companyAddress.country.code, "")
|
maybe(() => shop.companyAddress.country.code, "")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const {
|
||||||
|
errors: validationErrors,
|
||||||
|
submit: handleSubmitWithAddress
|
||||||
|
} = useAddressValidation<SiteSettingsPageFormData>(onSubmit);
|
||||||
|
|
||||||
const initialFormAddress: SiteSettingsPageAddressFormData = {
|
const initialFormAddress: SiteSettingsPageAddressFormData = {
|
||||||
city: maybe(() => shop.companyAddress.city, ""),
|
city: maybe(() => shop.companyAddress.city, ""),
|
||||||
companyName: maybe(() => shop.companyAddress.companyName, ""),
|
companyName: maybe(() => shop.companyAddress.companyName, ""),
|
||||||
|
@ -107,12 +129,18 @@ const SiteSettingsPage: React.FC<SiteSettingsPageProps> = props => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form
|
<Form
|
||||||
errors={errors}
|
errors={[...errors, ...validationErrors]}
|
||||||
initial={initialForm}
|
initial={initialForm}
|
||||||
onSubmit={onSubmit}
|
onSubmit={data => {
|
||||||
|
const submitFunc = areAddressInputFieldsModified(data)
|
||||||
|
? handleSubmitWithAddress
|
||||||
|
: onSubmit;
|
||||||
|
submitFunc(data);
|
||||||
|
}}
|
||||||
confirmLeave
|
confirmLeave
|
||||||
>
|
>
|
||||||
{({ change, data, errors: formErrors, hasChanged, submit }) => {
|
{({ change, data, errors: formErrors, hasChanged, submit }) => {
|
||||||
|
const siteFormErrors = { ...formErrors };
|
||||||
const countryChoices = mapCountriesToChoices(
|
const countryChoices = mapCountriesToChoices(
|
||||||
maybe(() => shop.countries, [])
|
maybe(() => shop.countries, [])
|
||||||
);
|
);
|
||||||
|
@ -141,7 +169,7 @@ const SiteSettingsPage: React.FC<SiteSettingsPageProps> = props => {
|
||||||
</div>
|
</div>
|
||||||
<SiteSettingsDetails
|
<SiteSettingsDetails
|
||||||
data={data}
|
data={data}
|
||||||
errors={formErrors}
|
errors={siteFormErrors}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
onChange={change}
|
onChange={change}
|
||||||
/>
|
/>
|
||||||
|
@ -159,7 +187,7 @@ const SiteSettingsPage: React.FC<SiteSettingsPageProps> = props => {
|
||||||
</div>
|
</div>
|
||||||
<SiteSettingsMailing
|
<SiteSettingsMailing
|
||||||
data={data}
|
data={data}
|
||||||
errors={formErrors}
|
errors={siteFormErrors}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
onChange={change}
|
onChange={change}
|
||||||
/>
|
/>
|
||||||
|
@ -180,7 +208,7 @@ const SiteSettingsPage: React.FC<SiteSettingsPageProps> = props => {
|
||||||
data={data}
|
data={data}
|
||||||
displayCountry={displayCountry}
|
displayCountry={displayCountry}
|
||||||
countries={countryChoices}
|
countries={countryChoices}
|
||||||
errors={formErrors}
|
errors={siteFormErrors}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
onChange={change}
|
onChange={change}
|
||||||
onCountryChange={handleCountryChange}
|
onCountryChange={handleCountryChange}
|
||||||
|
|
|
@ -13,7 +13,7 @@ import SiteSettingsKeyDialog, {
|
||||||
SiteSettingsKeyDialogForm
|
SiteSettingsKeyDialogForm
|
||||||
} from "../components/SiteSettingsKeyDialog";
|
} from "../components/SiteSettingsKeyDialog";
|
||||||
import SiteSettingsPage, {
|
import SiteSettingsPage, {
|
||||||
SiteSettingsPageAddressFormData,
|
areAddressInputFieldsModified,
|
||||||
SiteSettingsPageFormData
|
SiteSettingsPageFormData
|
||||||
} from "../components/SiteSettingsPage";
|
} from "../components/SiteSettingsPage";
|
||||||
import {
|
import {
|
||||||
|
@ -76,6 +76,7 @@ export const SiteSettings: React.FC<SiteSettingsProps> = ({ params }) => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TypedSiteSettingsQuery displayLoader>
|
<TypedSiteSettingsQuery displayLoader>
|
||||||
{siteSettings => (
|
{siteSettings => (
|
||||||
|
@ -124,32 +125,20 @@ export const SiteSettings: React.FC<SiteSettingsProps> = ({ params }) => {
|
||||||
const handleUpdateShopSettings = (
|
const handleUpdateShopSettings = (
|
||||||
data: SiteSettingsPageFormData
|
data: SiteSettingsPageFormData
|
||||||
) => {
|
) => {
|
||||||
const areAddressInputFieldsModified = ([
|
const addressInput = areAddressInputFieldsModified(data)
|
||||||
"city",
|
|
||||||
"companyName",
|
|
||||||
"country",
|
|
||||||
"countryArea",
|
|
||||||
"phone",
|
|
||||||
"postalCode",
|
|
||||||
"streetAddress1",
|
|
||||||
"streetAddress2"
|
|
||||||
] as Array<keyof SiteSettingsPageAddressFormData>)
|
|
||||||
.map(key => data[key])
|
|
||||||
.some(field => field !== "");
|
|
||||||
const addressInput = areAddressInputFieldsModified
|
|
||||||
? {
|
? {
|
||||||
city: data.city,
|
city: data.city,
|
||||||
companyName: data.companyName,
|
companyName: data.companyName,
|
||||||
country: maybe(() =>
|
country: findInEnum(data.country, CountryCode),
|
||||||
findInEnum(data.country, CountryCode)
|
|
||||||
),
|
|
||||||
countryArea: data.countryArea,
|
countryArea: data.countryArea,
|
||||||
phone: data.phone,
|
phone: data.phone,
|
||||||
postalCode: data.postalCode,
|
postalCode: data.postalCode,
|
||||||
streetAddress1: data.streetAddress1,
|
streetAddress1: data.streetAddress1,
|
||||||
streetAddress2: data.streetAddress2
|
streetAddress2: data.streetAddress2
|
||||||
}
|
}
|
||||||
: null;
|
: {
|
||||||
|
companyName: data.companyName
|
||||||
|
};
|
||||||
updateShopSettings({
|
updateShopSettings({
|
||||||
variables: {
|
variables: {
|
||||||
addressInput,
|
addressInput,
|
||||||
|
|
Loading…
Reference in a new issue