2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-08-26 21:26:36 +00:00
|
|
|
import { useIntl } from "react-intl";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
import AppHeader from "@saleor/components/AppHeader";
|
|
|
|
import CardSpacer from "@saleor/components/CardSpacer";
|
|
|
|
import { ConfirmButtonTransitionState } from "@saleor/components/ConfirmButton";
|
|
|
|
import Container from "@saleor/components/Container";
|
|
|
|
import CountryList from "@saleor/components/CountryList";
|
|
|
|
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-26 21:26:36 +00:00
|
|
|
import { sectionNames } from "@saleor/intl";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { CountryFragment } from "../../../taxes/types/CountryFragment";
|
|
|
|
import { UserError } from "../../../types";
|
|
|
|
import ShippingZoneCountriesAssignDialog from "../ShippingZoneCountriesAssignDialog";
|
|
|
|
import ShippingZoneInfo from "../ShippingZoneInfo";
|
|
|
|
|
|
|
|
export interface FormData {
|
|
|
|
countries: string[];
|
|
|
|
default: boolean;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ShippingZoneCreatePageProps {
|
|
|
|
countries: CountryFragment[];
|
|
|
|
disabled: boolean;
|
|
|
|
errors: UserError[];
|
|
|
|
saveButtonBarState: ConfirmButtonTransitionState;
|
|
|
|
onBack: () => void;
|
|
|
|
onSubmit: (data: FormData) => void;
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:34:54 +00:00
|
|
|
const ShippingZoneCreatePage: React.FC<ShippingZoneCreatePageProps> = ({
|
|
|
|
countries,
|
|
|
|
disabled,
|
|
|
|
errors,
|
|
|
|
onBack,
|
|
|
|
onSubmit,
|
|
|
|
saveButtonBarState
|
|
|
|
}) => {
|
2019-08-26 21:26:36 +00:00
|
|
|
const intl = useIntl();
|
2019-06-19 14:40:52 +00:00
|
|
|
const [isModalOpened, setModalStatus] = React.useState(false);
|
|
|
|
const toggleModal = () => setModalStatus(!isModalOpened);
|
|
|
|
|
|
|
|
const initialForm: FormData = {
|
|
|
|
countries: [],
|
|
|
|
default: false,
|
|
|
|
name: ""
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2020-02-24 14:14:48 +00:00
|
|
|
<Form initial={initialForm} onSubmit={onSubmit}>
|
|
|
|
{({ change, data, hasChanged, submit }) => (
|
2019-06-19 14:40:52 +00:00
|
|
|
<>
|
|
|
|
<Container>
|
2019-08-26 21:26:36 +00:00
|
|
|
<AppHeader onBack={onBack}>
|
|
|
|
{intl.formatMessage(sectionNames.shipping)}
|
|
|
|
</AppHeader>
|
|
|
|
<PageHeader
|
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Create New Shipping Zone",
|
|
|
|
description: "header"
|
|
|
|
})}
|
|
|
|
/>
|
2019-06-19 14:40:52 +00:00
|
|
|
<Grid>
|
|
|
|
<div>
|
|
|
|
<ShippingZoneInfo
|
|
|
|
data={data}
|
2020-02-24 14:14:48 +00:00
|
|
|
errors={errors}
|
2019-06-19 14:40:52 +00:00
|
|
|
onChange={change}
|
|
|
|
/>
|
|
|
|
<CardSpacer />
|
|
|
|
<CountryList
|
|
|
|
countries={data.countries.map(selectedCountry =>
|
|
|
|
countries.find(country => country.code === selectedCountry)
|
|
|
|
)}
|
|
|
|
disabled={disabled}
|
|
|
|
emptyText={
|
|
|
|
data.default
|
2019-08-26 21:26:36 +00:00
|
|
|
? intl.formatMessage({
|
|
|
|
defaultMessage:
|
|
|
|
"This is default shipping zone, which means that it covers all of the countries which are not assigned to other shipping zones"
|
|
|
|
})
|
|
|
|
: intl.formatMessage({
|
|
|
|
defaultMessage:
|
|
|
|
"Currently, there are no countries assigned to this shipping zone"
|
|
|
|
})
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
onCountryAssign={toggleModal}
|
|
|
|
onCountryUnassign={countryCode =>
|
|
|
|
change({
|
|
|
|
target: {
|
|
|
|
name: "countries",
|
|
|
|
value: data.countries.filter(
|
|
|
|
country => country !== countryCode
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} as any)
|
|
|
|
}
|
2019-08-26 21:26:36 +00:00
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Countries"
|
|
|
|
})}
|
2019-06-19 14:40:52 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Grid>
|
|
|
|
<SaveButtonBar
|
|
|
|
disabled={disabled || !hasChanged}
|
|
|
|
onCancel={onBack}
|
|
|
|
onSave={submit}
|
|
|
|
state={saveButtonBarState}
|
|
|
|
/>
|
|
|
|
</Container>
|
|
|
|
<ShippingZoneCountriesAssignDialog
|
|
|
|
open={isModalOpened}
|
2019-12-12 09:08:19 +00:00
|
|
|
onConfirm={formData => {
|
|
|
|
change({
|
|
|
|
target: {
|
|
|
|
name: "countries",
|
|
|
|
value: formData.restOfTheWorld ? [] : formData.countries
|
|
|
|
}
|
|
|
|
} as any);
|
|
|
|
toggleModal();
|
|
|
|
}}
|
2019-06-19 14:40:52 +00:00
|
|
|
confirmButtonState="default"
|
|
|
|
countries={countries}
|
|
|
|
initial={data.countries}
|
|
|
|
isDefault={data.default}
|
|
|
|
onClose={toggleModal}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
ShippingZoneCreatePage.displayName = "ShippingZoneCreatePage";
|
|
|
|
export default ShippingZoneCreatePage;
|