2021-05-14 08:15:15 +00:00
|
|
|
import {
|
|
|
|
Dialog,
|
|
|
|
DialogActions,
|
|
|
|
DialogContent,
|
|
|
|
DialogContentText,
|
2022-01-24 13:50:17 +00:00
|
|
|
DialogTitle,
|
|
|
|
Typography
|
2021-05-14 08:15:15 +00:00
|
|
|
} from "@material-ui/core";
|
2022-01-28 12:34:20 +00:00
|
|
|
import BackButton from "@saleor/components/BackButton";
|
|
|
|
import ConfirmButton from "@saleor/components/ConfirmButton";
|
2019-06-19 14:40:52 +00:00
|
|
|
import Form from "@saleor/components/Form";
|
2020-05-14 09:30:32 +00:00
|
|
|
import FormSpacer from "@saleor/components/FormSpacer";
|
2019-06-19 14:40:52 +00:00
|
|
|
import Money from "@saleor/components/Money";
|
|
|
|
import { SingleSelectField } from "@saleor/components/SingleSelectField";
|
2022-03-09 08:56:55 +00:00
|
|
|
import { OrderDetailsFragment, OrderErrorFragment } from "@saleor/graphql";
|
2020-05-14 09:30:32 +00:00
|
|
|
import useModalDialogErrors from "@saleor/hooks/useModalDialogErrors";
|
2019-08-26 17:44:42 +00:00
|
|
|
import { buttonMessages } from "@saleor/intl";
|
2022-01-28 12:34:20 +00:00
|
|
|
import { ConfirmButtonTransitionState, makeStyles } from "@saleor/macaw-ui";
|
2020-03-16 12:28:52 +00:00
|
|
|
import { getFormErrors } from "@saleor/utils/errors";
|
|
|
|
import getOrderErrorMessage from "@saleor/utils/errors/order";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
|
|
|
import { FormattedMessage, useIntl } from "react-intl";
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
export interface FormData {
|
|
|
|
shippingMethod: string;
|
|
|
|
}
|
|
|
|
|
2019-12-03 15:28:40 +00:00
|
|
|
const useStyles = makeStyles(
|
|
|
|
theme => ({
|
|
|
|
dialog: {
|
|
|
|
overflowY: "visible"
|
|
|
|
},
|
|
|
|
menuItem: {
|
|
|
|
display: "flex",
|
2022-01-24 13:50:17 +00:00
|
|
|
width: "100%",
|
|
|
|
flexWrap: "wrap"
|
2019-12-03 15:28:40 +00:00
|
|
|
},
|
|
|
|
price: {
|
|
|
|
marginRight: theme.spacing(3)
|
|
|
|
},
|
|
|
|
root: {
|
|
|
|
overflowY: "visible",
|
2021-08-23 09:16:35 +00:00
|
|
|
width: theme.breakpoints.values.sm,
|
|
|
|
margin: 0,
|
|
|
|
padding: theme.spacing(3)
|
2019-12-03 15:28:40 +00:00
|
|
|
},
|
|
|
|
shippingMethodName: {
|
|
|
|
flex: 1,
|
|
|
|
overflowX: "hidden",
|
|
|
|
textOverflow: "ellipsis"
|
2022-01-24 13:50:17 +00:00
|
|
|
},
|
|
|
|
message: {
|
|
|
|
width: "100%"
|
2019-12-03 15:28:40 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
{ name: "OrderShippingMethodEditDialog" }
|
|
|
|
);
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-03-16 12:28:52 +00:00
|
|
|
export interface OrderShippingMethodEditDialogProps {
|
2019-06-19 14:40:52 +00:00
|
|
|
confirmButtonState: ConfirmButtonTransitionState;
|
2020-03-16 12:28:52 +00:00
|
|
|
errors: OrderErrorFragment[];
|
2019-06-19 14:40:52 +00:00
|
|
|
open: boolean;
|
|
|
|
shippingMethod: string;
|
2022-03-09 08:56:55 +00:00
|
|
|
shippingMethods?: OrderDetailsFragment["shippingMethods"];
|
2019-06-19 14:40:52 +00:00
|
|
|
onClose();
|
|
|
|
onSubmit?(data: FormData);
|
|
|
|
}
|
|
|
|
|
2020-03-16 12:28:52 +00:00
|
|
|
const OrderShippingMethodEditDialog: React.FC<OrderShippingMethodEditDialogProps> = props => {
|
2019-10-30 14:34:24 +00:00
|
|
|
const {
|
2019-06-19 14:40:52 +00:00
|
|
|
confirmButtonState,
|
2020-03-16 12:28:52 +00:00
|
|
|
errors: apiErrors,
|
2019-06-19 14:40:52 +00:00
|
|
|
open,
|
|
|
|
shippingMethod,
|
|
|
|
shippingMethods,
|
|
|
|
onClose,
|
|
|
|
onSubmit
|
2019-10-30 14:34:24 +00:00
|
|
|
} = props;
|
|
|
|
const classes = useStyles(props);
|
2020-03-16 12:28:52 +00:00
|
|
|
const errors = useModalDialogErrors(apiErrors, open);
|
|
|
|
const intl = useIntl();
|
|
|
|
|
|
|
|
const formFields = ["shippingMethod"];
|
|
|
|
const formErrors = getFormErrors(formFields, errors);
|
|
|
|
const nonFieldErrors = errors.filter(err => !formFields.includes(err.field));
|
2019-10-30 14:34:24 +00:00
|
|
|
|
|
|
|
const choices = shippingMethods
|
2022-01-24 13:50:17 +00:00
|
|
|
? shippingMethods
|
|
|
|
.map(s => ({
|
|
|
|
label: (
|
|
|
|
<div className={classes.menuItem}>
|
|
|
|
<span className={classes.shippingMethodName}>{s.name}</span>
|
|
|
|
|
|
|
|
<span className={classes.price}>
|
|
|
|
<Money money={s.price} />
|
|
|
|
</span>
|
|
|
|
{!s.active && (
|
|
|
|
<Typography className={classes.message} variant="caption">
|
|
|
|
{s.message}
|
|
|
|
</Typography>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
disabled: !s.active,
|
|
|
|
value: s.id
|
|
|
|
}))
|
|
|
|
.sort((x, y) => (x.disabled === y.disabled ? 0 : x.disabled ? 1 : -1))
|
2019-10-30 14:34:24 +00:00
|
|
|
: [];
|
|
|
|
const initialForm: FormData = {
|
|
|
|
shippingMethod
|
|
|
|
};
|
2020-03-16 12:28:52 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
return (
|
|
|
|
<Dialog onClose={onClose} open={open} classes={{ paper: classes.dialog }}>
|
|
|
|
<DialogTitle>
|
|
|
|
<FormattedMessage
|
2022-05-05 07:54:28 +00:00
|
|
|
id="V/YxJa"
|
2019-10-30 14:34:24 +00:00
|
|
|
defaultMessage="Edit Shipping Method"
|
|
|
|
description="dialog header"
|
|
|
|
/>
|
|
|
|
</DialogTitle>
|
|
|
|
<Form initial={initialForm} onSubmit={onSubmit}>
|
|
|
|
{({ change, data }) => (
|
|
|
|
<>
|
|
|
|
<DialogContent className={classes.root}>
|
|
|
|
<SingleSelectField
|
|
|
|
choices={choices}
|
2020-03-16 12:28:52 +00:00
|
|
|
error={!!formErrors.shippingMethod}
|
|
|
|
hint={getOrderErrorMessage(formErrors.shippingMethod, intl)}
|
2019-10-30 14:34:24 +00:00
|
|
|
name="shippingMethod"
|
|
|
|
value={data.shippingMethod}
|
|
|
|
onChange={change}
|
|
|
|
/>
|
2020-03-16 12:28:52 +00:00
|
|
|
{nonFieldErrors.length > 0 && (
|
|
|
|
<>
|
|
|
|
<FormSpacer />
|
2020-07-02 10:59:09 +00:00
|
|
|
{nonFieldErrors.map((err, index) => (
|
|
|
|
<DialogContentText color="error" key={index}>
|
2020-03-16 12:28:52 +00:00
|
|
|
{getOrderErrorMessage(err, intl)}
|
|
|
|
</DialogContentText>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)}
|
2019-10-30 14:34:24 +00:00
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
2022-01-28 12:34:20 +00:00
|
|
|
<BackButton onClick={onClose} />
|
2019-10-30 14:34:24 +00:00
|
|
|
<ConfirmButton
|
|
|
|
transitionState={confirmButtonState}
|
|
|
|
type="submit"
|
2021-08-23 09:16:35 +00:00
|
|
|
disabled={!data.shippingMethod}
|
2019-10-30 14:34:24 +00:00
|
|
|
>
|
|
|
|
<FormattedMessage {...buttonMessages.confirm} />
|
|
|
|
</ConfirmButton>
|
|
|
|
</DialogActions>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Form>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
OrderShippingMethodEditDialog.displayName = "OrderShippingMethodEditDialog";
|
|
|
|
export default OrderShippingMethodEditDialog;
|