saleor-dashboard/src/orders/components/OrderProductAddDialog/OrderProductAddDialog.tsx

371 lines
12 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Button from "@material-ui/core/Button";
import CircularProgress from "@material-ui/core/CircularProgress";
import Dialog from "@material-ui/core/Dialog";
import DialogActions from "@material-ui/core/DialogActions";
import DialogContent from "@material-ui/core/DialogContent";
2020-03-16 12:28:52 +00:00
import DialogContentText from "@material-ui/core/DialogContentText";
2019-06-19 14:40:52 +00:00
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 TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableRow from "@material-ui/core/TableRow";
import TextField from "@material-ui/core/TextField";
import Checkbox from "@saleor/components/Checkbox";
import ConfirmButton, {
ConfirmButtonTransitionState
} from "@saleor/components/ConfirmButton";
import FormSpacer from "@saleor/components/FormSpacer";
2019-06-19 14:40:52 +00:00
import Money from "@saleor/components/Money";
2019-11-04 14:25:23 +00:00
import ResponsiveTable from "@saleor/components/ResponsiveTable";
2019-06-19 14:40:52 +00:00
import TableCellAvatar from "@saleor/components/TableCellAvatar";
import { OrderErrorFragment } from "@saleor/fragments/types/OrderErrorFragment";
import useModalDialogErrors from "@saleor/hooks/useModalDialogErrors";
import useModalDialogOpen from "@saleor/hooks/useModalDialogOpen";
2019-08-09 11:14:35 +00:00
import useSearchQuery from "@saleor/hooks/useSearchQuery";
import { buttonMessages } from "@saleor/intl";
2019-08-09 11:14:35 +00:00
import { maybe, renderCollection } from "@saleor/misc";
import { FetchMoreProps } from "@saleor/types";
2020-03-16 12:28:52 +00:00
import getOrderErrorMessage from "@saleor/utils/errors/order";
import React from "react";
import InfiniteScroll from "react-infinite-scroller";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import {
2019-10-15 12:17:35 +00:00
SearchOrderVariant_search_edges_node,
SearchOrderVariant_search_edges_node_variants
2019-06-19 14:40:52 +00:00
} from "../../types/SearchOrderVariant";
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
avatar: {
2020-09-25 14:49:21 +00:00
paddingLeft: 0,
width: 64
2019-12-03 15:28:40 +00:00
},
colName: {
paddingLeft: 0
},
colVariantCheckbox: {
padding: 0
},
content: {
overflowY: "scroll"
},
grayText: {
color: theme.palette.text.disabled
},
loadMoreLoaderContainer: {
alignItems: "center",
display: "flex",
height: theme.spacing(3),
2020-03-16 12:28:52 +00:00
justifyContent: "center",
marginTop: theme.spacing(3)
2019-12-03 15:28:40 +00:00
},
overflow: {
overflowY: "visible"
},
productCheckboxCell: {
"&:first-child": {
paddingLeft: 0,
paddingRight: 0
}
},
textRight: {
textAlign: "right"
},
variantCheckbox: {
left: theme.spacing(),
position: "relative"
},
wideCell: {
width: "100%"
2019-06-19 14:40:52 +00:00
}
2019-12-03 15:28:40 +00:00
}),
{ name: "OrderProductAddDialog" }
);
2019-06-19 14:40:52 +00:00
2019-08-09 11:14:35 +00:00
type SetVariantsAction = (
2019-10-15 12:17:35 +00:00
data: SearchOrderVariant_search_edges_node_variants[]
2019-08-09 11:14:35 +00:00
) => void;
2020-03-16 12:28:52 +00:00
export interface OrderProductAddDialogProps extends FetchMoreProps {
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;
2019-10-15 12:17:35 +00:00
products: SearchOrderVariant_search_edges_node[];
2019-06-19 14:40:52 +00:00
onClose: () => void;
2019-08-12 14:11:10 +00:00
onFetch: (query: string) => void;
2019-10-15 12:17:35 +00:00
onSubmit: (data: SearchOrderVariant_search_edges_node_variants[]) => void;
2019-06-19 14:40:52 +00:00
}
function hasAllVariantsSelected(
2019-10-15 12:17:35 +00:00
productVariants: SearchOrderVariant_search_edges_node_variants[],
selectedVariantsToProductsMap: SearchOrderVariant_search_edges_node_variants[]
2019-06-19 14:40:52 +00:00
): boolean {
return productVariants.reduce(
(acc, productVariant) =>
acc &&
2019-08-09 11:14:35 +00:00
!!selectedVariantsToProductsMap.find(
2019-06-19 14:40:52 +00:00
selectedVariant => selectedVariant.id === productVariant.id
),
true
);
}
function isVariantSelected(
2019-10-15 12:17:35 +00:00
variant: SearchOrderVariant_search_edges_node_variants,
selectedVariantsToProductsMap: SearchOrderVariant_search_edges_node_variants[]
2019-06-19 14:40:52 +00:00
): boolean {
2019-08-09 11:14:35 +00:00
return !!selectedVariantsToProductsMap.find(
2019-06-19 14:40:52 +00:00
selectedVariant => selectedVariant.id === variant.id
);
}
2019-08-09 11:14:35 +00:00
const onProductAdd = (
2019-10-15 12:17:35 +00:00
product: SearchOrderVariant_search_edges_node,
2019-08-09 11:14:35 +00:00
productIndex: number,
productsWithAllVariantsSelected: boolean[],
2019-10-15 12:17:35 +00:00
variants: SearchOrderVariant_search_edges_node_variants[],
2019-08-09 11:14:35 +00:00
setVariants: SetVariantsAction
) =>
productsWithAllVariantsSelected[productIndex]
? setVariants(
variants.filter(
selectedVariant =>
!product.variants.find(
productVariant => productVariant.id === selectedVariant.id
)
)
)
: setVariants([
...variants,
...product.variants.filter(
productVariant =>
!variants.find(
selectedVariant => selectedVariant.id === productVariant.id
)
)
]);
const onVariantAdd = (
2019-10-15 12:17:35 +00:00
variant: SearchOrderVariant_search_edges_node_variants,
2019-08-09 11:14:35 +00:00
variantIndex: number,
productIndex: number,
2019-10-15 12:17:35 +00:00
variants: SearchOrderVariant_search_edges_node_variants[],
2019-08-09 11:14:35 +00:00
selectedVariantsToProductsMap: boolean[][],
setVariants: SetVariantsAction
) =>
selectedVariantsToProductsMap[productIndex][variantIndex]
? setVariants(
variants.filter(selectedVariant => selectedVariant.id !== variant.id)
)
: setVariants([...variants, variant]);
2019-10-30 14:34:24 +00:00
const OrderProductAddDialog: React.FC<OrderProductAddDialogProps> = props => {
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,
loading,
hasMore,
products,
onFetch,
onFetchMore,
onClose,
onSubmit
2019-10-30 14:34:24 +00:00
} = props;
2019-06-19 14:40:52 +00:00
2020-03-16 12:28:52 +00:00
const classes = useStyles(props);
2019-10-30 14:34:24 +00:00
const intl = useIntl();
const [query, onQueryChange] = useSearchQuery(onFetch);
const [variants, setVariants] = React.useState<
SearchOrderVariant_search_edges_node_variants[]
>([]);
2020-03-16 12:28:52 +00:00
const errors = useModalDialogErrors(apiErrors, open);
2019-06-19 14:40:52 +00:00
2020-03-24 20:09:50 +00:00
useModalDialogOpen(open, {
onClose: () => setVariants([])
});
2019-10-30 14:34:24 +00:00
const selectedVariantsToProductsMap = products
? products.map(product =>
product.variants.map(variant => isVariantSelected(variant, variants))
)
: [];
const productsWithAllVariantsSelected = products
? products.map(product =>
hasAllVariantsSelected(product.variants, variants)
)
: [];
2019-08-09 11:14:35 +00:00
2019-10-30 14:34:24 +00:00
const handleSubmit = () => onSubmit(variants);
return (
<Dialog
onClose={onClose}
open={open}
classes={{ paper: classes.overflow }}
fullWidth
maxWidth="sm"
>
<DialogTitle>
<FormattedMessage
2020-05-08 14:01:04 +00:00
defaultMessage="Add Product"
2019-10-30 14:34:24 +00:00
description="dialog header"
/>
</DialogTitle>
<DialogContent className={classes.overflow}>
<TextField
name="query"
value={query}
onChange={onQueryChange}
label={intl.formatMessage({
defaultMessage: "Search Products"
})}
placeholder={intl.formatMessage({
defaultMessage:
"Search by product name, attribute, product type etc..."
})}
fullWidth
InputProps={{
autoComplete: "off",
endAdornment: loading && <CircularProgress size={16} />
}}
/>
</DialogContent>
<DialogContent className={classes.content}>
<InfiniteScroll
pageStart={0}
loadMore={onFetchMore}
hasMore={hasMore}
useWindow={false}
loader={
<div className={classes.loadMoreLoaderContainer}>
<CircularProgress size={16} />
</div>
}
threshold={10}
>
2019-11-04 14:25:23 +00:00
<ResponsiveTable key="table">
2019-10-30 14:34:24 +00:00
<TableBody>
{renderCollection(
products,
(product, productIndex) => (
<React.Fragment key={product ? product.id : "skeleton"}>
2019-08-09 11:14:35 +00:00
<TableRow>
2019-10-30 14:34:24 +00:00
<TableCell
padding="checkbox"
className={classes.productCheckboxCell}
>
<Checkbox
checked={
productsWithAllVariantsSelected[productIndex]
}
disabled={loading}
onChange={() =>
onProductAdd(
product,
productIndex,
productsWithAllVariantsSelected,
variants,
setVariants
)
}
/>
</TableCell>
<TableCellAvatar
className={classes.avatar}
thumbnail={maybe(() => product.thumbnail.url)}
/>
<TableCell className={classes.colName} colSpan={2}>
{maybe(() => product.name)}
2019-08-09 11:14:35 +00:00
</TableCell>
</TableRow>
2019-10-30 14:34:24 +00:00
{maybe(() => product.variants, []).map(
(variant, variantIndex) => (
<TableRow key={variant.id}>
<TableCell />
<TableCell className={classes.colVariantCheckbox}>
<Checkbox
className={classes.variantCheckbox}
checked={
selectedVariantsToProductsMap[productIndex][
variantIndex
]
}
disabled={loading}
onChange={() =>
onVariantAdd(
variant,
variantIndex,
productIndex,
variants,
selectedVariantsToProductsMap,
setVariants
)
}
/>
</TableCell>
<TableCell className={classes.colName}>
<div>{variant.name}</div>
<div className={classes.grayText}>
<FormattedMessage
defaultMessage="SKU {sku}"
description="variant sku"
values={{
sku: variant.sku
}}
/>
</div>
</TableCell>
<TableCell className={classes.textRight}>
2020-01-09 11:13:24 +00:00
<Money
money={variant.pricing.priceUndiscounted.net}
/>
2019-10-30 14:34:24 +00:00
</TableCell>
</TableRow>
)
)}
</React.Fragment>
),
() => (
<TableRow>
<TableCell colSpan={4}>
<FormattedMessage defaultMessage="No products matching given query" />
</TableCell>
</TableRow>
)
)}
</TableBody>
2019-11-04 14:25:23 +00:00
</ResponsiveTable>
2019-10-30 14:34:24 +00:00
</InfiniteScroll>
2020-03-16 12:28:52 +00:00
{errors.length > 0 && (
<>
<FormSpacer />
{errors.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>
<Button onClick={onClose}>
<FormattedMessage {...buttonMessages.back} />
</Button>
<ConfirmButton
transitionState={confirmButtonState}
color="primary"
variant="contained"
type="submit"
onClick={handleSubmit}
>
<FormattedMessage {...buttonMessages.confirm} />
</ConfirmButton>
</DialogActions>
</Dialog>
);
};
2019-06-19 14:40:52 +00:00
OrderProductAddDialog.displayName = "OrderProductAddDialog";
export default OrderProductAddDialog;