2021-05-14 08:15:15 +00:00
|
|
|
import {
|
|
|
|
Checkbox,
|
|
|
|
CircularProgress,
|
|
|
|
Dialog,
|
|
|
|
DialogActions,
|
|
|
|
DialogContent,
|
|
|
|
DialogTitle,
|
|
|
|
TableBody,
|
|
|
|
TableCell,
|
|
|
|
TextField,
|
2022-06-21 09:36:55 +00:00
|
|
|
Typography,
|
2021-05-14 08:15:15 +00:00
|
|
|
} from "@material-ui/core";
|
2022-01-28 12:34:20 +00:00
|
|
|
import BackButton from "@saleor/components/BackButton";
|
2020-05-14 09:30:32 +00:00
|
|
|
import CardSpacer from "@saleor/components/CardSpacer";
|
2022-01-28 12:34:20 +00:00
|
|
|
import ConfirmButton from "@saleor/components/ConfirmButton";
|
2020-05-14 09:30:32 +00:00
|
|
|
import ResponsiveTable from "@saleor/components/ResponsiveTable";
|
|
|
|
import Skeleton from "@saleor/components/Skeleton";
|
2022-10-27 10:58:17 +00:00
|
|
|
import TableRowLink from "@saleor/components/TableRowLink";
|
2022-03-09 08:56:55 +00:00
|
|
|
import { SearchStaffMembersQuery } from "@saleor/graphql";
|
2021-12-16 11:36:03 +00:00
|
|
|
import useElementScroll, {
|
2022-06-21 09:36:55 +00:00
|
|
|
isScrolledToBottom,
|
2021-12-16 11:36:03 +00:00
|
|
|
} from "@saleor/hooks/useElementScroll";
|
2020-04-23 15:43:08 +00:00
|
|
|
import useSearchQuery from "@saleor/hooks/useSearchQuery";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { buttonMessages } from "@saleor/intl";
|
2022-01-28 12:34:20 +00:00
|
|
|
import { ConfirmButtonTransitionState, makeStyles } from "@saleor/macaw-ui";
|
2021-12-16 11:36:03 +00:00
|
|
|
import { getUserInitials, getUserName, renderCollection } from "@saleor/misc";
|
2022-03-09 08:56:55 +00:00
|
|
|
import {
|
|
|
|
DialogProps,
|
|
|
|
FetchMoreProps,
|
|
|
|
RelayToFlat,
|
2022-06-21 09:36:55 +00:00
|
|
|
SearchPageProps,
|
2022-03-09 08:56:55 +00:00
|
|
|
} from "@saleor/types";
|
2022-12-02 10:45:19 +00:00
|
|
|
import clsx from "clsx";
|
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";
|
2020-04-23 15:43:08 +00:00
|
|
|
|
2021-12-16 11:36:03 +00:00
|
|
|
import { messages } from "./messages";
|
|
|
|
|
2020-04-23 15:43:08 +00:00
|
|
|
const useStyles = makeStyles(
|
|
|
|
theme => ({
|
|
|
|
avatar: {
|
|
|
|
alignItems: "center",
|
|
|
|
borderRadius: "100%",
|
|
|
|
display: "grid",
|
|
|
|
float: "left",
|
|
|
|
height: 32,
|
|
|
|
justifyContent: "center",
|
|
|
|
overflow: "hidden",
|
2022-06-21 09:36:55 +00:00
|
|
|
width: 32,
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
avatarCell: {
|
|
|
|
padding: 0,
|
2022-06-21 09:36:55 +00:00
|
|
|
width: 32,
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
avatarDefault: {
|
2021-07-01 08:21:41 +00:00
|
|
|
"& div": {
|
2020-04-23 15:43:08 +00:00
|
|
|
color: "#fff",
|
2021-07-01 08:21:41 +00:00
|
|
|
lineHeight: 2.8,
|
2022-06-21 09:36:55 +00:00
|
|
|
fontSize: "0.75rem",
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
background: theme.palette.primary.main,
|
|
|
|
height: 32,
|
|
|
|
textAlign: "center",
|
2022-06-21 09:36:55 +00:00
|
|
|
width: 32,
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
avatarImage: {
|
|
|
|
pointerEvents: "none",
|
2022-06-21 09:36:55 +00:00
|
|
|
width: "100%",
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
checkboxCell: {
|
|
|
|
"&&:not(first-child)": {
|
|
|
|
paddingLeft: 0,
|
|
|
|
paddingRight: 0,
|
2022-06-21 09:36:55 +00:00
|
|
|
width: 48,
|
|
|
|
},
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
colActions: {
|
2022-06-21 09:36:55 +00:00
|
|
|
textAlign: "right",
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
colName: {
|
2022-06-21 09:36:55 +00:00
|
|
|
paddingLeft: theme.spacing(),
|
2020-04-23 15:43:08 +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
|
|
|
},
|
2020-04-23 15:43:08 +00:00
|
|
|
dropShadow: {
|
2022-06-21 09:36:55 +00:00
|
|
|
boxShadow: `0px -5px 10px 0px ${theme.palette.divider}`,
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
inputContainer: {
|
2022-06-21 09:36:55 +00:00
|
|
|
overflowY: "visible",
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
loadMoreLoaderContainer: {
|
|
|
|
alignItems: "center",
|
|
|
|
display: "flex",
|
|
|
|
gridColumnEnd: "span 3",
|
|
|
|
height: theme.spacing(4),
|
2022-06-21 09:36:55 +00:00
|
|
|
justifyContent: "center",
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
overflow: {
|
2022-06-21 09:36:55 +00:00
|
|
|
overflowY: "visible",
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
scrollArea: {
|
|
|
|
maxHeight: 400,
|
|
|
|
overflowY: "scroll",
|
2021-07-01 08:21:41 +00:00
|
|
|
paddingTop: 0,
|
2022-06-21 09:36:55 +00:00
|
|
|
paddingBottom: 0,
|
2021-07-01 08:21:41 +00:00
|
|
|
},
|
|
|
|
table: {
|
2022-06-21 09:36:55 +00:00
|
|
|
marginBottom: theme.spacing(3),
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
statusText: {
|
2022-06-21 09:36:55 +00:00
|
|
|
color: "#9E9D9D",
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
wideCell: {
|
2022-06-21 09:36:55 +00:00
|
|
|
width: "80%",
|
|
|
|
},
|
2020-04-23 15:43:08 +00:00
|
|
|
}),
|
2022-06-21 09:36:55 +00:00
|
|
|
{ name: "AssignStaffMembersDialog" },
|
2020-04-23 15:43:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
export interface AssignMembersDialogProps
|
|
|
|
extends DialogProps,
|
|
|
|
FetchMoreProps,
|
|
|
|
SearchPageProps {
|
|
|
|
confirmButtonState: ConfirmButtonTransitionState;
|
|
|
|
disabled: boolean;
|
2022-03-09 08:56:55 +00:00
|
|
|
staffMembers: RelayToFlat<SearchStaffMembersQuery["search"]>;
|
2020-04-23 15:43:08 +00:00
|
|
|
hasMore: boolean;
|
|
|
|
onFetchMore: () => void;
|
2022-03-09 08:56:55 +00:00
|
|
|
onSubmit: (data: RelayToFlat<SearchStaffMembersQuery["search"]>) => void;
|
2020-04-23 15:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleStaffMemberAssign(
|
2022-03-09 08:56:55 +00:00
|
|
|
member: RelayToFlat<SearchStaffMembersQuery["search"]>[0],
|
2020-04-23 15:43:08 +00:00
|
|
|
isSelected: boolean,
|
2022-03-09 08:56:55 +00:00
|
|
|
selectedMembers: RelayToFlat<SearchStaffMembersQuery["search"]>,
|
|
|
|
setSelectedMembers: (
|
2022-06-21 09:36:55 +00:00
|
|
|
data: RelayToFlat<SearchStaffMembersQuery["search"]>,
|
|
|
|
) => void,
|
2020-04-23 15:43:08 +00:00
|
|
|
) {
|
|
|
|
if (isSelected) {
|
|
|
|
setSelectedMembers(
|
2022-06-21 09:36:55 +00:00
|
|
|
selectedMembers.filter(selectedMember => selectedMember.id !== member.id),
|
2020-04-23 15:43:08 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
setSelectedMembers([...selectedMembers, member]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 08:21:41 +00:00
|
|
|
const scrollableTargetId = "assignMembersScrollableDialog";
|
|
|
|
|
2020-04-23 15:43:08 +00:00
|
|
|
const AssignMembersDialog: React.FC<AssignMembersDialogProps> = ({
|
|
|
|
confirmButtonState,
|
|
|
|
disabled,
|
|
|
|
loading,
|
|
|
|
onClose,
|
|
|
|
onFetchMore,
|
|
|
|
hasMore,
|
|
|
|
onSearchChange,
|
|
|
|
onSubmit,
|
|
|
|
open,
|
2022-06-21 09:36:55 +00:00
|
|
|
staffMembers,
|
2020-04-23 15:43:08 +00:00
|
|
|
}) => {
|
|
|
|
const intl = useIntl();
|
|
|
|
const classes = useStyles({});
|
|
|
|
const [query, onQueryChange] = useSearchQuery(onSearchChange);
|
|
|
|
|
|
|
|
const [selectedMembers, setSelectedMembers] = React.useState<
|
2022-03-09 08:56:55 +00:00
|
|
|
RelayToFlat<SearchStaffMembersQuery["search"]>
|
2020-04-23 15:43:08 +00:00
|
|
|
>([]);
|
|
|
|
|
|
|
|
const anchor = React.useRef<HTMLDivElement>();
|
|
|
|
const scrollPosition = useElementScroll(anchor);
|
2021-12-16 11:36:03 +00:00
|
|
|
const dropShadow = !isScrolledToBottom(anchor, scrollPosition);
|
2020-04-23 15:43:08 +00:00
|
|
|
|
|
|
|
return (
|
2021-12-16 11:36:03 +00:00
|
|
|
<Dialog
|
|
|
|
onClose={onClose}
|
|
|
|
open={open}
|
|
|
|
maxWidth="sm"
|
|
|
|
fullWidth
|
|
|
|
classes={{
|
2022-06-21 09:36:55 +00:00
|
|
|
paper: classes.dialogPaper,
|
2021-12-16 11:36:03 +00:00
|
|
|
}}
|
|
|
|
>
|
2020-04-23 15:43:08 +00:00
|
|
|
<DialogTitle>
|
2021-12-16 11:36:03 +00:00
|
|
|
<FormattedMessage {...messages.title} />
|
2020-04-23 15:43:08 +00:00
|
|
|
</DialogTitle>
|
|
|
|
<DialogContent className={classes.inputContainer}>
|
|
|
|
<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)}
|
2020-04-23 15:43:08 +00:00
|
|
|
fullWidth
|
|
|
|
InputProps={{
|
|
|
|
autoComplete: "off",
|
2022-06-21 09:36:55 +00:00
|
|
|
endAdornment: loading && <CircularProgress size={16} />,
|
2020-04-23 15:43:08 +00:00
|
|
|
}}
|
|
|
|
disabled={disabled}
|
|
|
|
/>
|
|
|
|
</DialogContent>
|
2021-12-16 11:36:03 +00:00
|
|
|
<DialogContent
|
|
|
|
className={classes.scrollArea}
|
|
|
|
ref={anchor}
|
|
|
|
id={scrollableTargetId}
|
|
|
|
>
|
2020-04-23 15:43:08 +00:00
|
|
|
<InfiniteScroll
|
2021-12-16 11:36:03 +00:00
|
|
|
dataLength={staffMembers?.length || 0}
|
2021-07-01 08:21:41 +00:00
|
|
|
next={onFetchMore}
|
2020-04-23 15:43:08 +00:00
|
|
|
hasMore={hasMore}
|
2021-07-01 08:21:41 +00:00
|
|
|
scrollThreshold="100px"
|
|
|
|
loader={
|
|
|
|
<>
|
|
|
|
{staffMembers?.length > 0 && <CardSpacer />}
|
|
|
|
<div className={classes.loadMoreLoaderContainer}>
|
|
|
|
<CircularProgress size={24} />
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
scrollableTarget={scrollableTargetId}
|
2020-04-23 15:43:08 +00:00
|
|
|
>
|
2021-07-01 08:21:41 +00:00
|
|
|
<ResponsiveTable className={classes.table}>
|
2020-04-23 15:43:08 +00:00
|
|
|
<TableBody>
|
2021-12-16 11:36:03 +00:00
|
|
|
{renderCollection(
|
|
|
|
staffMembers,
|
|
|
|
member => {
|
|
|
|
if (!member) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-04-23 15:43:08 +00:00
|
|
|
const isSelected = selectedMembers.some(
|
2022-06-21 09:36:55 +00:00
|
|
|
selectedMember => selectedMember.id === member.id,
|
2020-04-23 15:43:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2022-10-27 10:58:17 +00:00
|
|
|
<TableRowLink key={member.id} data-test-id="user-row">
|
2020-04-23 15:43:08 +00:00
|
|
|
<TableCell
|
|
|
|
padding="checkbox"
|
|
|
|
className={classes.checkboxCell}
|
|
|
|
>
|
|
|
|
<Checkbox
|
|
|
|
color="primary"
|
|
|
|
checked={isSelected}
|
|
|
|
onChange={() =>
|
|
|
|
handleStaffMemberAssign(
|
|
|
|
member,
|
|
|
|
isSelected,
|
|
|
|
selectedMembers,
|
2022-06-21 09:36:55 +00:00
|
|
|
setSelectedMembers,
|
2020-04-23 15:43:08 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</TableCell>
|
|
|
|
<TableCell className={classes.avatarCell}>
|
|
|
|
<div className={classes.avatar}>
|
|
|
|
{!!member?.avatar?.url ? (
|
|
|
|
<img
|
|
|
|
className={classes.avatarImage}
|
|
|
|
src={member.avatar.url}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className={classes.avatarDefault}>
|
|
|
|
<Typography>{getUserInitials(member)}</Typography>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</TableCell>
|
|
|
|
<TableCell className={classes.colName}>
|
|
|
|
<Typography>
|
|
|
|
{getUserName(member) || <Skeleton />}
|
|
|
|
</Typography>
|
|
|
|
<Typography
|
|
|
|
variant={"caption"}
|
|
|
|
className={classes.statusText}
|
|
|
|
>
|
|
|
|
{!!member ? (
|
|
|
|
member.isActive ? (
|
2021-12-16 11:36:03 +00:00
|
|
|
intl.formatMessage(messages.staffActive)
|
2020-04-23 15:43:08 +00:00
|
|
|
) : (
|
2021-12-16 11:36:03 +00:00
|
|
|
intl.formatMessage(messages.staffInactive)
|
2020-04-23 15:43:08 +00:00
|
|
|
)
|
|
|
|
) : (
|
|
|
|
<Skeleton />
|
|
|
|
)}
|
|
|
|
</Typography>
|
|
|
|
</TableCell>
|
2022-10-27 10:58:17 +00:00
|
|
|
</TableRowLink>
|
2020-04-23 15:43:08 +00:00
|
|
|
);
|
2021-12-16 11:36:03 +00:00
|
|
|
},
|
|
|
|
() =>
|
|
|
|
!loading && (
|
2022-10-27 10:58:17 +00:00
|
|
|
<TableRowLink>
|
2021-12-16 11:36:03 +00:00
|
|
|
<TableCell colSpan={2}>
|
|
|
|
<FormattedMessage {...messages.noMembersFound} />
|
|
|
|
</TableCell>
|
2022-10-27 10:58:17 +00:00
|
|
|
</TableRowLink>
|
2022-06-21 09:36:55 +00:00
|
|
|
),
|
2021-12-16 11:36:03 +00:00
|
|
|
)}
|
2020-04-23 15:43:08 +00:00
|
|
|
</TableBody>
|
|
|
|
</ResponsiveTable>
|
|
|
|
</InfiniteScroll>
|
|
|
|
</DialogContent>
|
|
|
|
<DialogActions
|
2022-12-02 10:45:19 +00:00
|
|
|
className={clsx({
|
2022-06-21 09:36:55 +00:00
|
|
|
[classes.dropShadow]: dropShadow,
|
2020-04-23 15:43:08 +00:00
|
|
|
})}
|
|
|
|
>
|
2022-01-28 12:34:20 +00:00
|
|
|
<BackButton onClick={onClose} />
|
2020-04-23 15:43:08 +00:00
|
|
|
<ConfirmButton
|
2022-02-11 11:28:55 +00:00
|
|
|
data-test-id="submit"
|
2020-04-23 15:43:08 +00:00
|
|
|
type="submit"
|
|
|
|
transitionState={confirmButtonState}
|
|
|
|
onClick={() => {
|
|
|
|
onSubmit(selectedMembers);
|
|
|
|
}}
|
|
|
|
>
|
2021-12-16 11:36:03 +00:00
|
|
|
<FormattedMessage {...buttonMessages.assign} />
|
2020-04-23 15:43:08 +00:00
|
|
|
</ConfirmButton>
|
|
|
|
</DialogActions>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
AssignMembersDialog.displayName = "AssignMembersDialog";
|
|
|
|
export default AssignMembersDialog;
|