280 lines
9.7 KiB
TypeScript
280 lines
9.7 KiB
TypeScript
import makeStyles from "@material-ui/core/styles/makeStyles";
|
|
import TextField from "@material-ui/core/TextField";
|
|
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 Metadata from "@saleor/components/Metadata/Metadata";
|
|
import { MetadataFormData } from "@saleor/components/Metadata/types";
|
|
import { MultiAutocompleteChoiceType } from "@saleor/components/MultiAutocompleteSelectField";
|
|
import PageHeader from "@saleor/components/PageHeader";
|
|
import SaveButtonBar from "@saleor/components/SaveButtonBar";
|
|
import { SingleAutocompleteChoiceType } from "@saleor/components/SingleAutocompleteSelectField";
|
|
import { ShippingErrorFragment } from "@saleor/fragments/types/ShippingErrorFragment";
|
|
import {
|
|
ShippingZoneDetailsFragment,
|
|
ShippingZoneDetailsFragment_warehouses
|
|
} from "@saleor/fragments/types/ShippingZoneDetailsFragment";
|
|
import { SubmitPromise } from "@saleor/hooks/useForm";
|
|
import useStateFromProps from "@saleor/hooks/useStateFromProps";
|
|
import createMultiAutocompleteSelectHandler from "@saleor/utils/handlers/multiAutocompleteSelectChangeHandler";
|
|
import { mapMetadataItemToInput } from "@saleor/utils/maps";
|
|
import useMetadataChangeTrigger from "@saleor/utils/metadata/useMetadataChangeTrigger";
|
|
import React from "react";
|
|
import { FormattedMessage, useIntl } from "react-intl";
|
|
|
|
import { getStringOrPlaceholder } from "../../../misc";
|
|
import { ChannelProps, FetchMoreProps, SearchProps } from "../../../types";
|
|
import { ShippingMethodTypeEnum } from "../../../types/globalTypes";
|
|
import ShippingZoneInfo from "../ShippingZoneInfo";
|
|
import ShippingZoneRates from "../ShippingZoneRates";
|
|
import ShippingZoneWarehouses from "../ShippingZoneWarehouses";
|
|
|
|
export interface FormData extends MetadataFormData {
|
|
name: string;
|
|
description: string;
|
|
warehouses: string[];
|
|
}
|
|
|
|
const maxDescriptionLength = 300;
|
|
|
|
const useStyles = makeStyles(
|
|
{
|
|
label: {
|
|
flex: 1
|
|
},
|
|
labelContainer: {
|
|
"& span": {
|
|
paddingRight: 30
|
|
},
|
|
display: "flex"
|
|
}
|
|
},
|
|
{ name: "ShippingZoneDetailsPage" }
|
|
);
|
|
|
|
export interface ShippingZoneDetailsPageProps
|
|
extends FetchMoreProps,
|
|
SearchProps,
|
|
ChannelProps {
|
|
disabled: boolean;
|
|
errors: ShippingErrorFragment[];
|
|
saveButtonBarState: ConfirmButtonTransitionState;
|
|
shippingZone: ShippingZoneDetailsFragment;
|
|
warehouses: ShippingZoneDetailsFragment_warehouses[];
|
|
onBack: () => void;
|
|
onCountryAdd: () => void;
|
|
onCountryRemove: (code: string) => void;
|
|
onDelete: () => void;
|
|
onPriceRateAdd: () => void;
|
|
onPriceRateEdit: (id: string) => void;
|
|
onRateRemove: (rateId: string) => void;
|
|
onSubmit: (data: FormData) => SubmitPromise;
|
|
onWarehouseAdd: () => void;
|
|
onWeightRateAdd: () => void;
|
|
onWeightRateEdit: (id: string) => void;
|
|
}
|
|
|
|
function warehouseToChoice(
|
|
warehouse: Record<"id" | "name", string>
|
|
): SingleAutocompleteChoiceType {
|
|
return {
|
|
label: warehouse.name,
|
|
value: warehouse.id
|
|
};
|
|
}
|
|
|
|
const ShippingZoneDetailsPage: React.FC<ShippingZoneDetailsPageProps> = ({
|
|
disabled,
|
|
errors,
|
|
hasMore,
|
|
loading,
|
|
onBack,
|
|
onCountryAdd,
|
|
onCountryRemove,
|
|
onDelete,
|
|
onFetchMore,
|
|
onPriceRateAdd,
|
|
onPriceRateEdit,
|
|
onRateRemove,
|
|
onSearchChange,
|
|
onSubmit,
|
|
onWarehouseAdd,
|
|
onWeightRateAdd,
|
|
onWeightRateEdit,
|
|
saveButtonBarState,
|
|
selectedChannelId,
|
|
shippingZone,
|
|
warehouses
|
|
}) => {
|
|
const intl = useIntl();
|
|
const classes = useStyles({});
|
|
|
|
const initialForm: FormData = {
|
|
description: shippingZone?.description || "",
|
|
metadata: shippingZone?.metadata.map(mapMetadataItemToInput),
|
|
name: shippingZone?.name || "",
|
|
privateMetadata: shippingZone?.privateMetadata.map(mapMetadataItemToInput),
|
|
warehouses: shippingZone?.warehouses?.map(warehouse => warehouse.id) || []
|
|
};
|
|
const [warehouseDisplayValues, setWarehouseDisplayValues] = useStateFromProps<
|
|
MultiAutocompleteChoiceType[]
|
|
>(
|
|
shippingZone?.warehouses?.map(warehouse => ({
|
|
label: warehouse.name,
|
|
value: warehouse.id
|
|
})) || []
|
|
);
|
|
|
|
const warehouseChoices = warehouses.map(warehouseToChoice);
|
|
|
|
const {
|
|
makeChangeHandler: makeMetadataChangeHandler
|
|
} = useMetadataChangeTrigger();
|
|
|
|
return (
|
|
<Form initial={initialForm} onSubmit={onSubmit}>
|
|
{({ change, data, hasChanged, submit, toggleValue }) => {
|
|
const handleWarehouseChange = createMultiAutocompleteSelectHandler(
|
|
toggleValue,
|
|
setWarehouseDisplayValues,
|
|
warehouseDisplayValues,
|
|
warehouseChoices
|
|
);
|
|
|
|
const changeMetadata = makeMetadataChangeHandler(change);
|
|
const description = data.description;
|
|
|
|
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"
|
|
})
|
|
)}
|
|
onCountryAssign={onCountryAdd}
|
|
onCountryUnassign={onCountryRemove}
|
|
title={intl.formatMessage({
|
|
defaultMessage: "Countries"
|
|
})}
|
|
/>
|
|
<CardSpacer />
|
|
<ShippingZoneRates
|
|
disabled={disabled}
|
|
onRateAdd={onPriceRateAdd}
|
|
onRateEdit={onPriceRateEdit}
|
|
onRateRemove={onRateRemove}
|
|
rates={shippingZone?.shippingMethods?.filter(
|
|
method => method.type === ShippingMethodTypeEnum.PRICE
|
|
)}
|
|
variant="price"
|
|
selectedChannelId={selectedChannelId}
|
|
/>
|
|
<CardSpacer />
|
|
<ShippingZoneRates
|
|
disabled={disabled}
|
|
onRateAdd={onWeightRateAdd}
|
|
onRateEdit={onWeightRateEdit}
|
|
onRateRemove={onRateRemove}
|
|
rates={shippingZone?.shippingMethods?.filter(
|
|
method => method.type === ShippingMethodTypeEnum.WEIGHT
|
|
)}
|
|
variant="weight"
|
|
selectedChannelId={selectedChannelId}
|
|
/>
|
|
<CardSpacer />
|
|
<Metadata data={data} onChange={changeMetadata} />
|
|
</div>
|
|
<div>
|
|
<ShippingZoneWarehouses
|
|
data={data}
|
|
displayValues={warehouseDisplayValues}
|
|
hasMore={hasMore}
|
|
loading={loading}
|
|
onChange={handleWarehouseChange}
|
|
onFetchMore={onFetchMore}
|
|
onSearchChange={onSearchChange}
|
|
onWarehouseAdd={onWarehouseAdd}
|
|
warehouses={warehouseChoices}
|
|
/>
|
|
</div>
|
|
<TextField
|
|
error={description.length > maxDescriptionLength}
|
|
name={"description"}
|
|
label={
|
|
<div className={classes.labelContainer}>
|
|
<div className={classes.label}>
|
|
<FormattedMessage defaultMessage="Description" />
|
|
</div>
|
|
{description?.length > 0 && (
|
|
<span>
|
|
<FormattedMessage
|
|
defaultMessage="{numberOfCharacters} of {maxCharacters} characters"
|
|
description="character limit"
|
|
values={{
|
|
maxCharacters: maxDescriptionLength,
|
|
numberOfCharacters: description.length
|
|
}}
|
|
/>
|
|
</span>
|
|
)}
|
|
</div>
|
|
}
|
|
InputProps={{
|
|
inputProps: {
|
|
maxLength: maxDescriptionLength
|
|
}
|
|
}}
|
|
value={description}
|
|
onChange={change}
|
|
disabled={loading || disabled}
|
|
fullWidth
|
|
multiline
|
|
placeholder={intl.formatMessage({
|
|
defaultMessage: "Description of a shipping zone."
|
|
})}
|
|
rows={10}
|
|
/>
|
|
</Grid>
|
|
<SaveButtonBar
|
|
disabled={disabled || !hasChanged}
|
|
onCancel={onBack}
|
|
onDelete={onDelete}
|
|
onSave={submit}
|
|
state={saveButtonBarState}
|
|
/>
|
|
</Container>
|
|
);
|
|
}}
|
|
</Form>
|
|
);
|
|
};
|
|
ShippingZoneDetailsPage.displayName = "ShippingZoneDetailsPage";
|
|
export default ShippingZoneDetailsPage;
|