2019-06-19 14:40:52 +00:00
|
|
|
import { WindowTitle } from "@saleor/components/WindowTitle";
|
|
|
|
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";
|
2020-03-17 18:49:01 +00:00
|
|
|
import { findInEnum } from "../../misc";
|
2019-10-25 12:18:52 +00:00
|
|
|
import { AuthorizationKeyType, CountryCode } from "../../types/globalTypes";
|
2019-06-19 14:40:52 +00:00
|
|
|
import SiteSettingsKeyDialog, {
|
|
|
|
SiteSettingsKeyDialogForm
|
|
|
|
} from "../components/SiteSettingsKeyDialog";
|
|
|
|
import SiteSettingsPage, {
|
2019-12-06 13:56:16 +00:00
|
|
|
areAddressInputFieldsModified,
|
2019-06-19 14:40:52 +00:00
|
|
|
SiteSettingsPageFormData
|
|
|
|
} from "../components/SiteSettingsPage";
|
|
|
|
import {
|
|
|
|
TypedAuthorizationKeyAdd,
|
|
|
|
TypedAuthorizationKeyDelete,
|
|
|
|
TypedShopSettingsUpdate
|
|
|
|
} from "../mutations";
|
|
|
|
import { TypedSiteSettingsQuery } from "../queries";
|
|
|
|
import { AuthorizationKeyAdd } from "../types/AuthorizationKeyAdd";
|
|
|
|
import { AuthorizationKeyDelete } from "../types/AuthorizationKeyDelete";
|
|
|
|
import { ShopSettingsUpdate } from "../types/ShopSettingsUpdate";
|
|
|
|
import { siteSettingsUrl, SiteSettingsUrlQueryParams } from "../urls";
|
|
|
|
|
|
|
|
export interface SiteSettingsProps {
|
|
|
|
params: SiteSettingsUrlQueryParams;
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:34:54 +00:00
|
|
|
export const SiteSettings: React.FC<SiteSettingsProps> = ({ params }) => {
|
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 handleAddKeySuccess = (data: AuthorizationKeyAdd) => {
|
2020-03-17 18:49:01 +00:00
|
|
|
if (data.authorizationKeyAdd.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
|
|
|
});
|
|
|
|
navigate(siteSettingsUrl());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const handleDeleteKeySuccess = (data: AuthorizationKeyDelete) => {
|
2020-03-17 18:49:01 +00:00
|
|
|
if (data.authorizationKeyDelete.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
|
|
|
});
|
|
|
|
} else {
|
|
|
|
notify({
|
2020-07-01 09:39:36 +00:00
|
|
|
status: "error",
|
2020-03-17 18:58:55 +00:00
|
|
|
text: intl.formatMessage(commonMessages.somethingWentWrong)
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const handleSiteSettingsSuccess = (data: ShopSettingsUpdate) => {
|
|
|
|
if (
|
2020-03-17 18:49:01 +00:00
|
|
|
data.shopDomainUpdate.errors.length === 0 &&
|
|
|
|
data.shopSettingsUpdate.errors.length === 0 &&
|
|
|
|
data.shopAddressUpdate.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 => (
|
|
|
|
<TypedAuthorizationKeyAdd onCompleted={handleAddKeySuccess}>
|
|
|
|
{(addAuthorizationKey, addAuthorizationKeyOpts) => (
|
|
|
|
<TypedAuthorizationKeyDelete onCompleted={handleDeleteKeySuccess}>
|
|
|
|
{(deleteAuthorizationKey, _) => (
|
|
|
|
<TypedShopSettingsUpdate
|
|
|
|
onCompleted={handleSiteSettingsSuccess}
|
|
|
|
>
|
|
|
|
{(updateShopSettings, updateShopSettingsOpts) => {
|
|
|
|
const errors = [
|
2020-03-17 18:49:01 +00:00
|
|
|
...(updateShopSettingsOpts.data?.shopDomainUpdate
|
|
|
|
.errors || []),
|
|
|
|
...(updateShopSettingsOpts.data?.shopSettingsUpdate
|
|
|
|
.errors || []),
|
|
|
|
...(updateShopSettingsOpts.data?.shopAddressUpdate
|
|
|
|
.errors || [])
|
2019-06-19 14:40:52 +00:00
|
|
|
];
|
|
|
|
const loading =
|
|
|
|
siteSettings.loading ||
|
|
|
|
addAuthorizationKeyOpts.loading ||
|
|
|
|
updateShopSettingsOpts.loading;
|
|
|
|
|
|
|
|
const handleAuthenticationKeyAdd = (
|
|
|
|
data: SiteSettingsKeyDialogForm
|
|
|
|
) =>
|
|
|
|
addAuthorizationKey({
|
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
key: data.key,
|
|
|
|
password: data.password
|
|
|
|
},
|
|
|
|
keyType: data.type
|
|
|
|
}
|
|
|
|
});
|
2020-10-22 11:33:29 +00:00
|
|
|
const handleUpdateShopSettings = async (
|
2019-06-19 14:40:52 +00:00
|
|
|
data: SiteSettingsPageFormData
|
2019-10-24 12:25:42 +00:00
|
|
|
) => {
|
2019-12-06 13:56:16 +00:00
|
|
|
const addressInput = areAddressInputFieldsModified(data)
|
2019-10-24 12:25:42 +00:00
|
|
|
? {
|
2019-08-09 11:14:35 +00:00
|
|
|
city: data.city,
|
|
|
|
companyName: data.companyName,
|
2019-12-06 13:56:16 +00:00
|
|
|
country: findInEnum(data.country, CountryCode),
|
2019-08-09 11:14:35 +00:00
|
|
|
countryArea: data.countryArea,
|
|
|
|
phone: data.phone,
|
|
|
|
postalCode: data.postalCode,
|
|
|
|
streetAddress1: data.streetAddress1,
|
|
|
|
streetAddress2: data.streetAddress2
|
2019-10-24 12:25:42 +00:00
|
|
|
}
|
2019-12-06 13:56:16 +00:00
|
|
|
: {
|
|
|
|
companyName: data.companyName
|
|
|
|
};
|
2020-10-22 11:33:29 +00:00
|
|
|
const result = await updateShopSettings({
|
2019-10-24 12:25:42 +00:00
|
|
|
variables: {
|
|
|
|
addressInput,
|
2019-06-19 14:40:52 +00:00
|
|
|
shopDomainInput: {
|
|
|
|
domain: data.domain,
|
|
|
|
name: data.name
|
|
|
|
},
|
|
|
|
shopSettingsInput: {
|
2019-10-21 14:14:28 +00:00
|
|
|
customerSetPasswordUrl: data.customerSetPasswordUrl,
|
|
|
|
defaultMailSenderAddress:
|
|
|
|
data.defaultMailSenderAddress,
|
|
|
|
defaultMailSenderName: data.defaultMailSenderName,
|
2019-06-19 14:40:52 +00:00
|
|
|
description: data.description
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-10-22 11:33:29 +00:00
|
|
|
|
|
|
|
return [
|
2020-10-30 10:15:12 +00:00
|
|
|
...result.data.shopAddressUpdate.errors,
|
|
|
|
...result.data.shopDomainUpdate.errors,
|
|
|
|
...result.data.shopSettingsUpdate.errors
|
2020-10-22 11:33:29 +00:00
|
|
|
];
|
2019-10-24 12:25:42 +00:00
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2019-08-26 21:39:21 +00:00
|
|
|
<WindowTitle
|
|
|
|
title={intl.formatMessage(sectionNames.siteSettings)}
|
|
|
|
/>
|
2019-06-19 14:40:52 +00:00
|
|
|
<SiteSettingsPage
|
|
|
|
disabled={loading}
|
|
|
|
errors={errors}
|
2020-03-17 18:49:01 +00:00
|
|
|
shop={siteSettings.data?.shop}
|
2019-06-19 14:40:52 +00:00
|
|
|
onBack={() => navigate(configurationMenuUrl)}
|
|
|
|
onKeyAdd={() =>
|
|
|
|
navigate(
|
|
|
|
siteSettingsUrl({
|
|
|
|
action: "add-key"
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
onKeyRemove={keyType =>
|
|
|
|
deleteAuthorizationKey({
|
|
|
|
variables: { keyType }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
onSubmit={handleUpdateShopSettings}
|
2019-12-06 17:17:44 +00:00
|
|
|
saveButtonBarState={updateShopSettingsOpts.status}
|
2019-06-19 14:40:52 +00:00
|
|
|
/>
|
|
|
|
<SiteSettingsKeyDialog
|
2020-03-17 18:49:01 +00:00
|
|
|
errors={
|
|
|
|
addAuthorizationKeyOpts.data?.authorizationKeyAdd
|
|
|
|
.errors || []
|
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
initial={{
|
|
|
|
key: "",
|
|
|
|
password: "",
|
|
|
|
type: AuthorizationKeyType.FACEBOOK
|
|
|
|
}}
|
|
|
|
open={params.action === "add-key"}
|
|
|
|
onClose={() => navigate(siteSettingsUrl())}
|
|
|
|
onSubmit={handleAuthenticationKeyAdd}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedShopSettingsUpdate>
|
|
|
|
)}
|
|
|
|
</TypedAuthorizationKeyDelete>
|
|
|
|
)}
|
|
|
|
</TypedAuthorizationKeyAdd>
|
|
|
|
)}
|
|
|
|
</TypedSiteSettingsQuery>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default SiteSettings;
|