2021-05-14 08:15:15 +00:00
|
|
|
import {
|
|
|
|
CircularProgress,
|
|
|
|
Dialog,
|
|
|
|
DialogActions,
|
|
|
|
DialogContent,
|
|
|
|
DialogContentText,
|
|
|
|
DialogTitle,
|
|
|
|
TableBody,
|
|
|
|
TableCell,
|
|
|
|
TableRow,
|
|
|
|
TextField,
|
2022-06-21 09:36:55 +00:00
|
|
|
Typography,
|
2021-05-14 08:15:15 +00:00
|
|
|
} from "@material-ui/core";
|
2019-08-09 10:17:04 +00:00
|
|
|
import Checkbox from "@saleor/components/Checkbox";
|
2022-01-28 12:34:20 +00:00
|
|
|
import ConfirmButton from "@saleor/components/ConfirmButton";
|
2019-11-13 13:00:53 +00:00
|
|
|
import ResponsiveTable from "@saleor/components/ResponsiveTable";
|
2022-03-09 08:56:55 +00:00
|
|
|
import { AvailableAttributeFragment } from "@saleor/graphql";
|
2019-11-04 13:36:25 +00:00
|
|
|
import useElementScroll, {
|
2022-06-21 09:36:55 +00:00
|
|
|
isScrolledToBottom,
|
2019-11-04 13:36:25 +00:00
|
|
|
} from "@saleor/hooks/useElementScroll";
|
2019-08-09 10:17:04 +00:00
|
|
|
import useModalDialogErrors from "@saleor/hooks/useModalDialogErrors";
|
|
|
|
import useModalDialogOpen from "@saleor/hooks/useModalDialogOpen";
|
|
|
|
import useSearchQuery from "@saleor/hooks/useSearchQuery";
|
2022-01-28 12:34:20 +00:00
|
|
|
import { ConfirmButtonTransitionState, makeStyles } from "@saleor/macaw-ui";
|
2019-08-09 10:17:04 +00:00
|
|
|
import { maybe, renderCollection } from "@saleor/misc";
|
|
|
|
import { FetchMoreProps } from "@saleor/types";
|
2020-05-14 09:30:32 +00:00
|
|
|
import classNames from "classnames";
|
|
|
|
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";
|
|
|
|
|
2022-01-28 12:34:20 +00:00
|
|
|
import BackButton from "../BackButton";
|
2021-12-16 11:36:03 +00:00
|
|
|
import { messages } from "./messages";
|
|
|
|
|
2019-12-03 15:28:40 +00:00
|
|
|
const useStyles = makeStyles(
|
|
|
|
theme => ({
|
|
|
|
actions: {
|
2022-06-21 09:36:55 +00:00
|
|
|
boxShadow: "0px 4px 4px rgba(0, 0, 0, 0.25)",
|
2019-12-03 15:28:40 +00:00
|
|
|
},
|
|
|
|
checkboxCell: {
|
2022-06-21 09:36:55 +00:00
|
|
|
paddingLeft: 0,
|
2019-12-03 15:28:40 +00:00
|
|
|
},
|
2021-12-16 11:36:03 +00:00
|
|
|
dialogPaper: {
|
2022-06-21 09:36:55 +00:00
|
|
|
overflow: "hidden",
|
2021-12-16 11:36:03 +00:00
|
|
|
},
|
2019-12-03 15:28:40 +00:00
|
|
|
dropShadow: {
|
2022-06-21 09:36:55 +00:00
|
|
|
boxShadow: `0px -5px 10px 0px ${theme.palette.divider}`,
|
2019-12-03 15:28:40 +00:00
|
|
|
},
|
|
|
|
loadMoreLoaderContainer: {
|
|
|
|
alignItems: "center",
|
|
|
|
display: "flex",
|
2022-01-28 12:34:20 +00:00
|
|
|
marginTop: theme.spacing(2),
|
2019-12-03 15:28:40 +00:00
|
|
|
height: theme.spacing(3),
|
2022-06-21 09:36:55 +00:00
|
|
|
justifyContent: "center",
|
2019-12-03 15:28:40 +00:00
|
|
|
},
|
2021-08-11 09:03:39 +00:00
|
|
|
searchArea: {
|
|
|
|
marginBottom: theme.spacing(3),
|
|
|
|
overflowY: "hidden",
|
2022-06-21 09:36:55 +00:00
|
|
|
paddingBottom: theme.spacing(6),
|
2021-08-11 09:03:39 +00:00
|
|
|
},
|
2019-12-03 15:28:40 +00:00
|
|
|
scrollArea: {
|
2021-12-22 12:51:28 +00:00
|
|
|
maxHeight: 700,
|
2021-08-11 09:03:39 +00:00
|
|
|
overflowY: "scroll",
|
|
|
|
paddingTop: 0,
|
2022-06-21 09:36:55 +00:00
|
|
|
marginBottom: theme.spacing(3),
|
2019-12-03 15:28:40 +00:00
|
|
|
},
|
|
|
|
wideCell: {
|
2022-06-21 09:36:55 +00:00
|
|
|
width: "100%",
|
|
|
|
},
|
2019-12-03 15:28:40 +00:00
|
|
|
}),
|
2022-06-21 09:36:55 +00:00
|
|
|
{ name: "AssignAttributeDialog" },
|
2019-12-03 15:28:40 +00:00
|
|
|
);
|
2019-08-09 10:17:04 +00:00
|
|
|
|
|
|
|
export interface AssignAttributeDialogProps extends FetchMoreProps {
|
|
|
|
confirmButtonState: ConfirmButtonTransitionState;
|
2020-03-11 09:56:04 +00:00
|
|
|
errors: string[];
|
2019-08-09 10:17:04 +00:00
|
|
|
open: boolean;
|
2020-11-19 14:42:14 +00:00
|
|
|
attributes: AvailableAttributeFragment[];
|
2019-08-09 10:17:04 +00:00
|
|
|
selected: string[];
|
|
|
|
onClose: () => void;
|
2019-08-12 14:11:10 +00:00
|
|
|
onFetch: (query: string) => void;
|
2019-08-09 10:17:04 +00:00
|
|
|
onOpen: () => void;
|
|
|
|
onSubmit: () => void;
|
|
|
|
onToggle: (id: string) => void;
|
|
|
|
}
|
|
|
|
|
2021-07-01 08:21:41 +00:00
|
|
|
const scrollableTargetId = "assignAttributeScrollableDialog";
|
|
|
|
|
2019-08-09 10:17:04 +00:00
|
|
|
const AssignAttributeDialog: React.FC<AssignAttributeDialogProps> = ({
|
|
|
|
attributes,
|
|
|
|
confirmButtonState,
|
|
|
|
errors: apiErrors,
|
|
|
|
hasMore,
|
|
|
|
loading,
|
|
|
|
open,
|
|
|
|
selected,
|
|
|
|
onClose,
|
|
|
|
onFetch,
|
|
|
|
onFetchMore,
|
|
|
|
onOpen,
|
|
|
|
onSubmit,
|
2022-06-21 09:36:55 +00:00
|
|
|
onToggle,
|
2019-08-09 10:17:04 +00:00
|
|
|
}: AssignAttributeDialogProps) => {
|
2019-08-26 17:56:15 +00:00
|
|
|
const intl = useIntl();
|
2019-08-09 10:17:04 +00:00
|
|
|
const classes = useStyles({});
|
|
|
|
const [query, onQueryChange, resetQuery] = useSearchQuery(onFetch);
|
|
|
|
const errors = useModalDialogErrors(apiErrors, open);
|
2019-11-04 13:36:25 +00:00
|
|
|
const anchor = React.useRef(null);
|
|
|
|
const position = useElementScroll(anchor);
|
2019-08-09 10:17:04 +00:00
|
|
|
|
|
|
|
useModalDialogOpen(open, {
|
|
|
|
onClose: resetQuery,
|
2022-06-21 09:36:55 +00:00
|
|
|
onOpen,
|
2019-08-09 10:17:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2021-12-16 11:36:03 +00:00
|
|
|
<Dialog
|
|
|
|
onClose={onClose}
|
|
|
|
open={open}
|
|
|
|
fullWidth
|
|
|
|
maxWidth="sm"
|
|
|
|
classes={{
|
2022-06-21 09:36:55 +00:00
|
|
|
paper: classes.dialogPaper,
|
2021-12-16 11:36:03 +00:00
|
|
|
}}
|
|
|
|
>
|
2019-08-26 17:56:15 +00:00
|
|
|
<DialogTitle>
|
2021-12-16 11:36:03 +00:00
|
|
|
<FormattedMessage {...messages.title} />
|
2019-08-26 17:56:15 +00:00
|
|
|
</DialogTitle>
|
2021-08-11 09:03:39 +00:00
|
|
|
<DialogContent className={classes.searchArea}>
|
2019-08-09 10:17:04 +00:00
|
|
|
<TextField
|
|
|
|
name="query"
|
|
|
|
value={query}
|
|
|
|
onChange={onQueryChange}
|
2021-12-16 11:36:03 +00:00
|
|
|
label={intl.formatMessage(messages.searchInputLabel)}
|
|
|
|
placeholder={intl.formatMessage(messages.searchInputPlaceholder)}
|
2019-08-09 10:17:04 +00:00
|
|
|
fullWidth
|
|
|
|
InputProps={{
|
|
|
|
autoComplete: "off",
|
2022-06-21 09:36:55 +00:00
|
|
|
endAdornment: loading && <CircularProgress size={16} />,
|
2019-08-09 10:17:04 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</DialogContent>
|
2021-07-01 08:21:41 +00:00
|
|
|
<DialogContent
|
|
|
|
className={classes.scrollArea}
|
|
|
|
ref={anchor}
|
|
|
|
id={scrollableTargetId}
|
|
|
|
>
|
2019-08-09 10:17:04 +00:00
|
|
|
<InfiniteScroll
|
2022-01-28 12:34:20 +00:00
|
|
|
dataLength={attributes?.length || 0}
|
2021-07-01 08:21:41 +00:00
|
|
|
next={onFetchMore}
|
2019-08-09 10:17:04 +00:00
|
|
|
hasMore={hasMore}
|
2021-07-01 08:21:41 +00:00
|
|
|
scrollThreshold="100px"
|
2019-08-09 10:17:04 +00:00
|
|
|
loader={
|
|
|
|
<div className={classes.loadMoreLoaderContainer}>
|
|
|
|
<CircularProgress size={16} />
|
|
|
|
</div>
|
|
|
|
}
|
2021-07-01 08:21:41 +00:00
|
|
|
scrollableTarget={scrollableTargetId}
|
2019-08-09 10:17:04 +00:00
|
|
|
>
|
2019-11-04 14:25:23 +00:00
|
|
|
<ResponsiveTable key="table">
|
2019-08-09 10:17:04 +00:00
|
|
|
<TableBody>
|
|
|
|
{renderCollection(
|
|
|
|
attributes,
|
|
|
|
attribute => {
|
|
|
|
if (!attribute) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const isChecked = !!selected.find(
|
2022-06-21 09:36:55 +00:00
|
|
|
selectedAttribute => selectedAttribute === attribute.id,
|
2019-08-09 10:17:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TableRow key={maybe(() => attribute.id)}>
|
|
|
|
<TableCell
|
|
|
|
padding="checkbox"
|
|
|
|
className={classes.checkboxCell}
|
|
|
|
>
|
|
|
|
<Checkbox
|
|
|
|
checked={isChecked}
|
|
|
|
onChange={() => onToggle(attribute.id)}
|
|
|
|
/>
|
|
|
|
</TableCell>
|
|
|
|
<TableCell className={classes.wideCell}>
|
|
|
|
{attribute.name}
|
|
|
|
<Typography variant="caption">
|
|
|
|
{attribute.slug}
|
|
|
|
</Typography>
|
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
() =>
|
|
|
|
!loading && (
|
|
|
|
<TableRow>
|
|
|
|
<TableCell colSpan={2}>
|
2021-12-16 11:36:03 +00:00
|
|
|
<FormattedMessage {...messages.noMembersFound} />
|
2019-08-09 10:17:04 +00:00
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
2022-06-21 09:36:55 +00:00
|
|
|
),
|
2019-08-09 10:17:04 +00:00
|
|
|
)}
|
|
|
|
</TableBody>
|
2019-11-04 14:25:23 +00:00
|
|
|
</ResponsiveTable>
|
2019-08-09 10:17:04 +00:00
|
|
|
</InfiniteScroll>
|
|
|
|
</DialogContent>
|
|
|
|
{errors.length > 0 && (
|
|
|
|
<DialogContent>
|
|
|
|
{errors.map((error, errorIndex) => (
|
|
|
|
<DialogContentText color="error" key={errorIndex}>
|
2020-03-11 09:56:04 +00:00
|
|
|
{error}
|
2019-08-09 10:17:04 +00:00
|
|
|
</DialogContentText>
|
|
|
|
))}
|
|
|
|
</DialogContent>
|
|
|
|
)}
|
2019-11-04 13:36:25 +00:00
|
|
|
<DialogActions
|
|
|
|
className={classNames(classes.actions, {
|
2022-06-21 09:36:55 +00:00
|
|
|
[classes.dropShadow]: !isScrolledToBottom(anchor, position),
|
2019-11-04 13:36:25 +00:00
|
|
|
})}
|
|
|
|
>
|
2022-01-28 12:34:20 +00:00
|
|
|
<BackButton onClick={onClose} />
|
2019-08-09 10:17:04 +00:00
|
|
|
<ConfirmButton
|
|
|
|
transitionState={confirmButtonState}
|
|
|
|
type="submit"
|
|
|
|
onClick={onSubmit}
|
|
|
|
>
|
2021-12-16 11:36:03 +00:00
|
|
|
<FormattedMessage {...messages.assignButton} />
|
2019-08-09 10:17:04 +00:00
|
|
|
</ConfirmButton>
|
|
|
|
</DialogActions>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
AssignAttributeDialog.displayName = "AssignAttributeDialog";
|
|
|
|
export default AssignAttributeDialog;
|