2021-05-14 08:15:15 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
CircularProgress,
|
|
|
|
Dialog,
|
|
|
|
DialogActions,
|
|
|
|
DialogContent,
|
|
|
|
DialogTitle,
|
|
|
|
TableBody,
|
|
|
|
TableCell,
|
|
|
|
TableRow,
|
|
|
|
TextField
|
|
|
|
} from "@material-ui/core";
|
2019-06-19 14:40:52 +00:00
|
|
|
import ConfirmButton, {
|
|
|
|
ConfirmButtonTransitionState
|
|
|
|
} from "@saleor/components/ConfirmButton";
|
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";
|
2019-08-09 11:14:35 +00:00
|
|
|
import useSearchQuery from "@saleor/hooks/useSearchQuery";
|
2019-08-26 21:54:03 +00:00
|
|
|
import { buttonMessages } from "@saleor/intl";
|
2019-08-09 11:14:35 +00:00
|
|
|
import { maybe } from "@saleor/misc";
|
2019-11-19 16:50:40 +00:00
|
|
|
import { SearchProducts_search_edges_node } from "@saleor/searches/types/SearchProducts";
|
2020-10-26 13:04:04 +00:00
|
|
|
import useScrollableDialogStyle from "@saleor/styles/useScrollableDialogStyle";
|
2021-09-30 12:51:13 +00:00
|
|
|
import { DialogProps, FetchMoreProps } from "@saleor/types";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
2021-07-01 08:21:41 +00:00
|
|
|
import InfiniteScroll from "react-infinite-scroll-component";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { FormattedMessage, useIntl } from "react-intl";
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
import Checkbox from "../Checkbox";
|
2021-09-30 12:51:13 +00:00
|
|
|
import { messages } from "./messages";
|
|
|
|
import { useStyles } from "./styles";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2021-09-30 12:51:13 +00:00
|
|
|
export interface AssignProductDialogFormData {
|
2019-10-15 12:17:35 +00:00
|
|
|
products: SearchProducts_search_edges_node[];
|
2019-06-19 14:40:52 +00:00
|
|
|
query: string;
|
|
|
|
}
|
|
|
|
|
2021-09-30 12:51:13 +00:00
|
|
|
export interface AssignProductDialogProps extends FetchMoreProps, DialogProps {
|
2019-06-19 14:40:52 +00:00
|
|
|
confirmButtonState: ConfirmButtonTransitionState;
|
2019-10-15 12:17:35 +00:00
|
|
|
products: SearchProducts_search_edges_node[];
|
2019-06-19 14:40:52 +00:00
|
|
|
loading: boolean;
|
|
|
|
onFetch: (value: string) => void;
|
2021-09-30 12:51:13 +00:00
|
|
|
onSubmit: (data: string[]) => void;
|
2019-08-09 11:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleProductAssign(
|
2021-09-30 12:51:13 +00:00
|
|
|
productID: string,
|
2019-08-09 11:14:35 +00:00
|
|
|
isSelected: boolean,
|
2021-09-30 12:51:13 +00:00
|
|
|
selectedProducts: string[],
|
|
|
|
setSelectedProducts: (data: string[]) => void
|
2019-08-09 11:14:35 +00:00
|
|
|
) {
|
|
|
|
if (isSelected) {
|
|
|
|
setSelectedProducts(
|
2021-09-30 12:51:13 +00:00
|
|
|
selectedProducts.filter(selectedProduct => selectedProduct !== productID)
|
2019-08-09 11:14:35 +00:00
|
|
|
);
|
|
|
|
} else {
|
2021-09-30 12:51:13 +00:00
|
|
|
setSelectedProducts([...selectedProducts, productID]);
|
2019-08-09 11:14:35 +00:00
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 08:21:41 +00:00
|
|
|
const scrollableTargetId = "assignProductScrollableDialog";
|
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const AssignProductDialog: React.FC<AssignProductDialogProps> = props => {
|
|
|
|
const {
|
2019-06-19 14:40:52 +00:00
|
|
|
confirmButtonState,
|
2020-10-26 10:29:41 +00:00
|
|
|
hasMore,
|
2019-06-19 14:40:52 +00:00
|
|
|
open,
|
|
|
|
loading,
|
|
|
|
products,
|
|
|
|
onClose,
|
|
|
|
onFetch,
|
2020-10-26 10:29:41 +00:00
|
|
|
onFetchMore,
|
2019-06-19 14:40:52 +00:00
|
|
|
onSubmit
|
2019-10-30 14:34:24 +00:00
|
|
|
} = props;
|
|
|
|
const classes = useStyles(props);
|
2020-10-26 13:04:04 +00:00
|
|
|
const scrollableDialogClasses = useScrollableDialogStyle({});
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const intl = useIntl();
|
|
|
|
const [query, onQueryChange] = useSearchQuery(onFetch);
|
2021-09-30 12:51:13 +00:00
|
|
|
const [selectedProducts, setSelectedProducts] = React.useState<string[]>([]);
|
2019-08-09 11:14:35 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const handleSubmit = () => onSubmit(selectedProducts);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog
|
|
|
|
onClose={onClose}
|
|
|
|
open={open}
|
2020-10-26 13:04:04 +00:00
|
|
|
classes={{ paper: scrollableDialogClasses.dialog }}
|
2019-10-30 14:34:24 +00:00
|
|
|
fullWidth
|
|
|
|
maxWidth="sm"
|
|
|
|
>
|
|
|
|
<DialogTitle>
|
2021-09-30 12:51:13 +00:00
|
|
|
<FormattedMessage {...messages.assignVariantDialogHeader} />
|
2019-10-30 14:34:24 +00:00
|
|
|
</DialogTitle>
|
2021-07-01 08:21:41 +00:00
|
|
|
<DialogContent className={scrollableDialogClasses.topArea}>
|
2019-10-30 14:34:24 +00:00
|
|
|
<TextField
|
|
|
|
name="query"
|
|
|
|
value={query}
|
|
|
|
onChange={onQueryChange}
|
2021-09-30 12:51:13 +00:00
|
|
|
label={intl.formatMessage(messages.assignProductDialogSearch)}
|
|
|
|
placeholder={intl.formatMessage(messages.assignProductDialogContent)}
|
2019-10-30 14:34:24 +00:00
|
|
|
fullWidth
|
|
|
|
InputProps={{
|
|
|
|
autoComplete: "off",
|
|
|
|
endAdornment: loading && <CircularProgress size={16} />
|
|
|
|
}}
|
|
|
|
/>
|
2021-07-01 08:21:41 +00:00
|
|
|
</DialogContent>
|
|
|
|
<DialogContent
|
|
|
|
className={scrollableDialogClasses.scrollArea}
|
|
|
|
id={scrollableTargetId}
|
|
|
|
>
|
|
|
|
<InfiniteScroll
|
|
|
|
dataLength={products?.length}
|
|
|
|
next={onFetchMore}
|
|
|
|
hasMore={hasMore}
|
|
|
|
scrollThreshold="100px"
|
|
|
|
loader={
|
|
|
|
<div className={scrollableDialogClasses.loadMoreLoaderContainer}>
|
|
|
|
<CircularProgress size={16} />
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
scrollableTarget={scrollableTargetId}
|
2020-10-26 13:04:04 +00:00
|
|
|
>
|
2021-07-01 08:21:41 +00:00
|
|
|
<ResponsiveTable key="table">
|
|
|
|
<TableBody>
|
|
|
|
{products &&
|
|
|
|
products.map(product => {
|
|
|
|
const isSelected = selectedProducts.some(
|
2021-09-30 12:51:13 +00:00
|
|
|
selectedProduct => selectedProduct === product.id
|
2021-07-01 08:21:41 +00:00
|
|
|
);
|
2019-08-09 11:14:35 +00:00
|
|
|
|
2021-07-01 08:21:41 +00:00
|
|
|
return (
|
|
|
|
<TableRow
|
|
|
|
key={product.id}
|
|
|
|
data-test-id="assign-product-table-row"
|
|
|
|
>
|
|
|
|
<TableCellAvatar
|
|
|
|
className={classes.avatar}
|
|
|
|
thumbnail={maybe(() => product.thumbnail.url)}
|
|
|
|
/>
|
|
|
|
<TableCell className={classes.colName}>
|
|
|
|
{product.name}
|
|
|
|
</TableCell>
|
|
|
|
<TableCell
|
|
|
|
padding="checkbox"
|
|
|
|
className={classes.checkboxCell}
|
2021-03-12 14:57:02 +00:00
|
|
|
>
|
2021-07-01 08:21:41 +00:00
|
|
|
<Checkbox
|
|
|
|
checked={isSelected}
|
|
|
|
onChange={() =>
|
|
|
|
handleProductAssign(
|
2021-09-30 12:51:13 +00:00
|
|
|
product.id,
|
2021-07-01 08:21:41 +00:00
|
|
|
isSelected,
|
|
|
|
selectedProducts,
|
|
|
|
setSelectedProducts
|
|
|
|
)
|
|
|
|
}
|
2019-08-09 11:14:35 +00:00
|
|
|
/>
|
2021-07-01 08:21:41 +00:00
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</TableBody>
|
|
|
|
</ResponsiveTable>
|
|
|
|
</InfiniteScroll>
|
2019-10-30 14:34:24 +00:00
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
|
|
|
<Button onClick={onClose}>
|
|
|
|
<FormattedMessage {...buttonMessages.back} />
|
|
|
|
</Button>
|
|
|
|
<ConfirmButton
|
2021-03-12 14:57:02 +00:00
|
|
|
data-test="submit"
|
2019-10-30 14:34:24 +00:00
|
|
|
transitionState={confirmButtonState}
|
|
|
|
color="primary"
|
|
|
|
variant="contained"
|
|
|
|
type="submit"
|
|
|
|
onClick={handleSubmit}
|
|
|
|
>
|
2021-09-30 12:51:13 +00:00
|
|
|
<FormattedMessage {...messages.assignProductDialogButton} />
|
2019-10-30 14:34:24 +00:00
|
|
|
</ConfirmButton>
|
|
|
|
</DialogActions>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
AssignProductDialog.displayName = "AssignProductDialog";
|
|
|
|
export default AssignProductDialog;
|