import { createCountryHandler } from "@dashboard/components/AddressEdit/createCountryHandler"; import { TopNav } from "@dashboard/components/AppLayout/TopNav"; import { DashboardCard } from "@dashboard/components/Card"; import CompanyAddressInput from "@dashboard/components/CompanyAddressInput"; import { ConfirmButtonTransitionState } from "@dashboard/components/ConfirmButton"; import Form from "@dashboard/components/Form"; import { DetailPageLayout } from "@dashboard/components/Layouts"; import PageSectionHeader from "@dashboard/components/PageSectionHeader"; import Savebar from "@dashboard/components/Savebar"; import { configurationMenuUrl } from "@dashboard/configuration"; import { ShopErrorFragment, SiteSettingsQuery } from "@dashboard/graphql"; import useAddressValidation from "@dashboard/hooks/useAddressValidation"; import { SubmitPromise } from "@dashboard/hooks/useForm"; import useNavigator from "@dashboard/hooks/useNavigator"; import useStateFromProps from "@dashboard/hooks/useStateFromProps"; import { commonMessages } from "@dashboard/intl"; import createSingleAutocompleteSelectHandler from "@dashboard/utils/handlers/singleAutocompleteSelectChangeHandler"; import { mapCountriesToChoices } from "@dashboard/utils/maps"; import { Box, Checkbox, Divider, Text } from "@saleor/macaw-ui/next"; import React from "react"; import { useIntl } from "react-intl"; import SiteCheckoutSettingsCard from "../SiteCheckoutSettingsCard"; import { messages } from "./messages"; export interface SiteSettingsPageAddressFormData { city: string; companyName: string; country: string; countryArea: string; phone: string; postalCode: string; streetAddress1: string; streetAddress2: string; } export interface SiteSettingsPageFormData extends SiteSettingsPageAddressFormData { description: string; reserveStockDurationAnonymousUser: number; reserveStockDurationAuthenticatedUser: number; limitQuantityPerCheckout: number; emailConfirmation: boolean; } export interface SiteSettingsPageProps { disabled: boolean; errors: ShopErrorFragment[]; shop?: SiteSettingsQuery["shop"]; saveButtonBarState: ConfirmButtonTransitionState; onSubmit: (data: SiteSettingsPageFormData) => SubmitPromise; } export function areAddressInputFieldsModified( data: SiteSettingsPageAddressFormData, ): boolean { return ( [ "city", "country", "countryArea", "phone", "postalCode", "streetAddress1", "streetAddress2", ] as Array ) .map(key => data[key]) .some(field => field !== ""); } const SiteSettingsPage: React.FC = props => { const { disabled, errors, saveButtonBarState, shop, onSubmit } = props; const intl = useIntl(); const navigate = useNavigator(); const [displayCountry, setDisplayCountry] = useStateFromProps( shop?.companyAddress?.country.country || "", ); const { errors: validationErrors, submit: handleSubmitWithAddress } = useAddressValidation(onSubmit); const initialFormAddress: SiteSettingsPageAddressFormData = { 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 || "", streetAddress2: shop?.companyAddress?.streetAddress2 || "", }; const initialForm: SiteSettingsPageFormData = { ...initialFormAddress, description: shop?.description || "", reserveStockDurationAnonymousUser: shop?.reserveStockDurationAnonymousUser ?? 0, reserveStockDurationAuthenticatedUser: shop?.reserveStockDurationAuthenticatedUser ?? 0, limitQuantityPerCheckout: shop?.limitQuantityPerCheckout ?? 0, emailConfirmation: shop?.enableAccountConfirmationByEmail ?? false, }; return (
{ const submitFunc = areAddressInputFieldsModified(data) ? handleSubmitWithAddress : onSubmit; return submitFunc(data); }} confirmLeave disabled={disabled} > {({ change, data, set, isSaveDisabled, submit }) => { const countryChoices = mapCountriesToChoices(shop?.countries || []); const countrySelect = createSingleAutocompleteSelectHandler( change, setDisplayCountry, countryChoices, ); const handleCountrySelect = createCountryHandler(countrySelect, set); const handleEmailConfirmationChange = isEnabled => { change({ target: { name: "emailConfirmation", value: isEnabled } }); }; return ( {intl.formatMessage( messages.sectionEmailConfirmationHeader, )} {intl.formatMessage( messages.sectionEmailConfirmationHeader, )} navigate(configurationMenuUrl)} onSubmit={submit} /> ); }}
); }; SiteSettingsPage.displayName = "SiteSettingsPage"; export default SiteSettingsPage;