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

186 lines
5.6 KiB
TypeScript
Raw Normal View History

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";
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";
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";
2019-08-09 11:14:35 +00:00
import { getUserName, maybe, renderCollection } from "@saleor/misc";
2019-12-17 17:13:56 +00:00
import { ListActions, ListProps, SortPage } from "@saleor/types";
import { CustomerListUrlSortField } from "@saleor/customers/urls";
import TableCellHeader from "@saleor/components/TableCellHeader";
import { getArrowDirection } from "@saleor/utils/sort";
2019-06-19 14:40:52 +00:00
import { ListCustomers_customers_edges_node } from "../../types/ListCustomers";
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
[theme.breakpoints.up("lg")]: {
colEmail: {},
colName: {},
colOrders: {
width: 200
}
},
2019-06-19 14:40:52 +00:00
colEmail: {},
2019-12-03 15:28:40 +00:00
colName: {
paddingLeft: 0
},
2019-06-19 14:40:52 +00:00
colOrders: {
2019-12-03 15:28:40 +00:00
textAlign: "center"
},
tableRow: {
cursor: "pointer"
2019-06-19 14:40:52 +00:00
}
2019-12-03 15:28:40 +00:00
}),
{ name: "CustomerList" }
);
2019-06-19 14:40:52 +00:00
2019-12-17 17:13:56 +00:00
export interface CustomerListProps
extends ListProps,
ListActions,
SortPage<CustomerListUrlSortField> {
2019-06-19 14:40:52 +00:00
customers: ListCustomers_customers_edges_node[];
}
2019-08-09 11:14:35 +00:00
const numberOfColumns = 4;
2019-10-30 14:34:24 +00:00
const CustomerList: React.FC<CustomerListProps> = props => {
const {
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,
2019-12-17 17:13:56 +00:00
onSort,
2019-06-19 14:40:52 +00:00
toolbar,
toggle,
toggleAll,
selected,
2019-12-17 17:13:56 +00:00
sort,
2019-06-19 14:40:52 +00:00
isChecked
2019-10-30 14:34:24 +00:00
} = props;
const classes = useStyles(props);
return (
2019-11-04 14:25:23 +00:00
<ResponsiveTable>
2019-09-11 14:39:52 +00:00
<TableHead
colSpan={numberOfColumns}
selected={selected}
disabled={disabled}
items={customers}
toggleAll={toggleAll}
toolbar={toolbar}
>
2019-12-17 17:13:56 +00:00
<TableCellHeader
direction={
sort.sort === CustomerListUrlSortField.name
? getArrowDirection(sort.asc)
: undefined
}
arrowPosition="right"
onClick={() => onSort(CustomerListUrlSortField.name)}
className={classes.colName}
>
2019-09-11 14:39:52 +00:00
<FormattedMessage defaultMessage="Customer Name" />
2019-12-17 17:13:56 +00:00
</TableCellHeader>
<TableCellHeader
direction={
sort.sort === CustomerListUrlSortField.email
? getArrowDirection(sort.asc)
: undefined
}
onClick={() => onSort(CustomerListUrlSortField.email)}
className={classes.colEmail}
>
2019-09-11 14:39:52 +00:00
<FormattedMessage defaultMessage="Customer Email" />
2019-12-17 17:13:56 +00:00
</TableCellHeader>
<TableCellHeader
direction={
sort.sort === CustomerListUrlSortField.orders
? getArrowDirection(sort.asc)
: undefined
}
textAlign="center"
onClick={() => onSort(CustomerListUrlSortField.orders)}
className={classes.colOrders}
>
2019-09-11 14:39:52 +00:00
<FormattedMessage defaultMessage="No. of Orders" />
2019-12-17 17:13:56 +00:00
</TableCellHeader>
2019-09-11 14:39:52 +00:00
</TableHead>
<TableFooter>
<TableRow>
<TablePagination
colSpan={numberOfColumns}
settings={settings}
hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}
onNextPage={onNextPage}
onUpdateListSettings={onUpdateListSettings}
hasPreviousPage={
pageInfo && !disabled ? pageInfo.hasPreviousPage : false
}
onPreviousPage={onPreviousPage}
/>
</TableRow>
</TableFooter>
<TableBody>
{renderCollection(
customers,
customer => {
const isSelected = customer ? isChecked(customer.id) : false;
2019-06-19 14:40:52 +00:00
2019-09-11 14:39:52 +00:00
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}
disableClickPropagation
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 />
)}
2019-06-19 14:40:52 +00:00
</TableCell>
</TableRow>
2019-09-11 14:39:52 +00:00
);
},
() => (
<TableRow>
<TableCell colSpan={numberOfColumns}>
<FormattedMessage defaultMessage="No customers found" />
</TableCell>
</TableRow>
)
)}
</TableBody>
2019-11-04 14:25:23 +00:00
</ResponsiveTable>
2019-10-30 14:34:24 +00:00
);
};
2019-06-19 14:40:52 +00:00
CustomerList.displayName = "CustomerList";
export default CustomerList;