|
| 1 | +import Button from "@material-ui/core/Button" |
| 2 | +import Link from "@material-ui/core/Link" |
| 3 | +import { makeStyles } from "@material-ui/core/styles" |
| 4 | +import Table from "@material-ui/core/Table" |
| 5 | +import TableBody from "@material-ui/core/TableBody" |
| 6 | +import TableCell from "@material-ui/core/TableCell" |
| 7 | +import TableContainer from "@material-ui/core/TableContainer" |
| 8 | +import TableHead from "@material-ui/core/TableHead" |
| 9 | +import TableRow from "@material-ui/core/TableRow" |
| 10 | +import AddCircleOutline from "@material-ui/icons/AddCircleOutline" |
| 11 | +import KeyboardArrowRight from "@material-ui/icons/KeyboardArrowRight" |
| 12 | +import { useMachine } from "@xstate/react" |
| 13 | +import { ChooseOne, Cond } from "components/Conditionals/ChooseOne" |
| 14 | +import { EmptyState } from "components/EmptyState/EmptyState" |
| 15 | +import { Margins } from "components/Margins/Margins" |
| 16 | +import { PageHeader, PageHeaderTitle } from "components/PageHeader/PageHeader" |
| 17 | +import { TableCellLink } from "components/TableCellLink/TableCellLink" |
| 18 | +import { TableLoader } from "components/TableLoader/TableLoader" |
| 19 | +import { useOrganizationId } from "hooks/useOrganizationId" |
| 20 | +import React from "react" |
| 21 | +import { Helmet } from "react-helmet-async" |
| 22 | +import { Link as RouterLink, useNavigate } from "react-router-dom" |
| 23 | +import { pageTitle } from "util/page" |
| 24 | +import { groupsMachine } from "xServices/groups/groupsXService" |
| 25 | + |
| 26 | +const CreateGroupButton: React.FC = () => { |
| 27 | + return ( |
| 28 | + <Link underline="none" component={RouterLink} to="/groups/new"> |
| 29 | + <Button startIcon={<AddCircleOutline />}>Create group</Button> |
| 30 | + </Link> |
| 31 | + ) |
| 32 | +} |
| 33 | + |
| 34 | +export const GroupsPage: React.FC = () => { |
| 35 | + const organizationId = useOrganizationId() |
| 36 | + const [state] = useMachine(groupsMachine, { |
| 37 | + context: { |
| 38 | + organizationId, |
| 39 | + }, |
| 40 | + }) |
| 41 | + const { groups } = state.context |
| 42 | + const isLoading = Boolean(groups === undefined) |
| 43 | + const isEmpty = Boolean(groups && groups.length === 0) |
| 44 | + const navigate = useNavigate() |
| 45 | + const styles = useStyles() |
| 46 | + |
| 47 | + return ( |
| 48 | + <> |
| 49 | + <Helmet> |
| 50 | + <title>{pageTitle("Groups")}</title> |
| 51 | + </Helmet> |
| 52 | + <Margins> |
| 53 | + <PageHeader actions={<CreateGroupButton />}> |
| 54 | + <PageHeaderTitle>Groups</PageHeaderTitle> |
| 55 | + </PageHeader> |
| 56 | + <TableContainer> |
| 57 | + <Table> |
| 58 | + <TableHead> |
| 59 | + <TableRow> |
| 60 | + <TableCell width="50%">Name</TableCell> |
| 61 | + <TableCell width="49%">Users</TableCell> |
| 62 | + <TableCell width="1%"></TableCell> |
| 63 | + </TableRow> |
| 64 | + </TableHead> |
| 65 | + <TableBody> |
| 66 | + <ChooseOne> |
| 67 | + <Cond condition={isLoading}> |
| 68 | + <TableLoader /> |
| 69 | + </Cond> |
| 70 | + |
| 71 | + <Cond condition={isEmpty}> |
| 72 | + <TableRow> |
| 73 | + <TableCell colSpan={999}> |
| 74 | + <EmptyState |
| 75 | + message="No groups yet" |
| 76 | + description="Create your first group" |
| 77 | + cta={<CreateGroupButton />} |
| 78 | + /> |
| 79 | + </TableCell> |
| 80 | + </TableRow> |
| 81 | + </Cond> |
| 82 | + |
| 83 | + <Cond condition={!isEmpty}> |
| 84 | + {groups?.map((group) => { |
| 85 | + const groupPageLink = `/groups/${group.uuid}` |
| 86 | + |
| 87 | + return ( |
| 88 | + <TableRow |
| 89 | + key={group.uuid} |
| 90 | + hover |
| 91 | + data-testid={`group-${group.uuid}`} |
| 92 | + tabIndex={0} |
| 93 | + onKeyDown={(event) => { |
| 94 | + if (event.key === "Enter") { |
| 95 | + navigate(groupPageLink) |
| 96 | + } |
| 97 | + }} |
| 98 | + className={styles.clickableTableRow} |
| 99 | + > |
| 100 | + <TableCellLink to={groupPageLink}>{group.name}</TableCellLink> |
| 101 | + |
| 102 | + <TableCell>Users</TableCell> |
| 103 | + |
| 104 | + <TableCellLink to={groupPageLink}> |
| 105 | + <div className={styles.arrowCell}> |
| 106 | + <KeyboardArrowRight className={styles.arrowRight} /> |
| 107 | + </div> |
| 108 | + </TableCellLink> |
| 109 | + </TableRow> |
| 110 | + ) |
| 111 | + })} |
| 112 | + </Cond> |
| 113 | + </ChooseOne> |
| 114 | + </TableBody> |
| 115 | + </Table> |
| 116 | + </TableContainer> |
| 117 | + </Margins> |
| 118 | + </> |
| 119 | + ) |
| 120 | +} |
| 121 | + |
| 122 | +const useStyles = makeStyles((theme) => ({ |
| 123 | + clickableTableRow: { |
| 124 | + "&:hover td": { |
| 125 | + backgroundColor: theme.palette.action.hover, |
| 126 | + }, |
| 127 | + |
| 128 | + "&:focus": { |
| 129 | + outline: `1px solid ${theme.palette.secondary.dark}`, |
| 130 | + }, |
| 131 | + |
| 132 | + "& .MuiTableCell-root:last-child": { |
| 133 | + paddingRight: theme.spacing(2), |
| 134 | + }, |
| 135 | + }, |
| 136 | + arrowRight: { |
| 137 | + color: theme.palette.text.secondary, |
| 138 | + width: 20, |
| 139 | + height: 20, |
| 140 | + }, |
| 141 | + arrowCell: { |
| 142 | + display: "flex", |
| 143 | + }, |
| 144 | +})) |
| 145 | + |
| 146 | +export default GroupsPage |
0 commit comments