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

195 lines
6.6 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";
import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
import { SingleAutocompleteChoiceType } from "@saleor/components/SingleAutocompleteSelectField";
2020-02-06 15:19:08 +00:00
import useStateFromProps from "@saleor/hooks/useStateFromProps";
import { getStringOrPlaceholder } 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;
warehouse: 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;
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>
): SingleAutocompleteChoiceType {
2020-02-03 11:01:18 +00:00
return {
label: warehouse.name,
value: warehouse.id
};
}
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
}) => {
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 || "",
warehouse: shippingZone?.warehouses[0]?.id || null
2019-06-19 14:40:52 +00:00
};
const [warehouseDisplayValue, setWarehouseDisplayValue] = useStateFromProps(
shippingZone?.warehouses[0]?.name || ""
2020-02-03 11:01:18 +00:00
);
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 = createSingleAutocompleteSelectHandler(
2020-02-03 11:01:18 +00:00
change,
setWarehouseDisplayValue,
2020-02-03 11:01:18 +00:00
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={getStringOrPlaceholder(
shippingZone?.default === undefined
? undefined
: 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"
})
2020-02-03 11:01:18 +00:00
)}
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={warehouseDisplayValue}
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;