saleor-dashboard/src/customers/components/CustomerList/CustomerList.tsx

158 lines
4.7 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";
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 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 { getUserName, maybe, renderCollection } from "@saleor/misc";
import { ListActions, ListProps } from "@saleor/types";
2019-06-19 14:40:52 +00:00
import { ListCustomers_customers_edges_node } from "../../types/ListCustomers";
const styles = (theme: Theme) =>
createStyles({
[theme.breakpoints.up("lg")]: {
colEmail: {},
colName: {},
colOrders: {
width: 200
}
},
colEmail: {},
2019-09-02 08:44:26 +00:00
colName: {
paddingLeft: 0
},
2019-06-19 14:40:52 +00:00
colOrders: {
textAlign: "center"
},
tableRow: {
cursor: "pointer"
}
});
export interface CustomerListProps
extends ListProps,
ListActions,
WithStyles<typeof styles> {
customers: ListCustomers_customers_edges_node[];
}
2019-08-09 11:14:35 +00:00
const numberOfColumns = 4;
2019-06-19 14:40:52 +00:00
const CustomerList = withStyles(styles, { name: "CustomerList" })(
({
classes,
2019-08-09 11:14:35 +00:00
settings,
2019-06-19 14:40:52 +00:00
disabled,
customers,
pageInfo,
onNextPage,
onPreviousPage,
2019-08-09 11:14:35 +00:00
onUpdateListSettings,
2019-06-19 14:40:52 +00:00
onRowClick,
toolbar,
toggle,
toggleAll,
selected,
isChecked
}: CustomerListProps) => (
<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={customers}
toggleAll={toggleAll}
toolbar={toolbar}
>
<TableCell className={classes.colName}>
<FormattedMessage defaultMessage="Customer Name" />
2019-06-19 14:40:52 +00:00
</TableCell>
<TableCell className={classes.colEmail}>
<FormattedMessage defaultMessage="Customer Email" />
2019-06-19 14:40:52 +00:00
</TableCell>
<TableCell className={classes.colOrders}>
<FormattedMessage defaultMessage="No. of Orders" />
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(
customers,
customer => {
const isSelected = customer ? isChecked(customer.id) : false;
return (
<TableRow
className={!!customer ? classes.tableRow : undefined}
hover={!!customer}
key={customer ? customer.id : "skeleton"}
selected={isSelected}
onClick={customer ? onRowClick(customer.id) : undefined}
>
<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(customer.id)}
/>
</TableCell>
<TableCell className={classes.colName}>
{getUserName(customer)}
</TableCell>
<TableCell className={classes.colEmail}>
{maybe<React.ReactNode>(() => customer.email, <Skeleton />)}
</TableCell>
<TableCell className={classes.colOrders}>
{maybe<React.ReactNode>(
() => customer.orders.totalCount,
<Skeleton />
)}
</TableCell>
</TableRow>
);
},
() => (
<TableRow>
2019-08-09 11:14:35 +00:00
<TableCell colSpan={numberOfColumns}>
<FormattedMessage defaultMessage="No customers found" />
2019-06-19 14:40:52 +00:00
</TableCell>
</TableRow>
)
)}
</TableBody>
</Table>
</Card>
)
);
CustomerList.displayName = "CustomerList";
export default CustomerList;