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";
|
|
|
|
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 ConfirmButton, {
|
|
|
|
ConfirmButtonTransitionState
|
|
|
|
} from "@saleor/components/ConfirmButton";
|
|
|
|
import FormSpacer from "@saleor/components/FormSpacer";
|
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-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
|
|
|
import Checkbox from "../Checkbox";
|
|
|
|
|
|
|
|
export interface FormData {
|
2019-10-15 12:17:35 +00:00
|
|
|
products: SearchProducts_search_edges_node[];
|
2019-06-19 14:40:52 +00:00
|
|
|
query: string;
|
|
|
|
}
|
|
|
|
|
2019-12-03 15:28:40 +00:00
|
|
|
const useStyles = makeStyles(
|
|
|
|
{
|
|
|
|
avatar: {
|
|
|
|
"&:first-child": {
|
|
|
|
paddingLeft: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
checkboxCell: {
|
2019-06-19 14:40:52 +00:00
|
|
|
paddingLeft: 0
|
2019-12-03 15:28:40 +00:00
|
|
|
},
|
|
|
|
overflow: {
|
|
|
|
overflowY: "visible"
|
|
|
|
},
|
|
|
|
scrollArea: {
|
|
|
|
overflowY: "scroll"
|
|
|
|
},
|
|
|
|
wideCell: {
|
|
|
|
paddingLeft: 0,
|
|
|
|
width: "100%"
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
},
|
2019-12-03 15:28:40 +00:00
|
|
|
{ name: "AssignProductDialog" }
|
|
|
|
);
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-08-09 11:14:35 +00:00
|
|
|
export interface AssignProductDialogProps {
|
2019-06-19 14:40:52 +00:00
|
|
|
confirmButtonState: ConfirmButtonTransitionState;
|
|
|
|
open: boolean;
|
2019-10-15 12:17:35 +00:00
|
|
|
products: SearchProducts_search_edges_node[];
|
2019-06-19 14:40:52 +00:00
|
|
|
loading: boolean;
|
|
|
|
onClose: () => void;
|
|
|
|
onFetch: (value: string) => void;
|
2019-10-15 12:17:35 +00:00
|
|
|
onSubmit: (data: SearchProducts_search_edges_node[]) => void;
|
2019-08-09 11:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleProductAssign(
|
2019-10-15 12:17:35 +00:00
|
|
|
product: SearchProducts_search_edges_node,
|
2019-08-09 11:14:35 +00:00
|
|
|
isSelected: boolean,
|
2019-10-15 12:17:35 +00:00
|
|
|
selectedProducts: SearchProducts_search_edges_node[],
|
|
|
|
setSelectedProducts: (data: SearchProducts_search_edges_node[]) => void
|
2019-08-09 11:14:35 +00:00
|
|
|
) {
|
|
|
|
if (isSelected) {
|
|
|
|
setSelectedProducts(
|
|
|
|
selectedProducts.filter(
|
|
|
|
selectedProduct => selectedProduct.id !== product.id
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
setSelectedProducts([...selectedProducts, product]);
|
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const AssignProductDialog: React.FC<AssignProductDialogProps> = props => {
|
|
|
|
const {
|
2019-06-19 14:40:52 +00:00
|
|
|
confirmButtonState,
|
|
|
|
open,
|
|
|
|
loading,
|
|
|
|
products,
|
|
|
|
onClose,
|
|
|
|
onFetch,
|
|
|
|
onSubmit
|
2019-10-30 14:34:24 +00:00
|
|
|
} = props;
|
|
|
|
const classes = useStyles(props);
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const intl = useIntl();
|
|
|
|
const [query, onQueryChange] = useSearchQuery(onFetch);
|
|
|
|
const [selectedProducts, setSelectedProducts] = React.useState<
|
|
|
|
SearchProducts_search_edges_node[]
|
|
|
|
>([]);
|
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}
|
|
|
|
classes={{ paper: classes.overflow }}
|
|
|
|
fullWidth
|
|
|
|
maxWidth="sm"
|
|
|
|
>
|
|
|
|
<DialogTitle>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Assign Product"
|
|
|
|
description="dialog header"
|
|
|
|
/>
|
|
|
|
</DialogTitle>
|
|
|
|
<DialogContent>
|
|
|
|
<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} />
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<FormSpacer />
|
|
|
|
<div className={classes.scrollArea}>
|
2019-11-04 14:25:23 +00:00
|
|
|
<ResponsiveTable>
|
2019-10-30 14:34:24 +00:00
|
|
|
<TableBody>
|
|
|
|
{products &&
|
|
|
|
products.map(product => {
|
|
|
|
const isSelected = selectedProducts.some(
|
|
|
|
selectedProduct => selectedProduct.id === product.id
|
|
|
|
);
|
2019-08-09 11:14:35 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
return (
|
|
|
|
<TableRow key={product.id}>
|
|
|
|
<TableCellAvatar
|
|
|
|
className={classes.avatar}
|
|
|
|
thumbnail={maybe(() => product.thumbnail.url)}
|
|
|
|
/>
|
|
|
|
<TableCell className={classes.wideCell}>
|
|
|
|
{product.name}
|
|
|
|
</TableCell>
|
|
|
|
<TableCell
|
|
|
|
padding="checkbox"
|
|
|
|
className={classes.checkboxCell}
|
|
|
|
>
|
|
|
|
<Checkbox
|
|
|
|
checked={isSelected}
|
|
|
|
onChange={() =>
|
|
|
|
handleProductAssign(
|
|
|
|
product,
|
|
|
|
isSelected,
|
|
|
|
selectedProducts,
|
|
|
|
setSelectedProducts
|
|
|
|
)
|
|
|
|
}
|
2019-08-09 11:14:35 +00:00
|
|
|
/>
|
2019-10-30 14:34:24 +00:00
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</TableBody>
|
2019-11-04 14:25:23 +00:00
|
|
|
</ResponsiveTable>
|
2019-10-30 14:34:24 +00:00
|
|
|
</div>
|
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
|
|
|
<Button onClick={onClose}>
|
|
|
|
<FormattedMessage {...buttonMessages.back} />
|
|
|
|
</Button>
|
|
|
|
<ConfirmButton
|
|
|
|
transitionState={confirmButtonState}
|
|
|
|
color="primary"
|
|
|
|
variant="contained"
|
|
|
|
type="submit"
|
|
|
|
onClick={handleSubmit}
|
|
|
|
>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Assign products"
|
|
|
|
description="button"
|
|
|
|
/>
|
|
|
|
</ConfirmButton>
|
|
|
|
</DialogActions>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
AssignProductDialog.displayName = "AssignProductDialog";
|
|
|
|
export default AssignProductDialog;
|