saleor-dashboard/src/shipping/components/ShippingZoneDetailsPage/ShippingZoneDetailsPage.tsx

188 lines
6.4 KiB
TypeScript
Raw Normal View History

2019-08-09 10:26:22 +00:00
import React from "react";
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 { 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";
import { maybe, getStringOrPlaceholder } from "../../../misc";
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
}
export interface ShippingZoneDetailsPageProps {
disabled: boolean;
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;
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
};
}
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,
2020-02-03 11:01:18 +00:00
shippingZone,
warehouses
2019-06-19 14:40:52 +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-03 11:01:18 +00:00
const [warehouseDisplayValues, setWarehouseDisplayValues] = React.useState(
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}
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>
<div>
<ShippingZoneWarehouses
data={data}
displayValue={warehouseDisplayValues}
hasMore={false}
loading={false}
onChange={handleWarehouseChange}
onFetchMore={() => undefined}
warehouses={warehouseChoices}
/>
</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;