2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
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";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
import { configurationMenuUrl } from "../../configuration";
|
2019-10-25 12:18:52 +00:00
|
|
|
import { findInEnum, getMutationState, maybe } from "../../misc";
|
|
|
|
import { AuthorizationKeyType, CountryCode } from "../../types/globalTypes";
|
2019-06-19 14:40:52 +00:00
|
|
|
import SiteSettingsKeyDialog, {
|
|
|
|
SiteSettingsKeyDialogForm
|
|
|
|
} from "../components/SiteSettingsKeyDialog";
|
|
|
|
import SiteSettingsPage, {
|
2019-10-24 12:25:42 +00:00
|
|
|
SiteSettingsPageAddressFormData,
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const SiteSettings: React.StatelessComponent<SiteSettingsProps> = ({
|
|
|
|
params
|
|
|
|
}) => {
|
|
|
|
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) => {
|
|
|
|
if (!maybe(() => data.authorizationKeyAdd.errors.length)) {
|
|
|
|
notify({
|
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) => {
|
|
|
|
if (!maybe(() => data.authorizationKeyDelete.errors.length)) {
|
|
|
|
notify({
|
2019-08-26 21:39:21 +00:00
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
notify({
|
2019-08-26 21:39:21 +00:00
|
|
|
text: intl.formatMessage(
|
|
|
|
{
|
|
|
|
defaultMessage: "Could not delete authorization key: {errorMessage}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
errorMessage: data.authorizationKeyDelete.errors[0].message
|
|
|
|
}
|
|
|
|
)
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const handleSiteSettingsSuccess = (data: ShopSettingsUpdate) => {
|
|
|
|
if (
|
|
|
|
(!data.shopDomainUpdate.errors ||
|
|
|
|
data.shopDomainUpdate.errors.length === 0) &&
|
|
|
|
(!data.shopSettingsUpdate.errors ||
|
2019-08-09 11:14:35 +00:00
|
|
|
data.shopSettingsUpdate.errors.length === 0) &&
|
|
|
|
(!data.shopAddressUpdate.errors ||
|
|
|
|
data.shopAddressUpdate.errors.length === 0)
|
2019-06-19 14:40:52 +00:00
|
|
|
) {
|
|
|
|
notify({
|
2019-08-26 21:39:21 +00:00
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
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 = [
|
|
|
|
...maybe(
|
|
|
|
() =>
|
|
|
|
updateShopSettingsOpts.data.shopDomainUpdate.errors,
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
...maybe(
|
|
|
|
() =>
|
|
|
|
updateShopSettingsOpts.data.shopSettingsUpdate.errors,
|
|
|
|
[]
|
2019-08-09 11:14:35 +00:00
|
|
|
),
|
|
|
|
...maybe(
|
|
|
|
() =>
|
|
|
|
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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const handleUpdateShopSettings = (
|
|
|
|
data: SiteSettingsPageFormData
|
2019-10-24 12:25:42 +00:00
|
|
|
) => {
|
|
|
|
const areAddressInputFieldsModified = ([
|
|
|
|
"city",
|
|
|
|
"companyName",
|
|
|
|
"country",
|
|
|
|
"countryArea",
|
|
|
|
"phone",
|
|
|
|
"postalCode",
|
|
|
|
"streetAddress1",
|
|
|
|
"streetAddress2"
|
|
|
|
] as Array<keyof SiteSettingsPageAddressFormData>)
|
|
|
|
.map(key => data[key])
|
|
|
|
.some(field => field !== "");
|
|
|
|
const addressInput = areAddressInputFieldsModified
|
|
|
|
? {
|
2019-08-09 11:14:35 +00:00
|
|
|
city: data.city,
|
|
|
|
companyName: data.companyName,
|
2019-10-25 12:18:52 +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
|
|
|
}
|
|
|
|
: null;
|
|
|
|
updateShopSettings({
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-10-24 12:25:42 +00:00
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
const formTransitionState = getMutationState(
|
|
|
|
updateShopSettingsOpts.called,
|
|
|
|
updateShopSettingsOpts.loading,
|
|
|
|
[
|
|
|
|
...maybe(
|
|
|
|
() =>
|
|
|
|
updateShopSettingsOpts.data.shopDomainUpdate.errors,
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
...maybe(
|
|
|
|
() =>
|
|
|
|
updateShopSettingsOpts.data.shopSettingsUpdate
|
|
|
|
.errors,
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
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}
|
|
|
|
shop={maybe(() => siteSettings.data.shop)}
|
|
|
|
onBack={() => navigate(configurationMenuUrl)}
|
|
|
|
onKeyAdd={() =>
|
|
|
|
navigate(
|
|
|
|
siteSettingsUrl({
|
|
|
|
action: "add-key"
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
onKeyRemove={keyType =>
|
|
|
|
deleteAuthorizationKey({
|
|
|
|
variables: { keyType }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
onSubmit={handleUpdateShopSettings}
|
|
|
|
saveButtonBarState={formTransitionState}
|
|
|
|
/>
|
|
|
|
<SiteSettingsKeyDialog
|
|
|
|
errors={maybe(
|
|
|
|
() =>
|
|
|
|
addAuthorizationKeyOpts.data.authorizationKeyAdd
|
|
|
|
.errors,
|
|
|
|
[]
|
|
|
|
)}
|
|
|
|
initial={{
|
|
|
|
key: "",
|
|
|
|
password: "",
|
|
|
|
type: AuthorizationKeyType.FACEBOOK
|
|
|
|
}}
|
|
|
|
open={params.action === "add-key"}
|
|
|
|
onClose={() => navigate(siteSettingsUrl())}
|
|
|
|
onSubmit={handleAuthenticationKeyAdd}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedShopSettingsUpdate>
|
|
|
|
)}
|
|
|
|
</TypedAuthorizationKeyDelete>
|
|
|
|
)}
|
|
|
|
</TypedAuthorizationKeyAdd>
|
|
|
|
)}
|
|
|
|
</TypedSiteSettingsQuery>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default SiteSettings;
|