saleor-dashboard/src/navigation/components/MenuList/MenuList.tsx

165 lines
4.8 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Card from "@material-ui/core/Card";
import {
createStyles,
Theme,
withStyles,
WithStyles
} from "@material-ui/core/styles";
import Table from "@material-ui/core/Table";
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableFooter from "@material-ui/core/TableFooter";
import TableRow from "@material-ui/core/TableRow";
import DeleteIcon from "@material-ui/icons/Delete";
2019-08-09 10:26:22 +00:00
import React from "react";
import { FormattedMessage } from "react-intl";
2019-06-19 14:40:52 +00:00
import Checkbox from "@saleor/components/Checkbox";
import IconButtonTableCell from "@saleor/components/IconButtonTableCell";
import Skeleton from "@saleor/components/Skeleton";
import TableHead from "@saleor/components/TableHead";
import TablePagination from "@saleor/components/TablePagination";
2019-08-09 11:14:35 +00:00
import { maybe, renderCollection } from "@saleor/misc";
import { ListActions, ListProps } from "@saleor/types";
2019-06-19 14:40:52 +00:00
import { MenuList_menus_edges_node } from "../../types/MenuList";
export interface MenuListProps extends ListProps, ListActions {
menus: MenuList_menus_edges_node[];
onDelete: (id: string) => void;
}
const styles = (theme: Theme) =>
createStyles({
[theme.breakpoints.up("lg")]: {
colItems: {
width: 200
},
colTitle: {}
},
colItems: {
textAlign: "right"
},
colTitle: {},
row: {
cursor: "pointer"
}
});
2019-08-09 11:14:35 +00:00
const numberOfColumns = 4;
2019-06-19 14:40:52 +00:00
const MenuList = withStyles(styles, { name: "MenuList" })(
({
classes,
2019-08-09 11:14:35 +00:00
settings,
2019-06-19 14:40:52 +00:00
disabled,
isChecked,
menus,
onDelete,
onNextPage,
onPreviousPage,
2019-08-09 11:14:35 +00:00
onUpdateListSettings,
2019-06-19 14:40:52 +00:00
onRowClick,
pageInfo,
selected,
toggle,
toggleAll,
toolbar
}: MenuListProps & WithStyles<typeof styles>) => (
<Card>
<Table>
<TableHead
2019-08-09 11:14:35 +00:00
colSpan={numberOfColumns}
2019-06-19 14:40:52 +00:00
selected={selected}
disabled={disabled}
items={menus}
toggleAll={toggleAll}
toolbar={toolbar}
>
<TableCell className={classes.colTitle}>
<FormattedMessage
defaultMessage="Menu Title"
id="menuListMenutitle"
/>
2019-06-19 14:40:52 +00:00
</TableCell>
<TableCell className={classes.colItems}>
<FormattedMessage
defaultMessage="Items"
description="number of menu items"
id="menuListItems"
/>
2019-06-19 14:40:52 +00:00
</TableCell>
</TableHead>
<TableFooter>
<TableRow>
<TablePagination
2019-08-09 11:14:35 +00:00
colSpan={numberOfColumns}
settings={settings}
2019-06-19 14:40:52 +00:00
hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}
onNextPage={onNextPage}
2019-08-09 11:14:35 +00:00
onUpdateListSettings={onUpdateListSettings}
2019-06-19 14:40:52 +00:00
hasPreviousPage={
pageInfo && !disabled ? pageInfo.hasPreviousPage : false
}
onPreviousPage={onPreviousPage}
/>
</TableRow>
</TableFooter>
<TableBody>
{renderCollection(
menus,
menu => {
const isSelected = menu ? isChecked(menu.id) : false;
return (
<TableRow
hover={!!menu}
key={menu ? menu.id : "skeleton"}
onClick={menu && onRowClick(menu.id)}
className={classes.row}
selected={isSelected}
>
<TableCell padding="checkbox">
<Checkbox
checked={isSelected}
disabled={disabled}
2019-08-09 11:14:35 +00:00
disableClickPropagation
2019-06-19 14:40:52 +00:00
onChange={() => toggle(menu.id)}
/>
</TableCell>
<TableCell className={classes.colTitle}>
{maybe<React.ReactNode>(() => menu.name, <Skeleton />)}
</TableCell>
<TableCell className={classes.colItems}>
{maybe<React.ReactNode>(
() => menu.items.length,
<Skeleton />
)}
</TableCell>
<IconButtonTableCell
disabled={disabled}
onClick={() => onDelete(menu.id)}
>
<DeleteIcon />
</IconButtonTableCell>
</TableRow>
);
},
() => (
<TableRow>
2019-08-09 11:14:35 +00:00
<TableCell colSpan={numberOfColumns}>
<FormattedMessage
defaultMessage="No menus found"
id="menuListNoMenus"
/>
2019-08-09 11:14:35 +00:00
</TableCell>
2019-06-19 14:40:52 +00:00
</TableRow>
)
)}
</TableBody>
</Table>
</Card>
)
);
MenuList.displayName = "MenuList";
export default MenuList;