saleor-dashboard/src/discounts/components/DiscountCollections/DiscountCollections.tsx

182 lines
5.7 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Button from "@material-ui/core/Button";
import Card from "@material-ui/core/Card";
import IconButton from "@material-ui/core/IconButton";
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 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, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import CardTitle from "@saleor/components/CardTitle";
import Checkbox from "@saleor/components/Checkbox";
2019-11-04 14:25:23 +00:00
import ResponsiveTable from "@saleor/components/ResponsiveTable";
2019-06-19 14:40:52 +00:00
import Skeleton from "@saleor/components/Skeleton";
import TableHead from "@saleor/components/TableHead";
import TablePagination from "@saleor/components/TablePagination";
import { maybe, renderCollection } from "../../../misc";
import { ListActions, ListProps } from "../../../types";
import { SaleDetails_sale } from "../../types/SaleDetails";
import { VoucherDetails_voucher } from "../../types/VoucherDetails";
export interface DiscountCollectionsProps extends ListProps, ListActions {
discount: SaleDetails_sale | VoucherDetails_voucher;
onCollectionAssign: () => void;
onCollectionUnassign: (id: string) => void;
}
2019-10-30 14:34:24 +00:00
const useStyles = makeStyles(theme => ({
iconCell: {
"&:last-child": {
paddingRight: 0
2019-06-19 14:40:52 +00:00
},
2019-10-30 14:34:24 +00:00
width: 48 + theme.spacing(0.5)
},
tableRow: {
cursor: "pointer"
},
textRight: {
textAlign: "right"
},
wideColumn: {
width: "60%"
}
}));
2019-08-09 11:14:35 +00:00
const numberOfColumns = 4;
2019-10-30 14:34:24 +00:00
const DiscountCollections: React.FC<DiscountCollectionsProps> = props => {
const {
2019-06-19 14:40:52 +00:00
discount: sale,
2019-10-30 14:34:24 +00:00
2019-06-19 14:40:52 +00:00
disabled,
pageInfo,
onCollectionAssign,
onCollectionUnassign,
onRowClick,
onPreviousPage,
onNextPage,
isChecked,
selected,
toggle,
toggleAll,
toolbar
2019-10-30 14:34:24 +00:00
} = props;
const classes = useStyles(props);
const intl = useIntl();
2019-10-30 14:34:24 +00:00
return (
<Card>
<CardTitle
title={intl.formatMessage({
defaultMessage: "Eligible Collections",
description: "section header"
})}
toolbar={
<Button color="primary" onClick={onCollectionAssign}>
<FormattedMessage
defaultMessage="Assign collections"
description="button"
/>
</Button>
}
/>
2019-11-04 14:25:23 +00:00
<ResponsiveTable>
2019-10-30 14:34:24 +00:00
<TableHead
colSpan={numberOfColumns}
selected={selected}
disabled={disabled}
items={maybe(() => sale.collections.edges.map(edge => edge.node))}
toggleAll={toggleAll}
toolbar={toolbar}
>
<TableCell className={classes.wideColumn}>
<FormattedMessage defaultMessage="Collection name" />
</TableCell>
<TableCell className={classes.textRight}>
<FormattedMessage
defaultMessage="Products"
description="number of products"
/>
</TableCell>
<TableCell />
</TableHead>
<TableFooter>
<TableRow>
<TablePagination
colSpan={numberOfColumns}
hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}
onNextPage={onNextPage}
hasPreviousPage={
pageInfo && !disabled ? pageInfo.hasPreviousPage : false
}
onPreviousPage={onPreviousPage}
/>
</TableRow>
</TableFooter>
<TableBody>
{renderCollection(
maybe(() => sale.collections.edges.map(edge => edge.node)),
collection => {
const isSelected = collection ? isChecked(collection.id) : false;
return (
<TableRow
selected={isSelected}
hover={!!collection}
key={collection ? collection.id : "skeleton"}
onClick={collection && onRowClick(collection.id)}
className={classes.tableRow}
>
<TableCell padding="checkbox">
<Checkbox
checked={isSelected}
disabled={disabled}
disableClickPropagation
onChange={() => toggle(collection.id)}
/>
</TableCell>
<TableCell>
{maybe<React.ReactNode>(
() => collection.name,
<Skeleton />
)}
</TableCell>
<TableCell className={classes.textRight}>
{maybe<React.ReactNode>(
() => collection.products.totalCount,
<Skeleton />
)}
</TableCell>
<TableCell className={classes.iconCell}>
<IconButton
disabled={!collection || disabled}
onClick={event => {
event.stopPropagation();
onCollectionUnassign(collection.id);
}}
>
<DeleteIcon color="primary" />
</IconButton>
2019-06-19 14:40:52 +00:00
</TableCell>
</TableRow>
2019-10-30 14:34:24 +00:00
);
},
() => (
<TableRow>
<TableCell colSpan={numberOfColumns}>
<FormattedMessage defaultMessage="No collections found" />
</TableCell>
</TableRow>
)
)}
</TableBody>
2019-11-04 14:25:23 +00:00
</ResponsiveTable>
2019-10-30 14:34:24 +00:00
</Card>
);
};
2019-06-19 14:40:52 +00:00
DiscountCollections.displayName = "DiscountCollections";
export default DiscountCollections;