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";
|
2020-03-17 17:41:47 +00:00
|
|
|
import { ShippingErrorFragment } from "@saleor/shipping/types/ShippingErrorFragment";
|
2020-02-03 11:01:18 +00:00
|
|
|
import createMultiAutocompleteSelectHandler from "@saleor/utils/handlers/multiAutocompleteSelectChangeHandler";
|
|
|
|
import { MultiAutocompleteChoiceType } from "@saleor/components/MultiAutocompleteSelectField";
|
2020-02-06 15:19:08 +00:00
|
|
|
import useStateFromProps from "@saleor/hooks/useStateFromProps";
|
2020-02-05 14:11:37 +00:00
|
|
|
import { maybe } from "../../../misc";
|
2020-02-11 15:05:09 +00:00
|
|
|
import { FetchMoreProps, SearchProps } from "../../../types";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { ShippingMethodTypeEnum } from "../../../types/globalTypes";
|
2020-02-03 11:01:18 +00:00
|
|
|
import {
|
|
|
|
ShippingZoneDetailsFragment,
|
|
|
|
ShippingZoneDetailsFragment_warehouses
|
|
|
|
} from "../../types/ShippingZoneDetailsFragment";
|
2019-06-19 14:40:52 +00:00
|
|
|
import ShippingZoneInfo from "../ShippingZoneInfo";
|
|
|
|
import ShippingZoneRates from "../ShippingZoneRates";
|
2020-02-03 11:01:18 +00:00
|
|
|
import ShippingZoneWarehouses from "../ShippingZoneWarehouses";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
export interface FormData {
|
|
|
|
name: string;
|
2020-02-03 11:01:18 +00:00
|
|
|
warehouses: string[];
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 15:05:09 +00:00
|
|
|
export interface ShippingZoneDetailsPageProps
|
|
|
|
extends FetchMoreProps,
|
|
|
|
SearchProps {
|
2019-06-19 14:40:52 +00:00
|
|
|
disabled: boolean;
|
2020-03-17 17:41:47 +00:00
|
|
|
errors: ShippingErrorFragment[];
|
2019-06-19 14:40:52 +00:00
|
|
|
saveButtonBarState: ConfirmButtonTransitionState;
|
|
|
|
shippingZone: ShippingZoneDetailsFragment;
|
2020-02-03 11:01:18 +00:00
|
|
|
warehouses: ShippingZoneDetailsFragment_warehouses[];
|
2019-06-19 14:40:52 +00:00
|
|
|
onBack: () => void;
|
|
|
|
onCountryAdd: () => void;
|
|
|
|
onCountryRemove: (code: string) => void;
|
|
|
|
onDelete: () => void;
|
|
|
|
onPriceRateAdd: () => void;
|
|
|
|
onPriceRateEdit: (id: string) => void;
|
|
|
|
onRateRemove: (rateId: string) => void;
|
|
|
|
onSubmit: (data: FormData) => void;
|
2020-02-10 16:07:17 +00:00
|
|
|
onWarehouseAdd: () => void;
|
2019-06-19 14:40:52 +00:00
|
|
|
onWeightRateAdd: () => void;
|
|
|
|
onWeightRateEdit: (id: string) => void;
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:01:18 +00:00
|
|
|
function warehouseToChoice(
|
|
|
|
warehouse: Record<"id" | "name", string>
|
|
|
|
): MultiAutocompleteChoiceType {
|
|
|
|
return {
|
|
|
|
label: warehouse.name,
|
|
|
|
value: warehouse.id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:34:54 +00:00
|
|
|
const ShippingZoneDetailsPage: React.FC<ShippingZoneDetailsPageProps> = ({
|
2019-06-19 14:40:52 +00:00
|
|
|
disabled,
|
|
|
|
errors,
|
2020-02-11 15:05:09 +00:00
|
|
|
hasMore,
|
|
|
|
loading,
|
2019-06-19 14:40:52 +00:00
|
|
|
onBack,
|
|
|
|
onCountryAdd,
|
|
|
|
onCountryRemove,
|
|
|
|
onDelete,
|
2020-02-11 15:05:09 +00:00
|
|
|
onFetchMore,
|
2019-06-19 14:40:52 +00:00
|
|
|
onPriceRateAdd,
|
|
|
|
onPriceRateEdit,
|
|
|
|
onRateRemove,
|
2020-02-11 15:05:09 +00:00
|
|
|
onSearchChange,
|
2019-06-19 14:40:52 +00:00
|
|
|
onSubmit,
|
2020-02-10 16:07:17 +00:00
|
|
|
onWarehouseAdd,
|
2019-06-19 14:40:52 +00:00
|
|
|
onWeightRateAdd,
|
|
|
|
onWeightRateEdit,
|
|
|
|
saveButtonBarState,
|
2020-02-03 11:01:18 +00:00
|
|
|
shippingZone,
|
|
|
|
warehouses
|
2019-06-19 14:40:52 +00:00
|
|
|
}) => {
|
2019-08-26 21:26:36 +00:00
|
|
|
const intl = useIntl();
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
const initialForm: FormData = {
|
2020-02-03 11:01:18 +00:00
|
|
|
name: shippingZone?.name || "",
|
|
|
|
warehouses: shippingZone?.warehouses.map(warehouse => warehouse.id) || []
|
2019-06-19 14:40:52 +00:00
|
|
|
};
|
2020-02-06 15:19:08 +00:00
|
|
|
const [warehouseDisplayValues, setWarehouseDisplayValues] = useStateFromProps(
|
2020-02-03 11:01:18 +00:00
|
|
|
shippingZone?.warehouses.map(warehouseToChoice)
|
|
|
|
);
|
|
|
|
|
|
|
|
const warehouseChoices = warehouses.map(warehouseToChoice);
|
2020-03-17 18:01:38 +00:00
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
return (
|
2020-02-24 14:14:48 +00:00
|
|
|
<Form initial={initialForm} onSubmit={onSubmit}>
|
2020-02-03 11:01:18 +00:00
|
|
|
{({ change, data, hasChanged, submit }) => {
|
|
|
|
const handleWarehouseChange = createMultiAutocompleteSelectHandler(
|
|
|
|
change,
|
|
|
|
setWarehouseDisplayValues,
|
|
|
|
warehouseDisplayValues,
|
|
|
|
warehouseChoices
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Container>
|
|
|
|
<AppHeader onBack={onBack}>
|
|
|
|
<FormattedMessage defaultMessage="Shipping" />
|
|
|
|
</AppHeader>
|
|
|
|
<PageHeader title={shippingZone?.name} />
|
|
|
|
<Grid>
|
|
|
|
<div>
|
|
|
|
<ShippingZoneInfo
|
|
|
|
data={data}
|
|
|
|
disabled={disabled}
|
|
|
|
errors={errors}
|
|
|
|
onChange={change}
|
|
|
|
/>
|
|
|
|
<CardSpacer />
|
|
|
|
<CountryList
|
|
|
|
countries={shippingZone?.countries}
|
|
|
|
disabled={disabled}
|
|
|
|
emptyText={maybe(
|
|
|
|
() =>
|
|
|
|
shippingZone.default
|
|
|
|
? 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"
|
|
|
|
}),
|
|
|
|
"..."
|
|
|
|
)}
|
|
|
|
onCountryAssign={onCountryAdd}
|
|
|
|
onCountryUnassign={onCountryRemove}
|
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Countries"
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<CardSpacer />
|
|
|
|
<ShippingZoneRates
|
|
|
|
disabled={disabled}
|
|
|
|
onRateAdd={onPriceRateAdd}
|
|
|
|
onRateEdit={onPriceRateEdit}
|
|
|
|
onRateRemove={onRateRemove}
|
2020-02-11 15:05:09 +00:00
|
|
|
rates={shippingZone?.shippingMethods?.filter(
|
|
|
|
method => method.type === ShippingMethodTypeEnum.PRICE
|
2020-02-03 11:01:18 +00:00
|
|
|
)}
|
|
|
|
variant="price"
|
|
|
|
/>
|
|
|
|
<CardSpacer />
|
|
|
|
<ShippingZoneRates
|
|
|
|
disabled={disabled}
|
|
|
|
onRateAdd={onWeightRateAdd}
|
|
|
|
onRateEdit={onWeightRateEdit}
|
|
|
|
onRateRemove={onRateRemove}
|
2020-02-11 15:05:09 +00:00
|
|
|
rates={shippingZone?.shippingMethods?.filter(
|
|
|
|
method => method.type === ShippingMethodTypeEnum.WEIGHT
|
2020-02-03 11:01:18 +00:00
|
|
|
)}
|
|
|
|
variant="weight"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<ShippingZoneWarehouses
|
|
|
|
data={data}
|
|
|
|
displayValue={warehouseDisplayValues}
|
2020-02-11 15:05:09 +00:00
|
|
|
hasMore={hasMore}
|
|
|
|
loading={loading}
|
2020-02-03 11:01:18 +00:00
|
|
|
onChange={handleWarehouseChange}
|
2020-02-11 15:05:09 +00:00
|
|
|
onFetchMore={onFetchMore}
|
|
|
|
onSearchChange={onSearchChange}
|
2020-02-10 16:07:17 +00:00
|
|
|
onWarehouseAdd={onWarehouseAdd}
|
2020-02-11 15:05:09 +00:00
|
|
|
warehouses={warehouseChoices}
|
2020-02-03 11:01:18 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Grid>
|
|
|
|
<SaveButtonBar
|
|
|
|
disabled={disabled || !hasChanged}
|
|
|
|
onCancel={onBack}
|
|
|
|
onDelete={onDelete}
|
|
|
|
onSave={submit}
|
|
|
|
state={saveButtonBarState}
|
|
|
|
/>
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}}
|
2019-06-19 14:40:52 +00:00
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
ShippingZoneDetailsPage.displayName = "ShippingZoneDetailsPage";
|
|
|
|
export default ShippingZoneDetailsPage;
|