2021-05-14 08:15:15 +00:00
|
|
|
import {
|
|
|
|
CircularProgress,
|
|
|
|
Dialog,
|
|
|
|
DialogActions,
|
|
|
|
DialogContent,
|
|
|
|
DialogTitle,
|
|
|
|
TableBody,
|
|
|
|
TableCell,
|
|
|
|
TableRow,
|
2022-06-21 09:36:55 +00:00
|
|
|
TextField,
|
2021-05-14 08:15:15 +00:00
|
|
|
} from "@material-ui/core";
|
2022-01-28 12:34:20 +00:00
|
|
|
import ConfirmButton 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";
|
2022-03-09 08:56:55 +00:00
|
|
|
import { SearchProductsQuery } from "@saleor/graphql";
|
2022-09-21 13:35:05 +00:00
|
|
|
import useModalDialogOpen from "@saleor/hooks/useModalDialogOpen";
|
2019-08-09 11:14:35 +00:00
|
|
|
import useSearchQuery from "@saleor/hooks/useSearchQuery";
|
2022-01-28 12:34:20 +00:00
|
|
|
import { ConfirmButtonTransitionState } from "@saleor/macaw-ui";
|
2019-08-09 11:14:35 +00:00
|
|
|
import { maybe } from "@saleor/misc";
|
2020-10-26 13:04:04 +00:00
|
|
|
import useScrollableDialogStyle from "@saleor/styles/useScrollableDialogStyle";
|
2022-03-09 08:56:55 +00:00
|
|
|
import { DialogProps, FetchMoreProps, RelayToFlat } from "@saleor/types";
|
2022-09-21 13:35:05 +00:00
|
|
|
import React, { useEffect } 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";
|
|
|
|
|
2022-01-28 12:34:20 +00:00
|
|
|
import BackButton from "../BackButton";
|
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 {
|
2022-03-09 08:56:55 +00:00
|
|
|
products: RelayToFlat<SearchProductsQuery["search"]>;
|
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;
|
2022-03-09 08:56:55 +00:00
|
|
|
products: RelayToFlat<SearchProductsQuery["search"]>;
|
2022-09-21 13:35:05 +00:00
|
|
|
selectedIds?: Record<string, boolean>;
|
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
|
|
|
}
|
|
|
|
|
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,
|
2022-06-21 09:36:55 +00:00
|
|
|
onSubmit,
|
2022-09-21 13:35:05 +00:00
|
|
|
selectedIds,
|
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-10-30 14:34:24 +00:00
|
|
|
const intl = useIntl();
|
2022-09-21 13:35:05 +00:00
|
|
|
const [query, onQueryChange, queryReset] = useSearchQuery(onFetch);
|
|
|
|
const [productsDict, setProductsDict] = React.useState(selectedIds || {});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (selectedIds) {
|
|
|
|
setProductsDict(prev => {
|
|
|
|
const prevIds = Object.keys(prev);
|
|
|
|
const newIds = Object.keys(selectedIds);
|
|
|
|
|
|
|
|
const preSelected = newIds
|
|
|
|
.filter(n => !prevIds.includes(n))
|
|
|
|
.reduce((p, c) => ({ ...p, [c]: true }), {});
|
2019-08-09 11:14:35 +00:00
|
|
|
|
2022-09-21 13:35:05 +00:00
|
|
|
return { ...prev, ...preSelected };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [selectedIds]);
|
|
|
|
|
|
|
|
useModalDialogOpen(open, {
|
|
|
|
onOpen: () => {
|
|
|
|
queryReset();
|
|
|
|
setProductsDict(selectedIds);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleSubmit = () => {
|
|
|
|
const selectedProductsAsArray = Object.keys(productsDict)
|
|
|
|
.filter(key => productsDict[key])
|
|
|
|
.map(key => key);
|
|
|
|
|
|
|
|
onSubmit(selectedProductsAsArray);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleChange = productId => {
|
|
|
|
setProductsDict(prev => ({
|
|
|
|
...prev,
|
|
|
|
[productId]: !prev[productId] ?? true,
|
|
|
|
}));
|
|
|
|
};
|
2019-10-30 14:34:24 +00:00
|
|
|
|
|
|
|
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>
|
2022-08-25 08:35:07 +00:00
|
|
|
<DialogContent>
|
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",
|
2022-06-21 09:36:55 +00:00
|
|
|
endAdornment: loading && <CircularProgress size={16} />,
|
2019-10-30 14:34:24 +00:00
|
|
|
}}
|
|
|
|
/>
|
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 => {
|
2022-09-21 13:35:05 +00:00
|
|
|
const isSelected = productsDict[product.id] || false;
|
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}
|
2022-09-21 13:35:05 +00:00
|
|
|
onChange={() => handleChange(product.id)}
|
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>
|
2022-01-28 12:34:20 +00:00
|
|
|
<BackButton onClick={onClose} />
|
2019-10-30 14:34:24 +00:00
|
|
|
<ConfirmButton
|
2022-02-11 11:28:55 +00:00
|
|
|
data-test-id="submit"
|
2019-10-30 14:34:24 +00:00
|
|
|
transitionState={confirmButtonState}
|
|
|
|
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;
|