2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-08-26 21:26:36 +00:00
|
|
|
import { FormattedMessage, 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";
|
|
|
|
import { maybe } from "../../../misc";
|
|
|
|
import { UserError } from "../../../types";
|
|
|
|
import { ShippingMethodTypeEnum } from "../../../types/globalTypes";
|
|
|
|
import { ShippingZoneDetailsFragment } from "../../types/ShippingZoneDetailsFragment";
|
|
|
|
import ShippingZoneInfo from "../ShippingZoneInfo";
|
|
|
|
import ShippingZoneRates from "../ShippingZoneRates";
|
|
|
|
|
|
|
|
export interface FormData {
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ShippingZoneDetailsPageProps {
|
|
|
|
disabled: boolean;
|
|
|
|
errors: UserError[];
|
|
|
|
saveButtonBarState: ConfirmButtonTransitionState;
|
|
|
|
shippingZone: ShippingZoneDetailsFragment;
|
|
|
|
onBack: () => void;
|
|
|
|
onCountryAdd: () => void;
|
|
|
|
onCountryRemove: (code: string) => void;
|
|
|
|
onDelete: () => void;
|
|
|
|
onPriceRateAdd: () => void;
|
|
|
|
onPriceRateEdit: (id: string) => void;
|
|
|
|
onRateRemove: (rateId: string) => void;
|
|
|
|
onSubmit: (data: FormData) => void;
|
|
|
|
onWeightRateAdd: () => void;
|
|
|
|
onWeightRateEdit: (id: string) => void;
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:34:54 +00:00
|
|
|
const ShippingZoneDetailsPage: React.FC<ShippingZoneDetailsPageProps> = ({
|
2019-06-19 14:40:52 +00:00
|
|
|
disabled,
|
|
|
|
errors,
|
|
|
|
onBack,
|
|
|
|
onCountryAdd,
|
|
|
|
onCountryRemove,
|
|
|
|
onDelete,
|
|
|
|
onPriceRateAdd,
|
|
|
|
onPriceRateEdit,
|
|
|
|
onRateRemove,
|
|
|
|
onSubmit,
|
|
|
|
onWeightRateAdd,
|
|
|
|
onWeightRateEdit,
|
|
|
|
saveButtonBarState,
|
|
|
|
shippingZone
|
|
|
|
}) => {
|
2019-08-26 21:26:36 +00:00
|
|
|
const intl = useIntl();
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
const initialForm: FormData = {
|
2020-02-24 14:14:48 +00:00
|
|
|
name: shippingZone?.name || ""
|
2019-06-19 14:40:52 +00:00
|
|
|
};
|
|
|
|
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}>
|
|
|
|
<FormattedMessage defaultMessage="Shipping" />
|
|
|
|
</AppHeader>
|
2019-06-19 14:40:52 +00:00
|
|
|
<PageHeader title={maybe(() => shippingZone.name)} />
|
|
|
|
<Grid>
|
|
|
|
<div>
|
2020-02-24 14:14:48 +00:00
|
|
|
<ShippingZoneInfo data={data} errors={errors} onChange={change} />
|
2019-06-19 14:40:52 +00:00
|
|
|
<CardSpacer />
|
|
|
|
<CountryList
|
|
|
|
countries={maybe(() => shippingZone.countries)}
|
|
|
|
disabled={disabled}
|
|
|
|
emptyText={maybe(
|
|
|
|
() =>
|
|
|
|
shippingZone.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={onCountryAdd}
|
|
|
|
onCountryUnassign={onCountryRemove}
|
2019-08-26 21:26:36 +00:00
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Countries"
|
|
|
|
})}
|
2019-06-19 14:40:52 +00:00
|
|
|
/>
|
|
|
|
<CardSpacer />
|
|
|
|
<ShippingZoneRates
|
|
|
|
disabled={disabled}
|
|
|
|
onRateAdd={onPriceRateAdd}
|
|
|
|
onRateEdit={onPriceRateEdit}
|
|
|
|
onRateRemove={onRateRemove}
|
|
|
|
rates={maybe(() =>
|
|
|
|
shippingZone.shippingMethods.filter(
|
|
|
|
method => method.type === ShippingMethodTypeEnum.PRICE
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
variant="price"
|
|
|
|
/>
|
|
|
|
<CardSpacer />
|
|
|
|
<ShippingZoneRates
|
|
|
|
disabled={disabled}
|
|
|
|
onRateAdd={onWeightRateAdd}
|
|
|
|
onRateEdit={onWeightRateEdit}
|
|
|
|
onRateRemove={onRateRemove}
|
|
|
|
rates={maybe(() =>
|
|
|
|
shippingZone.shippingMethods.filter(
|
|
|
|
method => method.type === ShippingMethodTypeEnum.WEIGHT
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
variant="weight"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Grid>
|
|
|
|
<SaveButtonBar
|
|
|
|
disabled={disabled || !hasChanged}
|
|
|
|
onCancel={onBack}
|
|
|
|
onDelete={onDelete}
|
|
|
|
onSave={submit}
|
|
|
|
state={saveButtonBarState}
|
|
|
|
/>
|
|
|
|
</Container>
|
|
|
|
)}
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
ShippingZoneDetailsPage.displayName = "ShippingZoneDetailsPage";
|
|
|
|
export default ShippingZoneDetailsPage;
|