2022-12-21 08:54:45 +00:00
|
|
|
import { createCountryHandler } from "@saleor/components/AddressEdit/createCountryHandler";
|
2022-05-06 08:59:55 +00:00
|
|
|
import { Backlink } from "@saleor/components/Backlink";
|
2020-05-14 09:30:32 +00:00
|
|
|
import CompanyAddressInput from "@saleor/components/CompanyAddressInput";
|
2019-06-19 14:40:52 +00:00
|
|
|
import Container from "@saleor/components/Container";
|
|
|
|
import Form from "@saleor/components/Form";
|
|
|
|
import Grid from "@saleor/components/Grid";
|
2019-10-21 10:13:23 +00:00
|
|
|
import Hr from "@saleor/components/Hr";
|
2019-06-19 14:40:52 +00:00
|
|
|
import PageHeader from "@saleor/components/PageHeader";
|
2021-10-13 11:35:00 +00:00
|
|
|
import PageSectionHeader from "@saleor/components/PageSectionHeader";
|
2021-07-21 08:59:52 +00:00
|
|
|
import Savebar from "@saleor/components/Savebar";
|
2022-05-06 08:59:55 +00:00
|
|
|
import { configurationMenuUrl } from "@saleor/configuration";
|
2022-03-09 08:56:55 +00:00
|
|
|
import { ShopErrorFragment, SiteSettingsQuery } from "@saleor/graphql";
|
2019-12-06 13:56:16 +00:00
|
|
|
import useAddressValidation from "@saleor/hooks/useAddressValidation";
|
2020-11-02 13:26:02 +00:00
|
|
|
import { SubmitPromise } from "@saleor/hooks/useForm";
|
2022-05-06 08:59:55 +00:00
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
2019-08-09 11:14:35 +00:00
|
|
|
import useStateFromProps from "@saleor/hooks/useStateFromProps";
|
2019-08-26 21:39:21 +00:00
|
|
|
import { commonMessages, sectionNames } from "@saleor/intl";
|
2022-05-06 08:59:55 +00:00
|
|
|
import { ConfirmButtonTransitionState, makeStyles } from "@saleor/macaw-ui";
|
2019-08-09 11:14:35 +00:00
|
|
|
import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
|
|
|
|
import { mapCountriesToChoices } from "@saleor/utils/maps";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
2021-10-13 11:35:00 +00:00
|
|
|
import { useIntl } from "react-intl";
|
2020-05-14 09:30:32 +00:00
|
|
|
|
2021-10-13 11:35:00 +00:00
|
|
|
import SiteCheckoutSettingsCard from "../SiteCheckoutSettingsCard";
|
|
|
|
import { messages } from "./messages";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-08-09 11:14:35 +00:00
|
|
|
export interface SiteSettingsPageAddressFormData {
|
|
|
|
city: string;
|
|
|
|
companyName: string;
|
|
|
|
country: string;
|
|
|
|
countryArea: string;
|
|
|
|
phone: string;
|
|
|
|
postalCode: string;
|
|
|
|
streetAddress1: string;
|
|
|
|
streetAddress2: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface SiteSettingsPageFormData
|
2021-03-29 09:24:47 +00:00
|
|
|
extends SiteSettingsPageAddressFormData {
|
2019-06-19 14:40:52 +00:00
|
|
|
description: string;
|
2021-10-13 11:35:00 +00:00
|
|
|
reserveStockDurationAnonymousUser: number;
|
|
|
|
reserveStockDurationAuthenticatedUser: number;
|
2021-12-07 09:43:24 +00:00
|
|
|
limitQuantityPerCheckout: number;
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface SiteSettingsPageProps {
|
|
|
|
disabled: boolean;
|
2020-03-17 18:49:01 +00:00
|
|
|
errors: ShopErrorFragment[];
|
2022-12-14 17:11:24 +00:00
|
|
|
shop?: SiteSettingsQuery["shop"];
|
2019-06-19 14:40:52 +00:00
|
|
|
saveButtonBarState: ConfirmButtonTransitionState;
|
2020-11-02 13:26:02 +00:00
|
|
|
onSubmit: (data: SiteSettingsPageFormData) => SubmitPromise;
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 13:56:16 +00:00
|
|
|
export function areAddressInputFieldsModified(
|
2022-06-21 09:36:55 +00:00
|
|
|
data: SiteSettingsPageAddressFormData,
|
2019-12-06 13:56:16 +00:00
|
|
|
): boolean {
|
|
|
|
return ([
|
|
|
|
"city",
|
|
|
|
"country",
|
|
|
|
"countryArea",
|
|
|
|
"phone",
|
|
|
|
"postalCode",
|
|
|
|
"streetAddress1",
|
2022-06-21 09:36:55 +00:00
|
|
|
"streetAddress2",
|
2019-12-06 13:56:16 +00:00
|
|
|
] as Array<keyof SiteSettingsPageAddressFormData>)
|
|
|
|
.map(key => data[key])
|
|
|
|
.some(field => field !== "");
|
|
|
|
}
|
|
|
|
|
2019-10-21 10:13:23 +00:00
|
|
|
const useStyles = makeStyles(
|
2019-10-28 16:16:49 +00:00
|
|
|
theme => ({
|
2019-10-21 10:13:23 +00:00
|
|
|
hr: {
|
|
|
|
gridColumnEnd: "span 2",
|
2022-06-21 09:36:55 +00:00
|
|
|
margin: theme.spacing(1, 0),
|
|
|
|
},
|
2019-10-21 10:13:23 +00:00
|
|
|
}),
|
|
|
|
{
|
2022-06-21 09:36:55 +00:00
|
|
|
name: "SiteSettingsPage",
|
|
|
|
},
|
2019-10-21 10:13:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const SiteSettingsPage: React.FC<SiteSettingsPageProps> = props => {
|
2022-05-06 08:59:55 +00:00
|
|
|
const { disabled, errors, saveButtonBarState, shop, onSubmit } = props;
|
|
|
|
|
2019-10-21 10:13:23 +00:00
|
|
|
const classes = useStyles(props);
|
2019-08-26 21:39:21 +00:00
|
|
|
const intl = useIntl();
|
2022-05-06 08:59:55 +00:00
|
|
|
const navigate = useNavigator();
|
|
|
|
|
2019-08-09 11:14:35 +00:00
|
|
|
const [displayCountry, setDisplayCountry] = useStateFromProps(
|
2022-12-23 12:17:41 +00:00
|
|
|
shop?.companyAddress?.country.country || "",
|
2019-08-09 11:14:35 +00:00
|
|
|
);
|
|
|
|
|
2019-12-06 13:56:16 +00:00
|
|
|
const {
|
|
|
|
errors: validationErrors,
|
2022-06-21 09:36:55 +00:00
|
|
|
submit: handleSubmitWithAddress,
|
2020-10-22 11:33:29 +00:00
|
|
|
} = useAddressValidation(onSubmit);
|
2019-12-06 13:56:16 +00:00
|
|
|
|
2019-10-21 14:14:28 +00:00
|
|
|
const initialFormAddress: SiteSettingsPageAddressFormData = {
|
2021-09-13 14:14:04 +00:00
|
|
|
city: shop?.companyAddress?.city || "",
|
|
|
|
companyName: shop?.companyAddress?.companyName || "",
|
|
|
|
country: shop?.companyAddress?.country.code || "",
|
|
|
|
countryArea: shop?.companyAddress?.countryArea || "",
|
|
|
|
phone: shop?.companyAddress?.phone || "",
|
|
|
|
postalCode: shop?.companyAddress?.postalCode || "",
|
|
|
|
streetAddress1: shop?.companyAddress?.streetAddress1 || "",
|
2022-06-21 09:36:55 +00:00
|
|
|
streetAddress2: shop?.companyAddress?.streetAddress2 || "",
|
2019-06-19 14:40:52 +00:00
|
|
|
};
|
2019-10-21 14:14:28 +00:00
|
|
|
const initialForm: SiteSettingsPageFormData = {
|
|
|
|
...initialFormAddress,
|
2021-09-13 14:14:04 +00:00
|
|
|
description: shop?.description || "",
|
2022-12-14 17:11:24 +00:00
|
|
|
reserveStockDurationAnonymousUser:
|
|
|
|
shop?.reserveStockDurationAnonymousUser ?? 0,
|
2021-10-13 11:35:00 +00:00
|
|
|
reserveStockDurationAuthenticatedUser:
|
2022-12-14 17:11:24 +00:00
|
|
|
shop?.reserveStockDurationAuthenticatedUser ?? 0,
|
|
|
|
limitQuantityPerCheckout: shop?.limitQuantityPerCheckout ?? 0,
|
2019-10-21 14:14:28 +00:00
|
|
|
};
|
2019-08-09 11:14:35 +00:00
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
return (
|
|
|
|
<Form
|
|
|
|
initial={initialForm}
|
2019-12-06 13:56:16 +00:00
|
|
|
onSubmit={data => {
|
|
|
|
const submitFunc = areAddressInputFieldsModified(data)
|
|
|
|
? handleSubmitWithAddress
|
|
|
|
: onSubmit;
|
2020-10-22 11:33:29 +00:00
|
|
|
return submitFunc(data);
|
2019-12-06 13:56:16 +00:00
|
|
|
}}
|
2019-06-19 14:40:52 +00:00
|
|
|
confirmLeave
|
2022-03-23 09:13:23 +00:00
|
|
|
disabled={disabled}
|
2019-06-19 14:40:52 +00:00
|
|
|
>
|
2022-12-21 08:54:45 +00:00
|
|
|
{({ change, data, set, isSaveDisabled, submit }) => {
|
2020-03-17 18:49:01 +00:00
|
|
|
const countryChoices = mapCountriesToChoices(shop?.countries || []);
|
2022-12-21 08:54:45 +00:00
|
|
|
const countrySelect = createSingleAutocompleteSelectHandler(
|
2019-08-09 11:14:35 +00:00
|
|
|
change,
|
|
|
|
setDisplayCountry,
|
2022-06-21 09:36:55 +00:00
|
|
|
countryChoices,
|
2019-08-09 11:14:35 +00:00
|
|
|
);
|
|
|
|
|
2022-12-21 08:54:45 +00:00
|
|
|
const handleCountrySelect = createCountryHandler(countrySelect, set);
|
|
|
|
|
2019-08-09 11:14:35 +00:00
|
|
|
return (
|
|
|
|
<Container>
|
2022-05-06 08:59:55 +00:00
|
|
|
<Backlink href={configurationMenuUrl}>
|
2019-08-26 21:39:21 +00:00
|
|
|
{intl.formatMessage(sectionNames.configuration)}
|
2021-07-21 08:59:52 +00:00
|
|
|
</Backlink>
|
2019-08-09 11:14:35 +00:00
|
|
|
<PageHeader
|
2019-08-26 21:39:21 +00:00
|
|
|
title={intl.formatMessage(commonMessages.generalInformations)}
|
2021-09-14 13:57:02 +00:00
|
|
|
underline={true}
|
2019-06-19 14:40:52 +00:00
|
|
|
/>
|
2019-08-09 11:14:35 +00:00
|
|
|
<Grid variant="inverted">
|
2021-10-13 11:35:00 +00:00
|
|
|
<PageSectionHeader
|
|
|
|
title={intl.formatMessage(messages.sectionCheckoutTitle)}
|
|
|
|
description={intl.formatMessage(
|
2022-06-21 09:36:55 +00:00
|
|
|
messages.sectionCheckoutDescription,
|
2021-10-13 11:35:00 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<SiteCheckoutSettingsCard
|
|
|
|
data={data}
|
|
|
|
errors={errors}
|
|
|
|
disabled={disabled}
|
|
|
|
onChange={change}
|
|
|
|
/>
|
|
|
|
<Hr className={classes.hr} />
|
|
|
|
<PageSectionHeader
|
|
|
|
title={intl.formatMessage(messages.sectionCompanyTitle)}
|
|
|
|
description={intl.formatMessage(
|
2022-06-21 09:36:55 +00:00
|
|
|
messages.sectionCompanyDescription,
|
2021-10-13 11:35:00 +00:00
|
|
|
)}
|
|
|
|
/>
|
2020-01-30 13:17:29 +00:00
|
|
|
<CompanyAddressInput
|
2019-08-09 11:14:35 +00:00
|
|
|
data={data}
|
|
|
|
displayCountry={displayCountry}
|
|
|
|
countries={countryChoices}
|
2020-03-17 18:49:01 +00:00
|
|
|
errors={[...errors, ...validationErrors]}
|
2019-08-09 11:14:35 +00:00
|
|
|
disabled={disabled}
|
2020-01-30 13:17:29 +00:00
|
|
|
header={intl.formatMessage({
|
2022-05-05 07:54:28 +00:00
|
|
|
id: "+jCDvp",
|
2020-01-30 13:17:29 +00:00
|
|
|
defaultMessage: "Store Information",
|
2022-06-21 09:36:55 +00:00
|
|
|
description: "section header",
|
2020-01-30 13:17:29 +00:00
|
|
|
})}
|
2019-08-09 11:14:35 +00:00
|
|
|
onChange={change}
|
2022-12-21 08:54:45 +00:00
|
|
|
onCountryChange={handleCountrySelect}
|
2019-08-09 11:14:35 +00:00
|
|
|
/>
|
|
|
|
</Grid>
|
2021-07-21 08:59:52 +00:00
|
|
|
<Savebar
|
2019-08-09 11:14:35 +00:00
|
|
|
state={saveButtonBarState}
|
2022-12-14 17:11:24 +00:00
|
|
|
disabled={!!isSaveDisabled}
|
2022-05-06 08:59:55 +00:00
|
|
|
onCancel={() => navigate(configurationMenuUrl)}
|
2021-07-21 08:59:52 +00:00
|
|
|
onSubmit={submit}
|
2019-06-19 14:40:52 +00:00
|
|
|
/>
|
2019-08-09 11:14:35 +00:00
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}}
|
2019-06-19 14:40:52 +00:00
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
2019-10-21 10:13:23 +00:00
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
SiteSettingsPage.displayName = "SiteSettingsPage";
|
|
|
|
export default SiteSettingsPage;
|