saleor-dashboard/src/siteSettings/components/SiteSettingsPage/SiteSettingsPage.tsx

250 lines
8.7 KiB
TypeScript
Raw Normal View History

2019-10-28 16:16:49 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-06-19 14:40:52 +00:00
import Typography from "@material-ui/core/Typography";
2019-08-09 10:26:22 +00:00
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import AppHeader from "@saleor/components/AppHeader";
import { ConfirmButtonTransitionState } from "@saleor/components/ConfirmButton";
import Container from "@saleor/components/Container";
import Form from "@saleor/components/Form";
import Grid from "@saleor/components/Grid";
2019-10-21 10:13:23 +00:00
import Hr from "@saleor/components/Hr";
2019-06-19 14:40:52 +00:00
import PageHeader from "@saleor/components/PageHeader";
import SaveButtonBar from "@saleor/components/SaveButtonBar";
import useAddressValidation from "@saleor/hooks/useAddressValidation";
2019-08-09 11:14:35 +00:00
import useStateFromProps from "@saleor/hooks/useStateFromProps";
import { commonMessages, sectionNames } from "@saleor/intl";
2019-08-09 11:14:35 +00:00
import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
import { mapCountriesToChoices } from "@saleor/utils/maps";
import { ShopErrorFragment } from "@saleor/siteSettings/types/ShopErrorFragment";
2020-01-30 13:17:29 +00:00
import CompanyAddressInput from "@saleor/components/CompanyAddressInput";
2019-06-19 14:40:52 +00:00
import { maybe } from "../../../misc";
import { AuthorizationKeyType } from "../../../types/globalTypes";
import { SiteSettings_shop } from "../../types/SiteSettings";
import SiteSettingsDetails from "../SiteSettingsDetails/SiteSettingsDetails";
import SiteSettingsKeys from "../SiteSettingsKeys/SiteSettingsKeys";
2019-10-21 11:11:05 +00:00
import SiteSettingsMailing, {
SiteSettingsMailingFormData
} from "../SiteSettingsMailing";
2019-06-19 14:40:52 +00:00
2019-08-09 11:14:35 +00:00
export interface SiteSettingsPageAddressFormData {
city: string;
companyName: string;
country: string;
countryArea: string;
phone: string;
postalCode: string;
streetAddress1: string;
streetAddress2: string;
}
export interface SiteSettingsPageFormData
2019-10-21 11:11:05 +00:00
extends SiteSettingsPageAddressFormData,
SiteSettingsMailingFormData {
2019-06-19 14:40:52 +00:00
description: string;
domain: string;
name: string;
}
export interface SiteSettingsPageProps {
disabled: boolean;
errors: ShopErrorFragment[];
2019-06-19 14:40:52 +00:00
shop: SiteSettings_shop;
saveButtonBarState: ConfirmButtonTransitionState;
onBack: () => void;
onKeyAdd: () => void;
onKeyRemove: (keyType: AuthorizationKeyType) => void;
onSubmit: (data: SiteSettingsPageFormData) => void;
}
export function areAddressInputFieldsModified(
data: SiteSettingsPageAddressFormData
): boolean {
return ([
"city",
"country",
"countryArea",
"phone",
"postalCode",
"streetAddress1",
"streetAddress2"
] as Array<keyof SiteSettingsPageAddressFormData>)
.map(key => data[key])
.some(field => field !== "");
}
2019-10-21 10:13:23 +00:00
const useStyles = makeStyles(
2019-10-28 16:16:49 +00:00
theme => ({
2019-10-21 10:13:23 +00:00
hr: {
gridColumnEnd: "span 2",
2019-10-28 16:16:49 +00:00
margin: theme.spacing(1, 0)
2019-10-21 10:13:23 +00:00
}
}),
{
name: "SiteSettingsPage"
}
);
const SiteSettingsPage: React.FC<SiteSettingsPageProps> = props => {
const {
disabled,
errors,
saveButtonBarState,
shop,
onBack,
onKeyAdd,
onKeyRemove,
onSubmit
} = props;
const classes = useStyles(props);
const intl = useIntl();
2019-08-09 11:14:35 +00:00
const [displayCountry, setDisplayCountry] = useStateFromProps(
maybe(() => shop.companyAddress.country.code, "")
);
const {
errors: validationErrors,
submit: handleSubmitWithAddress
} = useAddressValidation<SiteSettingsPageFormData>(onSubmit);
2019-10-21 14:14:28 +00:00
const initialFormAddress: SiteSettingsPageAddressFormData = {
2019-08-09 11:14:35 +00:00
city: maybe(() => shop.companyAddress.city, ""),
companyName: maybe(() => shop.companyAddress.companyName, ""),
country: maybe(() => shop.companyAddress.country.code, ""),
countryArea: maybe(() => shop.companyAddress.countryArea, ""),
phone: maybe(() => shop.companyAddress.phone, ""),
postalCode: maybe(() => shop.companyAddress.postalCode, ""),
streetAddress1: maybe(() => shop.companyAddress.streetAddress1, ""),
streetAddress2: maybe(() => shop.companyAddress.streetAddress2, "")
2019-06-19 14:40:52 +00:00
};
2019-10-21 14:14:28 +00:00
const initialForm: SiteSettingsPageFormData = {
...initialFormAddress,
customerSetPasswordUrl: maybe(() => shop.customerSetPasswordUrl, ""),
defaultMailSenderAddress: maybe(() => shop.defaultMailSenderAddress, ""),
defaultMailSenderName: maybe(() => shop.defaultMailSenderName, ""),
description: maybe(() => shop.description, ""),
domain: maybe(() => shop.domain.host, ""),
name: maybe(() => shop.name, "")
};
2019-08-09 11:14:35 +00:00
2019-06-19 14:40:52 +00:00
return (
<Form
initial={initialForm}
onSubmit={data => {
const submitFunc = areAddressInputFieldsModified(data)
? handleSubmitWithAddress
: onSubmit;
submitFunc(data);
}}
2019-06-19 14:40:52 +00:00
confirmLeave
>
2020-02-24 14:14:48 +00:00
{({ change, data, hasChanged, submit }) => {
const countryChoices = mapCountriesToChoices(shop?.countries || []);
2019-08-09 11:14:35 +00:00
const handleCountryChange = createSingleAutocompleteSelectHandler(
change,
setDisplayCountry,
countryChoices
);
return (
<Container>
<AppHeader onBack={onBack}>
{intl.formatMessage(sectionNames.configuration)}
</AppHeader>
2019-08-09 11:14:35 +00:00
<PageHeader
title={intl.formatMessage(commonMessages.generalInformations)}
2019-06-19 14:40:52 +00:00
/>
2019-08-09 11:14:35 +00:00
<Grid variant="inverted">
2019-10-21 10:13:23 +00:00
<div>
<Typography>
{intl.formatMessage(sectionNames.siteSettings)}
</Typography>
2019-10-28 16:16:49 +00:00
<Typography variant="body2">
2019-10-21 10:13:23 +00:00
<FormattedMessage defaultMessage="These are general information about your store. They define what is the URL of your store and what is shown in browsers taskbar." />
</Typography>
</div>
2019-08-09 11:14:35 +00:00
<SiteSettingsDetails
data={data}
errors={errors}
2019-08-09 11:14:35 +00:00
disabled={disabled}
onChange={change}
/>
2019-10-21 10:13:23 +00:00
<Hr className={classes.hr} />
<div>
<Typography>
<FormattedMessage
defaultMessage="Mailing Configuration"
description="section header"
/>
</Typography>
2019-10-28 16:16:49 +00:00
<Typography variant="body2">
2019-10-21 10:13:23 +00:00
<FormattedMessage defaultMessage="This where you will find all of the settings determining your stores e-mails. You can determine main email address and some of the contents of your emails." />
</Typography>
</div>
<SiteSettingsMailing
data={data}
errors={errors}
2019-10-21 10:13:23 +00:00
disabled={disabled}
onChange={change}
/>
<Hr className={classes.hr} />
<div>
<Typography>
<FormattedMessage
defaultMessage="Company Information"
description="section header"
/>
</Typography>
2019-10-28 16:16:49 +00:00
<Typography variant="body2">
2019-10-21 10:13:23 +00:00
<FormattedMessage defaultMessage="This adress will be used to generate invoices and calculate shipping rates." />
<FormattedMessage defaultMessage="Email adress you provide here will be used as a contact adress for your customers." />
</Typography>
</div>
2020-01-30 13:17:29 +00:00
<CompanyAddressInput
2019-08-09 11:14:35 +00:00
data={data}
displayCountry={displayCountry}
countries={countryChoices}
errors={[...errors, ...validationErrors]}
2019-08-09 11:14:35 +00:00
disabled={disabled}
2020-01-30 13:17:29 +00:00
header={intl.formatMessage({
defaultMessage: "Store Information",
description: "section header"
})}
2019-08-09 11:14:35 +00:00
onChange={change}
onCountryChange={handleCountryChange}
/>
2019-10-21 10:13:23 +00:00
<Hr className={classes.hr} />
<div>
<Typography>
<FormattedMessage
defaultMessage="Authentication Methods"
description="section header"
/>
</Typography>
2019-10-28 16:16:49 +00:00
<Typography variant="body2">
2019-11-05 12:57:30 +00:00
<FormattedMessage defaultMessage="Authentication method defines additional ways that customers can log in to your ecommerce." />
2019-10-21 10:13:23 +00:00
</Typography>
</div>
2019-08-09 11:14:35 +00:00
<SiteSettingsKeys
disabled={disabled}
keys={maybe(() => shop.authorizationKeys)}
onAdd={onKeyAdd}
onRemove={onKeyRemove}
/>
</Grid>
<SaveButtonBar
state={saveButtonBarState}
disabled={disabled || !hasChanged}
onCancel={onBack}
onSave={submit}
2019-06-19 14:40:52 +00:00
/>
2019-08-09 11:14:35 +00:00
</Container>
);
}}
2019-06-19 14:40:52 +00:00
</Form>
);
};
2019-10-21 10:13:23 +00:00
2019-06-19 14:40:52 +00:00
SiteSettingsPage.displayName = "SiteSettingsPage";
export default SiteSettingsPage;