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

201 lines
6.5 KiB
TypeScript
Raw Normal View History

import { Typography } from "@material-ui/core";
import CompanyAddressInput from "@saleor/components/CompanyAddressInput";
2019-06-19 14:40:52 +00:00
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";
Use MacawUI (#1229) * Replace withStyleswith useStyles (#1100) * Replace withStyleswith useStyles * Update messages * Use rem as a spacing unit (#1101) * Use rems as spacing units * Fix visual bugs * Update stories * Use macaw-ui as theme provider (#1108) * Use macaw ui as a theme provider * Add react-dom to aliases * Fix jest module resolution * Update useTheme hook usage * Fix test wrapper * Use macaw from git repo * Fix CI * Update stories * Fix aliasing * Extract savebar to macaw ui (#1146) * wip * Use savebar from macaw * Use confirm button from macaw * Improve file structure * Use sidebar context from macaw * Update macaw * Update macaw version * Remove savebar from storybook * Update stories * Use alerts and notifications from macaw (#1166) * Use alerts from macaw * Add notifications from macaw * Update stories * Pin macaw version * Encapsulate limit reached in one component * Remove unused imports * Use backlinks from macaw (#1183) * Use backlink from macaw * Update macaw version * Use macaw sidebar (#1148) * Use sidebar from macaw * Use shipped logo * Use lowercase * Update stories * Use user chip from macaw (#1191) * Use user chip from macaw * Use dedicated components for menu items * Simplify code * Bump version and fix types (#1210) * Rename onBack to onClick * Rename UserChip to UserChipMenu * Rename IMenuItem to SidebarMenuItem * Update macaw version * Fix tables after changes in macaw (#1220) * Update macaw version * Update changelog * Update stories * Fix after rebase * Update to macaw 0.2.0 * Lint files * Update macaw to 0.2.2
2021-07-21 08:59:52 +00:00
import Savebar from "@saleor/components/Savebar";
import { ShopErrorFragment } from "@saleor/fragments/types/ShopErrorFragment";
import useAddressValidation from "@saleor/hooks/useAddressValidation";
import { SubmitPromise } from "@saleor/hooks/useForm";
2019-08-09 11:14:35 +00:00
import useStateFromProps from "@saleor/hooks/useStateFromProps";
import { commonMessages, sectionNames } from "@saleor/intl";
Use MacawUI (#1229) * Replace withStyleswith useStyles (#1100) * Replace withStyleswith useStyles * Update messages * Use rem as a spacing unit (#1101) * Use rems as spacing units * Fix visual bugs * Update stories * Use macaw-ui as theme provider (#1108) * Use macaw ui as a theme provider * Add react-dom to aliases * Fix jest module resolution * Update useTheme hook usage * Fix test wrapper * Use macaw from git repo * Fix CI * Update stories * Fix aliasing * Extract savebar to macaw ui (#1146) * wip * Use savebar from macaw * Use confirm button from macaw * Improve file structure * Use sidebar context from macaw * Update macaw * Update macaw version * Remove savebar from storybook * Update stories * Use alerts and notifications from macaw (#1166) * Use alerts from macaw * Add notifications from macaw * Update stories * Pin macaw version * Encapsulate limit reached in one component * Remove unused imports * Use backlinks from macaw (#1183) * Use backlink from macaw * Update macaw version * Use macaw sidebar (#1148) * Use sidebar from macaw * Use shipped logo * Use lowercase * Update stories * Use user chip from macaw (#1191) * Use user chip from macaw * Use dedicated components for menu items * Simplify code * Bump version and fix types (#1210) * Rename onBack to onClick * Rename UserChip to UserChipMenu * Rename IMenuItem to SidebarMenuItem * Update macaw version * Fix tables after changes in macaw (#1220) * Update macaw version * Update changelog * Update stories * Fix after rebase * Update to macaw 0.2.0 * Lint files * Update macaw to 0.2.2
2021-07-21 08:59:52 +00:00
import { Backlink } from "@saleor/macaw-ui";
import { makeStyles } from "@saleor/macaw-ui";
2019-08-09 11:14:35 +00:00
import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
import { mapCountriesToChoices } from "@saleor/utils/maps";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import { SiteSettings_shop } from "../../types/SiteSettings";
import SiteSettingsDetails from "../SiteSettingsDetails/SiteSettingsDetails";
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
2021-03-29 09:24:47 +00:00
extends SiteSettingsPageAddressFormData {
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;
onSubmit: (data: SiteSettingsPageFormData) => SubmitPromise;
2019-06-19 14:40:52 +00:00
}
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,
onSubmit
} = props;
const classes = useStyles(props);
const intl = useIntl();
2019-08-09 11:14:35 +00:00
const [displayCountry, setDisplayCountry] = useStateFromProps(
shop?.companyAddress?.country.code || ""
2019-08-09 11:14:35 +00:00
);
const {
errors: validationErrors,
submit: handleSubmitWithAddress
2020-10-22 11:33:29 +00:00
} = useAddressValidation(onSubmit);
2019-10-21 14:14:28 +00:00
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 || ""
2019-06-19 14:40:52 +00:00
};
2019-10-21 14:14:28 +00:00
const initialForm: SiteSettingsPageFormData = {
...initialFormAddress,
description: shop?.description || "",
domain: shop?.domain.host || "",
name: shop?.name || ""
2019-10-21 14:14:28 +00:00
};
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;
2020-10-22 11:33:29 +00:00
return 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>
Use MacawUI (#1229) * Replace withStyleswith useStyles (#1100) * Replace withStyleswith useStyles * Update messages * Use rem as a spacing unit (#1101) * Use rems as spacing units * Fix visual bugs * Update stories * Use macaw-ui as theme provider (#1108) * Use macaw ui as a theme provider * Add react-dom to aliases * Fix jest module resolution * Update useTheme hook usage * Fix test wrapper * Use macaw from git repo * Fix CI * Update stories * Fix aliasing * Extract savebar to macaw ui (#1146) * wip * Use savebar from macaw * Use confirm button from macaw * Improve file structure * Use sidebar context from macaw * Update macaw * Update macaw version * Remove savebar from storybook * Update stories * Use alerts and notifications from macaw (#1166) * Use alerts from macaw * Add notifications from macaw * Update stories * Pin macaw version * Encapsulate limit reached in one component * Remove unused imports * Use backlinks from macaw (#1183) * Use backlink from macaw * Update macaw version * Use macaw sidebar (#1148) * Use sidebar from macaw * Use shipped logo * Use lowercase * Update stories * Use user chip from macaw (#1191) * Use user chip from macaw * Use dedicated components for menu items * Simplify code * Bump version and fix types (#1210) * Rename onBack to onClick * Rename UserChip to UserChipMenu * Rename IMenuItem to SidebarMenuItem * Update macaw version * Fix tables after changes in macaw (#1220) * Update macaw version * Update changelog * Update stories * Fix after rebase * Update to macaw 0.2.0 * Lint files * Update macaw to 0.2.2
2021-07-21 08:59:52 +00:00
<Backlink onClick={onBack}>
{intl.formatMessage(sectionNames.configuration)}
Use MacawUI (#1229) * Replace withStyleswith useStyles (#1100) * Replace withStyleswith useStyles * Update messages * Use rem as a spacing unit (#1101) * Use rems as spacing units * Fix visual bugs * Update stories * Use macaw-ui as theme provider (#1108) * Use macaw ui as a theme provider * Add react-dom to aliases * Fix jest module resolution * Update useTheme hook usage * Fix test wrapper * Use macaw from git repo * Fix CI * Update stories * Fix aliasing * Extract savebar to macaw ui (#1146) * wip * Use savebar from macaw * Use confirm button from macaw * Improve file structure * Use sidebar context from macaw * Update macaw * Update macaw version * Remove savebar from storybook * Update stories * Use alerts and notifications from macaw (#1166) * Use alerts from macaw * Add notifications from macaw * Update stories * Pin macaw version * Encapsulate limit reached in one component * Remove unused imports * Use backlinks from macaw (#1183) * Use backlink from macaw * Update macaw version * Use macaw sidebar (#1148) * Use sidebar from macaw * Use shipped logo * Use lowercase * Update stories * Use user chip from macaw (#1191) * Use user chip from macaw * Use dedicated components for menu items * Simplify code * Bump version and fix types (#1210) * Rename onBack to onClick * Rename UserChip to UserChipMenu * Rename IMenuItem to SidebarMenuItem * Update macaw version * Fix tables after changes in macaw (#1220) * Update macaw version * Update changelog * Update stories * Fix after rebase * Update to macaw 0.2.0 * Lint files * Update macaw to 0.2.2
2021-07-21 08:59:52 +00:00
</Backlink>
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="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}
/>
</Grid>
Use MacawUI (#1229) * Replace withStyleswith useStyles (#1100) * Replace withStyleswith useStyles * Update messages * Use rem as a spacing unit (#1101) * Use rems as spacing units * Fix visual bugs * Update stories * Use macaw-ui as theme provider (#1108) * Use macaw ui as a theme provider * Add react-dom to aliases * Fix jest module resolution * Update useTheme hook usage * Fix test wrapper * Use macaw from git repo * Fix CI * Update stories * Fix aliasing * Extract savebar to macaw ui (#1146) * wip * Use savebar from macaw * Use confirm button from macaw * Improve file structure * Use sidebar context from macaw * Update macaw * Update macaw version * Remove savebar from storybook * Update stories * Use alerts and notifications from macaw (#1166) * Use alerts from macaw * Add notifications from macaw * Update stories * Pin macaw version * Encapsulate limit reached in one component * Remove unused imports * Use backlinks from macaw (#1183) * Use backlink from macaw * Update macaw version * Use macaw sidebar (#1148) * Use sidebar from macaw * Use shipped logo * Use lowercase * Update stories * Use user chip from macaw (#1191) * Use user chip from macaw * Use dedicated components for menu items * Simplify code * Bump version and fix types (#1210) * Rename onBack to onClick * Rename UserChip to UserChipMenu * Rename IMenuItem to SidebarMenuItem * Update macaw version * Fix tables after changes in macaw (#1220) * Update macaw version * Update changelog * Update stories * Fix after rebase * Update to macaw 0.2.0 * Lint files * Update macaw to 0.2.2
2021-07-21 08:59:52 +00:00
<Savebar
2019-08-09 11:14:35 +00:00
state={saveButtonBarState}
disabled={disabled || !hasChanged}
onCancel={onBack}
Use MacawUI (#1229) * Replace withStyleswith useStyles (#1100) * Replace withStyleswith useStyles * Update messages * Use rem as a spacing unit (#1101) * Use rems as spacing units * Fix visual bugs * Update stories * Use macaw-ui as theme provider (#1108) * Use macaw ui as a theme provider * Add react-dom to aliases * Fix jest module resolution * Update useTheme hook usage * Fix test wrapper * Use macaw from git repo * Fix CI * Update stories * Fix aliasing * Extract savebar to macaw ui (#1146) * wip * Use savebar from macaw * Use confirm button from macaw * Improve file structure * Use sidebar context from macaw * Update macaw * Update macaw version * Remove savebar from storybook * Update stories * Use alerts and notifications from macaw (#1166) * Use alerts from macaw * Add notifications from macaw * Update stories * Pin macaw version * Encapsulate limit reached in one component * Remove unused imports * Use backlinks from macaw (#1183) * Use backlink from macaw * Update macaw version * Use macaw sidebar (#1148) * Use sidebar from macaw * Use shipped logo * Use lowercase * Update stories * Use user chip from macaw (#1191) * Use user chip from macaw * Use dedicated components for menu items * Simplify code * Bump version and fix types (#1210) * Rename onBack to onClick * Rename UserChip to UserChipMenu * Rename IMenuItem to SidebarMenuItem * Update macaw version * Fix tables after changes in macaw (#1220) * Update macaw version * Update changelog * Update stories * Fix after rebase * Update to macaw 0.2.0 * Lint files * Update macaw to 0.2.2
2021-07-21 08:59:52 +00:00
onSubmit={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;