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

149 lines
4.9 KiB
TypeScript
Raw Normal View History

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";
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";
import PageHeader from "@saleor/components/PageHeader";
import SaveButtonBar from "@saleor/components/SaveButtonBar";
2019-08-09 11:14:35 +00:00
import useStateFromProps from "@saleor/hooks/useStateFromProps";
import { UserError } from "@saleor/types";
import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
import { mapCountriesToChoices } from "@saleor/utils/maps";
2019-06-19 14:40:52 +00:00
import i18n from "../../../i18n";
import { maybe } from "../../../misc";
import { AuthorizationKeyType } from "../../../types/globalTypes";
import { SiteSettings_shop } from "../../types/SiteSettings";
2019-08-09 11:14:35 +00:00
import SiteSettingsAddress from "../SiteSettingsAddress/SiteSettingsAddress";
2019-06-19 14:40:52 +00:00
import SiteSettingsDetails from "../SiteSettingsDetails/SiteSettingsDetails";
import SiteSettingsKeys from "../SiteSettingsKeys/SiteSettingsKeys";
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
extends SiteSettingsPageAddressFormData {
2019-06-19 14:40:52 +00:00
description: string;
domain: string;
name: string;
}
export interface SiteSettingsPageProps {
disabled: boolean;
2019-08-09 11:14:35 +00:00
errors: UserError[];
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;
}
const SiteSettingsPage: React.StatelessComponent<SiteSettingsPageProps> = ({
disabled,
errors,
saveButtonBarState,
shop,
onBack,
onKeyAdd,
onKeyRemove,
onSubmit
}) => {
2019-08-09 11:14:35 +00:00
const [displayCountry, setDisplayCountry] = useStateFromProps(
maybe(() => shop.companyAddress.country.code, "")
);
2019-06-19 14:40:52 +00:00
const initialForm: SiteSettingsPageFormData = {
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, ""),
2019-06-19 14:40:52 +00:00
description: maybe(() => shop.description, ""),
domain: maybe(() => shop.domain.host, ""),
2019-08-09 11:14:35 +00:00
name: maybe(() => shop.name, ""),
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-08-09 11:14:35 +00:00
2019-06-19 14:40:52 +00:00
return (
<Form
errors={errors}
initial={initialForm}
onSubmit={onSubmit}
confirmLeave
>
2019-08-09 11:14:35 +00:00
{({ change, data, errors: formErrors, hasChanged, submit }) => {
const countryChoices = mapCountriesToChoices(
maybe(() => shop.countries, [])
);
const handleCountryChange = createSingleAutocompleteSelectHandler(
change,
setDisplayCountry,
countryChoices
);
return (
<Container>
<AppHeader onBack={onBack}>{i18n.t("Configuration")}</AppHeader>
<PageHeader
title={i18n.t("General Information", {
context: "page header"
})}
2019-06-19 14:40:52 +00:00
/>
2019-08-09 11:14:35 +00:00
<Grid variant="inverted">
<Typography variant="h6">{i18n.t("Site Settings")}</Typography>
<SiteSettingsDetails
data={data}
errors={formErrors}
disabled={disabled}
onChange={change}
/>
<Typography variant="h6">
{i18n.t("Company information")}
</Typography>
<SiteSettingsAddress
data={data}
displayCountry={displayCountry}
countries={countryChoices}
errors={formErrors}
disabled={disabled}
onChange={change}
onCountryChange={handleCountryChange}
/>
<Typography variant="h6">
{i18n.t("Authentication keys")}
</Typography>
<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>
);
};
SiteSettingsPage.displayName = "SiteSettingsPage";
export default SiteSettingsPage;