2019-06-19 14:40:52 +00:00
|
|
|
import { WindowTitle } from "@saleor/components/WindowTitle";
|
2021-09-21 13:30:41 +00:00
|
|
|
import { IS_CLOUD_INSTANCE } from "@saleor/config";
|
2019-06-19 14:40:52 +00:00
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
2019-08-26 21:39:21 +00:00
|
|
|
import { commonMessages, sectionNames } from "@saleor/intl";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
2019-08-26 21:39:21 +00:00
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
import { configurationMenuUrl } from "../../configuration";
|
2022-02-01 09:58:06 +00:00
|
|
|
import { extractMutationErrors, findInEnum } from "../../misc";
|
2020-12-21 12:50:04 +00:00
|
|
|
import { CountryCode } from "../../types/globalTypes";
|
2019-06-19 14:40:52 +00:00
|
|
|
import SiteSettingsPage, {
|
2019-12-06 13:56:16 +00:00
|
|
|
areAddressInputFieldsModified,
|
2019-06-19 14:40:52 +00:00
|
|
|
SiteSettingsPageFormData
|
|
|
|
} from "../components/SiteSettingsPage";
|
2020-12-21 12:50:04 +00:00
|
|
|
import { TypedShopSettingsUpdate } from "../mutations";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { TypedSiteSettingsQuery } from "../queries";
|
|
|
|
import { ShopSettingsUpdate } from "../types/ShopSettingsUpdate";
|
2020-12-21 12:50:04 +00:00
|
|
|
import { SiteSettingsUrlQueryParams } from "../urls";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
export interface SiteSettingsProps {
|
|
|
|
params: SiteSettingsUrlQueryParams;
|
|
|
|
}
|
|
|
|
|
2020-12-21 12:50:04 +00:00
|
|
|
export const SiteSettings: React.FC<SiteSettingsProps> = () => {
|
2019-06-19 14:40:52 +00:00
|
|
|
const navigate = useNavigator();
|
|
|
|
const notify = useNotifier();
|
2019-08-26 21:39:21 +00:00
|
|
|
const intl = useIntl();
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
const handleSiteSettingsSuccess = (data: ShopSettingsUpdate) => {
|
|
|
|
if (
|
2021-09-13 14:14:04 +00:00
|
|
|
[
|
|
|
|
...data.shopAddressUpdate.errors,
|
|
|
|
...data.shopSettingsUpdate.errors,
|
|
|
|
...(data.shopDomainUpdate?.errors || [])
|
|
|
|
].length === 0
|
2019-06-19 14:40:52 +00:00
|
|
|
) {
|
|
|
|
notify({
|
2020-07-01 09:39:36 +00:00
|
|
|
status: "success",
|
2019-08-26 21:39:21 +00:00
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2019-12-06 13:56:16 +00:00
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
return (
|
|
|
|
<TypedSiteSettingsQuery displayLoader>
|
|
|
|
{siteSettings => (
|
2020-12-21 12:50:04 +00:00
|
|
|
<TypedShopSettingsUpdate onCompleted={handleSiteSettingsSuccess}>
|
|
|
|
{(updateShopSettings, updateShopSettingsOpts) => {
|
|
|
|
const errors = [
|
2021-09-13 14:14:04 +00:00
|
|
|
...(updateShopSettingsOpts.data?.shopDomainUpdate?.errors || []),
|
2020-12-21 12:50:04 +00:00
|
|
|
...(updateShopSettingsOpts.data?.shopSettingsUpdate.errors || []),
|
|
|
|
...(updateShopSettingsOpts.data?.shopAddressUpdate.errors || [])
|
|
|
|
];
|
|
|
|
const loading =
|
|
|
|
siteSettings.loading || updateShopSettingsOpts.loading;
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-12-21 12:50:04 +00:00
|
|
|
const handleUpdateShopSettings = async (
|
|
|
|
data: SiteSettingsPageFormData
|
|
|
|
) => {
|
|
|
|
const addressInput = areAddressInputFieldsModified(data)
|
|
|
|
? {
|
|
|
|
city: data.city,
|
|
|
|
companyName: data.companyName,
|
|
|
|
country: findInEnum(data.country, CountryCode),
|
|
|
|
countryArea: data.countryArea,
|
|
|
|
phone: data.phone,
|
|
|
|
postalCode: data.postalCode,
|
|
|
|
streetAddress1: data.streetAddress1,
|
|
|
|
streetAddress2: data.streetAddress2
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
companyName: data.companyName
|
|
|
|
};
|
2020-10-22 11:33:29 +00:00
|
|
|
|
2022-02-01 09:58:06 +00:00
|
|
|
return extractMutationErrors(
|
|
|
|
updateShopSettings({
|
|
|
|
variables: {
|
|
|
|
addressInput,
|
|
|
|
shopDomainInput: {
|
|
|
|
domain: data.domain,
|
|
|
|
name: data.name
|
|
|
|
},
|
|
|
|
shopSettingsInput: {
|
|
|
|
description: data.description,
|
|
|
|
reserveStockDurationAnonymousUser:
|
|
|
|
data.reserveStockDurationAnonymousUser || null,
|
|
|
|
reserveStockDurationAuthenticatedUser:
|
|
|
|
data.reserveStockDurationAuthenticatedUser || null
|
|
|
|
},
|
|
|
|
isCloudInstance: IS_CLOUD_INSTANCE
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2020-12-21 12:50:04 +00:00
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-12-21 12:50:04 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<WindowTitle
|
|
|
|
title={intl.formatMessage(sectionNames.siteSettings)}
|
|
|
|
/>
|
|
|
|
<SiteSettingsPage
|
|
|
|
disabled={loading}
|
|
|
|
errors={errors}
|
|
|
|
shop={siteSettings.data?.shop}
|
|
|
|
onBack={() => navigate(configurationMenuUrl)}
|
|
|
|
onSubmit={handleUpdateShopSettings}
|
|
|
|
saveButtonBarState={updateShopSettingsOpts.status}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedShopSettingsUpdate>
|
2019-06-19 14:40:52 +00:00
|
|
|
)}
|
|
|
|
</TypedSiteSettingsQuery>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default SiteSettings;
|