2019-06-19 14:40:52 +00:00
|
|
|
import Button from "@material-ui/core/Button";
|
|
|
|
import Dialog from "@material-ui/core/Dialog";
|
|
|
|
import DialogActions from "@material-ui/core/DialogActions";
|
|
|
|
import DialogContent from "@material-ui/core/DialogContent";
|
|
|
|
import DialogTitle from "@material-ui/core/DialogTitle";
|
2019-10-30 14:34:24 +00:00
|
|
|
import { makeStyles } from "@material-ui/core/styles";
|
2019-06-19 14:40:52 +00:00
|
|
|
import TextField from "@material-ui/core/TextField";
|
|
|
|
import Typography from "@material-ui/core/Typography";
|
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 ConfirmButton, {
|
|
|
|
ConfirmButtonTransitionState
|
|
|
|
} from "@saleor/components/ConfirmButton";
|
2019-09-09 09:28:06 +00:00
|
|
|
import ControlledCheckbox from "@saleor/components/ControlledCheckbox";
|
2019-06-19 14:40:52 +00:00
|
|
|
import Form from "@saleor/components/Form";
|
|
|
|
import FormSpacer from "@saleor/components/FormSpacer";
|
|
|
|
import Hr from "@saleor/components/Hr";
|
|
|
|
import Skeleton from "@saleor/components/Skeleton";
|
2019-08-26 21:26:36 +00:00
|
|
|
import { buttonMessages } from "@saleor/intl";
|
2020-02-24 14:14:48 +00:00
|
|
|
import { getFieldError } from "@saleor/utils/errors";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { maybe } from "../../../misc";
|
2020-02-24 14:14:48 +00:00
|
|
|
import { UserError } from "../../../types";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { ShippingMethodTypeEnum } from "../../../types/globalTypes";
|
|
|
|
import { ShippingZoneDetailsFragment_shippingMethods } from "../../types/ShippingZoneDetailsFragment";
|
|
|
|
|
|
|
|
export interface FormData {
|
|
|
|
name: string;
|
|
|
|
noLimits: boolean;
|
|
|
|
minValue: string;
|
|
|
|
maxValue: string;
|
|
|
|
isFree: boolean;
|
|
|
|
price: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ShippingZoneRateDialogProps {
|
|
|
|
action: "create" | "edit";
|
|
|
|
confirmButtonState: ConfirmButtonTransitionState;
|
|
|
|
defaultCurrency: string;
|
|
|
|
disabled: boolean;
|
|
|
|
errors: UserError[];
|
|
|
|
open: boolean;
|
|
|
|
rate: ShippingZoneDetailsFragment_shippingMethods;
|
|
|
|
variant: ShippingMethodTypeEnum;
|
|
|
|
onClose: () => void;
|
|
|
|
onSubmit: (data: FormData) => void;
|
|
|
|
}
|
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const useStyles = makeStyles(
|
|
|
|
theme => ({
|
2019-06-19 14:40:52 +00:00
|
|
|
grid: {
|
|
|
|
display: "grid",
|
2019-10-28 16:16:49 +00:00
|
|
|
gridColumnGap: theme.spacing(2),
|
2019-06-19 14:40:52 +00:00
|
|
|
gridTemplateColumns: "1fr 1fr"
|
|
|
|
},
|
|
|
|
subheading: {
|
2019-10-28 16:16:49 +00:00
|
|
|
marginBottom: theme.spacing(2),
|
|
|
|
marginTop: theme.spacing(2)
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
2019-10-30 14:34:24 +00:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
name: "ShippingZoneRateDialog"
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const ShippingZoneRateDialog: React.FC<ShippingZoneRateDialogProps> = props => {
|
|
|
|
const {
|
2019-06-19 14:40:52 +00:00
|
|
|
action,
|
2019-10-30 14:34:24 +00:00
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
confirmButtonState,
|
|
|
|
defaultCurrency,
|
|
|
|
disabled,
|
|
|
|
errors,
|
|
|
|
onClose,
|
|
|
|
onSubmit,
|
|
|
|
open,
|
|
|
|
rate,
|
|
|
|
variant
|
2019-10-30 14:34:24 +00:00
|
|
|
} = props;
|
2019-08-26 21:26:36 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const classes = useStyles(props);
|
|
|
|
const intl = useIntl();
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const initialForm: FormData =
|
|
|
|
action === "create"
|
|
|
|
? {
|
|
|
|
isFree: false,
|
|
|
|
maxValue: "",
|
|
|
|
minValue: "",
|
|
|
|
name: "",
|
|
|
|
noLimits: false,
|
|
|
|
price: ""
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
isFree: maybe(() => rate.price.amount === 0, false),
|
|
|
|
maxValue:
|
|
|
|
variant === ShippingMethodTypeEnum.PRICE
|
|
|
|
? maybe(() => rate.maximumOrderPrice.amount.toString(), "")
|
|
|
|
: maybe(() => rate.maximumOrderWeight.value.toString(), ""),
|
|
|
|
minValue:
|
|
|
|
variant === ShippingMethodTypeEnum.PRICE
|
|
|
|
? maybe(() => rate.minimumOrderPrice.amount.toString(), "")
|
|
|
|
: maybe(() => rate.minimumOrderWeight.value.toString(), ""),
|
|
|
|
name: maybe(() => rate.name, ""),
|
|
|
|
noLimits: false,
|
|
|
|
price: maybe(() => rate.price.amount.toString(), "")
|
|
|
|
};
|
|
|
|
if (action === "edit") {
|
|
|
|
initialForm.noLimits = !initialForm.maxValue && !initialForm.minValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog onClose={onClose} open={open} fullWidth maxWidth="sm">
|
2020-02-24 14:14:48 +00:00
|
|
|
<Form initial={initialForm} onSubmit={onSubmit}>
|
|
|
|
{({ change, data, hasChanged }) => (
|
|
|
|
<>
|
|
|
|
<DialogTitle>
|
|
|
|
{variant === ShippingMethodTypeEnum.PRICE
|
|
|
|
? action === "create"
|
2019-10-30 14:34:24 +00:00
|
|
|
? intl.formatMessage({
|
2020-02-24 14:14:48 +00:00
|
|
|
defaultMessage: "Add Price Rate",
|
|
|
|
description: "dialog header"
|
2019-10-30 14:34:24 +00:00
|
|
|
})
|
|
|
|
: intl.formatMessage({
|
2020-02-24 14:14:48 +00:00
|
|
|
defaultMessage: "Edit Price Rate",
|
|
|
|
description: "dialog header"
|
2019-10-30 14:34:24 +00:00
|
|
|
})
|
2020-02-24 14:14:48 +00:00
|
|
|
: action === "create"
|
|
|
|
? intl.formatMessage({
|
|
|
|
defaultMessage: "Add Weight Rate",
|
|
|
|
description:
|
|
|
|
"add weight based shipping method, dialog header"
|
|
|
|
})
|
|
|
|
: intl.formatMessage({
|
|
|
|
defaultMessage: "Edit Weight Rate",
|
|
|
|
description:
|
|
|
|
"edit weight based shipping method, dialog header"
|
2019-10-30 14:34:24 +00:00
|
|
|
})}
|
2020-02-24 14:14:48 +00:00
|
|
|
</DialogTitle>
|
|
|
|
<DialogContent>
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
|
|
|
error={!!getFieldError(errors, "name")}
|
|
|
|
fullWidth
|
|
|
|
helperText={
|
|
|
|
getFieldError(errors, "name") ||
|
|
|
|
intl.formatMessage({
|
|
|
|
defaultMessage:
|
|
|
|
"This will be shown to customers at checkout"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Rate Name",
|
|
|
|
description: "shipping method name"
|
|
|
|
})}
|
|
|
|
name={"name" as keyof FormData}
|
|
|
|
value={data.name}
|
|
|
|
onChange={change}
|
|
|
|
/>
|
|
|
|
</DialogContent>
|
|
|
|
<Hr />
|
|
|
|
<DialogContent>
|
|
|
|
{!!variant ? (
|
|
|
|
<>
|
|
|
|
<Typography
|
|
|
|
className={classes.subheading}
|
|
|
|
variant="subtitle1"
|
|
|
|
>
|
|
|
|
{variant === ShippingMethodTypeEnum.PRICE
|
|
|
|
? intl.formatMessage({
|
|
|
|
defaultMessage: "Value range",
|
|
|
|
description: "order price range"
|
|
|
|
})
|
|
|
|
: intl.formatMessage({
|
|
|
|
defaultMessage: "Weight range",
|
|
|
|
description: "order weight range"
|
|
|
|
})}
|
|
|
|
</Typography>
|
|
|
|
<ControlledCheckbox
|
|
|
|
name={"noLimits" as keyof FormData}
|
|
|
|
label={
|
2019-10-30 14:34:24 +00:00
|
|
|
<>
|
2020-02-24 14:14:48 +00:00
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="There are no value limits"
|
|
|
|
description="shipping method has no value limits"
|
|
|
|
/>
|
|
|
|
<Typography variant="caption">
|
|
|
|
{variant === ShippingMethodTypeEnum.PRICE
|
|
|
|
? intl.formatMessage({
|
|
|
|
defaultMessage:
|
|
|
|
"This rate will apply to all orders of all prices"
|
|
|
|
})
|
|
|
|
: intl.formatMessage({
|
|
|
|
defaultMessage:
|
|
|
|
"This rate will apply to all orders of all weights"
|
|
|
|
})}
|
|
|
|
</Typography>
|
2019-10-30 14:34:24 +00:00
|
|
|
</>
|
2020-02-24 14:14:48 +00:00
|
|
|
}
|
|
|
|
checked={data.noLimits}
|
|
|
|
onChange={change}
|
|
|
|
disabled={disabled}
|
2019-06-19 14:40:52 +00:00
|
|
|
/>
|
2020-02-24 14:14:48 +00:00
|
|
|
{!data.noLimits && (
|
|
|
|
<>
|
|
|
|
<FormSpacer />
|
|
|
|
<div className={classes.grid}>
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
|
|
|
error={
|
|
|
|
variant === ShippingMethodTypeEnum.PRICE
|
|
|
|
? !!getFieldError(errors, "minimumOrderPrice")
|
|
|
|
: !!getFieldError(errors, "minimumOrderWeight")
|
|
|
|
}
|
|
|
|
fullWidth
|
|
|
|
helperText={
|
|
|
|
variant === ShippingMethodTypeEnum.PRICE
|
|
|
|
? getFieldError(errors, "minimumOrderPrice")
|
|
|
|
: getFieldError(errors, "minimumOrderWeight")
|
|
|
|
}
|
|
|
|
label={
|
|
|
|
variant === ShippingMethodTypeEnum.PRICE
|
|
|
|
? getFieldError(errors, "minimumOrderPrice") ||
|
|
|
|
intl.formatMessage({
|
|
|
|
defaultMessage: "Minimal Order Value"
|
|
|
|
})
|
|
|
|
: getFieldError(errors, "minimumOrderWeight") ||
|
|
|
|
intl.formatMessage({
|
|
|
|
defaultMessage: "Minimal Order Weight"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
name={"minValue" as keyof FormData}
|
|
|
|
type="number"
|
|
|
|
value={data.minValue}
|
|
|
|
onChange={change}
|
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
|
|
|
error={
|
|
|
|
variant === ShippingMethodTypeEnum.PRICE
|
|
|
|
? !!getFieldError(errors, "maximumOrderPrice")
|
|
|
|
: !!getFieldError(errors, "maximumOrderWeight")
|
|
|
|
}
|
|
|
|
fullWidth
|
|
|
|
helperText={
|
|
|
|
variant === ShippingMethodTypeEnum.PRICE
|
|
|
|
? getFieldError(errors, "maximumOrderPrice")
|
|
|
|
: getFieldError(errors, "maximumOrderWeight")
|
|
|
|
}
|
|
|
|
label={
|
|
|
|
variant === ShippingMethodTypeEnum.PRICE
|
|
|
|
? getFieldError(errors, "maximumOrderPrice") ||
|
|
|
|
intl.formatMessage({
|
|
|
|
defaultMessage: "Maximal Order Value"
|
|
|
|
})
|
|
|
|
: getFieldError(errors, "maximumOrderWeight") ||
|
|
|
|
intl.formatMessage({
|
|
|
|
defaultMessage: "Maximal Order Weight"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
name={"maxValue" as keyof FormData}
|
|
|
|
type="number"
|
|
|
|
value={data.maxValue}
|
|
|
|
onChange={change}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<Skeleton />
|
|
|
|
)}
|
|
|
|
</DialogContent>
|
|
|
|
<Hr />
|
|
|
|
<DialogContent>
|
|
|
|
<Typography className={classes.subheading} variant="subtitle1">
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Rate"
|
|
|
|
description="shipping method"
|
2019-10-30 14:34:24 +00:00
|
|
|
/>
|
2020-02-24 14:14:48 +00:00
|
|
|
</Typography>
|
|
|
|
<ControlledCheckbox
|
|
|
|
name={"isFree" as keyof FormData}
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "This is free shipping",
|
|
|
|
description: "shipping method, switch button"
|
|
|
|
})}
|
|
|
|
checked={data.isFree}
|
|
|
|
onChange={change}
|
|
|
|
disabled={disabled}
|
|
|
|
/>
|
|
|
|
{!data.isFree && (
|
|
|
|
<>
|
|
|
|
<FormSpacer />
|
|
|
|
<div className={classes.grid}>
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
|
|
|
error={!!getFieldError(errors, "price")}
|
|
|
|
fullWidth
|
|
|
|
helperText={getFieldError(errors, "price")?.message}
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Rate Price",
|
|
|
|
description: "shipping method price"
|
2019-10-30 14:34:24 +00:00
|
|
|
})}
|
2020-02-24 14:14:48 +00:00
|
|
|
name={"price" as keyof FormData}
|
|
|
|
type="number"
|
|
|
|
value={data.price}
|
|
|
|
onChange={change}
|
|
|
|
InputProps={{
|
|
|
|
endAdornment: defaultCurrency
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
|
|
|
<Button onClick={onClose}>
|
|
|
|
<FormattedMessage {...buttonMessages.back} />
|
|
|
|
</Button>
|
|
|
|
<ConfirmButton
|
|
|
|
disabled={disabled || !hasChanged}
|
|
|
|
transitionState={confirmButtonState}
|
|
|
|
color="primary"
|
|
|
|
variant="contained"
|
|
|
|
type="submit"
|
|
|
|
>
|
|
|
|
{action === "create"
|
|
|
|
? intl.formatMessage({
|
|
|
|
defaultMessage: "Create rate",
|
|
|
|
description: "button"
|
|
|
|
})
|
|
|
|
: intl.formatMessage({
|
|
|
|
defaultMessage: "Update rate",
|
|
|
|
description: "button"
|
|
|
|
})}
|
|
|
|
</ConfirmButton>
|
|
|
|
</DialogActions>
|
|
|
|
</>
|
|
|
|
)}
|
2019-10-30 14:34:24 +00:00
|
|
|
</Form>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
ShippingZoneRateDialog.displayName = "ShippingZoneRateDialog";
|
|
|
|
export default ShippingZoneRateDialog;
|