saleor-dashboard/src/siteSettings/views/index.tsx

119 lines
4.2 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import { WindowTitle } from "@saleor/components/WindowTitle";
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";
import { commonMessages, sectionNames } from "@saleor/intl";
import React from "react";
import { useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import { configurationMenuUrl } from "../../configuration";
Exit dirty form (#1816) * Add Exit form prompt component and change some minor styles in other components to match * Add Exit form prompt provider * Adjust generic form and useform hook to allow using exit form prompt provider * Add exit form prompt provider to index * wip * Fix types * Fix styling * Fix types * Revert warehouse details refactor * Add handling of edge cases to exit prompt * Refactor, add comments, fix some types * Refactor after exit form dialog name change * fix types * Fixes after review * Add default value for useform prop opts so the app doesn't crash * Add missing category prop to getting initial data for category details form * Add exit dialog to everywhere WIP (#1600) * Add Exit form prompt component and change some minor styles in other components to match * Add Exit form prompt provider * Adjust generic form and useform hook to allow using exit form prompt provider * Add exit form prompt provider to index * wip * Fix types * Fix styling * Fix types * Revert warehouse details refactor * Add handling of edge cases to exit prompt * Refactor, add comments, fix some types * Refactor after exit form dialog name change * fix types * Add CommonUseFormResultWithHandlers type for later use and refactor handleFormSubmit util * Refactor login form not to use custom form since it doesn't need to * Add exit form dialog to order refund page * Add exit form dialog to order return page * Add exit form dialog to order order settings page * Add exit form dialog to product variant page * Add exit form dialog to product create page * Add exit form dialog to product update page * Add exit form dialog to product variant create page * Fix confirm leave prop passing in generic Form * Add util function to handle for submit to extract errors * Add confirmLeave prop to generic forms * Move handleChange for custom forms to useForm * Add exit dialog to more forms * Add extract mutation errors util function * Add extracting errors to submit functions that use metadata create handler * Fix typo * Add missing category prop to getting initial data for category details form * Fix types * wip * wip * wip * wip * Fix types & refactor * Fix types & refactor * Fix typescript * Fix unmatching tag * Fixes * Add handling of multiple forms at once to exit dirty form provider * Change all usages of ExitFormDialogContext to designated hook * wip * wip * wip * Fix types wip * Fix types * Remove console logs * Add isSubmitting prop to exit form dialog in order to avoid enabling exit dialog while submit is still in progresS * Replace handleSubmit global util with a hook to use exit form dialog props inside * Move useHandleSubmit to general hooks dir, update imports * Small fixes * Update snapshots * Fix types * Small fixes due to extensive rebase * Update package lock * Fixes after rebase * Remove exit form from customer address dialog * Fix types and update messages * Fix types * Change imports names * Refactor * Remove unnecessary console.log * Update types, snapshots. etc after rebase
2022-02-01 09:58:06 +00:00
import { extractMutationErrors, findInEnum } from "../../misc";
import { CountryCode } from "../../types/globalTypes";
2019-06-19 14:40:52 +00:00
import SiteSettingsPage, {
areAddressInputFieldsModified,
2019-06-19 14:40:52 +00:00
SiteSettingsPageFormData
} from "../components/SiteSettingsPage";
import { TypedShopSettingsUpdate } from "../mutations";
2019-06-19 14:40:52 +00:00
import { TypedSiteSettingsQuery } from "../queries";
import { ShopSettingsUpdate } from "../types/ShopSettingsUpdate";
import { SiteSettingsUrlQueryParams } from "../urls";
2019-06-19 14:40:52 +00:00
export interface SiteSettingsProps {
params: SiteSettingsUrlQueryParams;
}
export const SiteSettings: React.FC<SiteSettingsProps> = () => {
2019-06-19 14:40:52 +00:00
const navigate = useNavigator();
const notify = useNotifier();
const intl = useIntl();
2019-06-19 14:40:52 +00:00
const handleSiteSettingsSuccess = (data: ShopSettingsUpdate) => {
if (
[
...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",
text: intl.formatMessage(commonMessages.savedChanges)
2019-06-19 14:40:52 +00:00
});
}
};
2019-06-19 14:40:52 +00:00
return (
<TypedSiteSettingsQuery displayLoader>
{siteSettings => (
<TypedShopSettingsUpdate onCompleted={handleSiteSettingsSuccess}>
{(updateShopSettings, updateShopSettingsOpts) => {
const errors = [
...(updateShopSettingsOpts.data?.shopDomainUpdate?.errors || []),
...(updateShopSettingsOpts.data?.shopSettingsUpdate.errors || []),
...(updateShopSettingsOpts.data?.shopAddressUpdate.errors || [])
];
const loading =
siteSettings.loading || updateShopSettingsOpts.loading;
2019-06-19 14:40:52 +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
Exit dirty form (#1816) * Add Exit form prompt component and change some minor styles in other components to match * Add Exit form prompt provider * Adjust generic form and useform hook to allow using exit form prompt provider * Add exit form prompt provider to index * wip * Fix types * Fix styling * Fix types * Revert warehouse details refactor * Add handling of edge cases to exit prompt * Refactor, add comments, fix some types * Refactor after exit form dialog name change * fix types * Fixes after review * Add default value for useform prop opts so the app doesn't crash * Add missing category prop to getting initial data for category details form * Add exit dialog to everywhere WIP (#1600) * Add Exit form prompt component and change some minor styles in other components to match * Add Exit form prompt provider * Adjust generic form and useform hook to allow using exit form prompt provider * Add exit form prompt provider to index * wip * Fix types * Fix styling * Fix types * Revert warehouse details refactor * Add handling of edge cases to exit prompt * Refactor, add comments, fix some types * Refactor after exit form dialog name change * fix types * Add CommonUseFormResultWithHandlers type for later use and refactor handleFormSubmit util * Refactor login form not to use custom form since it doesn't need to * Add exit form dialog to order refund page * Add exit form dialog to order return page * Add exit form dialog to order order settings page * Add exit form dialog to product variant page * Add exit form dialog to product create page * Add exit form dialog to product update page * Add exit form dialog to product variant create page * Fix confirm leave prop passing in generic Form * Add util function to handle for submit to extract errors * Add confirmLeave prop to generic forms * Move handleChange for custom forms to useForm * Add exit dialog to more forms * Add extract mutation errors util function * Add extracting errors to submit functions that use metadata create handler * Fix typo * Add missing category prop to getting initial data for category details form * Fix types * wip * wip * wip * wip * Fix types & refactor * Fix types & refactor * Fix typescript * Fix unmatching tag * Fixes * Add handling of multiple forms at once to exit dirty form provider * Change all usages of ExitFormDialogContext to designated hook * wip * wip * wip * Fix types wip * Fix types * Remove console logs * Add isSubmitting prop to exit form dialog in order to avoid enabling exit dialog while submit is still in progresS * Replace handleSubmit global util with a hook to use exit form dialog props inside * Move useHandleSubmit to general hooks dir, update imports * Small fixes * Update snapshots * Fix types * Small fixes due to extensive rebase * Update package lock * Fixes after rebase * Remove exit form from customer address dialog * Fix types and update messages * Fix types * Change imports names * Refactor * Remove unnecessary console.log * Update types, snapshots. etc after rebase
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
}
})
);
};
2019-06-19 14:40:52 +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;