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

306 lines
9.3 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";
import Checkbox from "@saleor/components/Checkbox";
import Date from "@saleor/components/Date";
import Money from "@saleor/components/Money";
import Percent from "@saleor/components/Percent";
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 TableCellHeader from "@saleor/components/TableCellHeader";
2019-06-19 14:40:52 +00:00
import TableHead from "@saleor/components/TableHead";
import TablePagination from "@saleor/components/TablePagination";
import { VoucherListUrlSortField } from "@saleor/discounts/urls";
2019-08-09 11:14:35 +00:00
import { maybe, renderCollection } from "@saleor/misc";
2019-12-17 17:13:56 +00:00
import { ListActions, ListProps, SortPage } from "@saleor/types";
2019-08-09 11:14:35 +00:00
import { DiscountValueTypeEnum } from "@saleor/types/globalTypes";
2019-12-17 17:13:56 +00:00
import { getArrowDirection } from "@saleor/utils/sort";
import React from "react";
import { FormattedMessage } from "react-intl";
2019-06-19 14:40:52 +00:00
import { VoucherList_vouchers_edges_node } from "../../types/VoucherList";
2019-12-17 17:13:56 +00:00
export interface VoucherListProps
extends ListProps,
ListActions,
SortPage<VoucherListUrlSortField> {
2019-06-19 14:40:52 +00:00
defaultCurrency: string;
vouchers: VoucherList_vouchers_edges_node[];
}
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
[theme.breakpoints.up("lg")]: {
colEnd: {
width: 180
},
colMinSpent: {
width: 150
},
colName: {},
colStart: {
width: 180
},
colUses: {
width: 150
},
colValue: {
width: 150
}
},
2019-06-19 14:40:52 +00:00
colEnd: {
2019-12-03 15:28:40 +00:00
textAlign: "right"
2019-06-19 14:40:52 +00:00
},
colMinSpent: {
2019-12-03 15:28:40 +00:00
textAlign: "right"
},
colName: {
paddingLeft: 0
2019-09-02 08:44:26 +00:00
},
2019-06-19 14:40:52 +00:00
colStart: {
2019-12-03 15:28:40 +00:00
textAlign: "right"
2019-06-19 14:40:52 +00:00
},
colUses: {
2019-12-03 15:28:40 +00:00
textAlign: "right"
2019-06-19 14:40:52 +00:00
},
colValue: {
2019-12-03 15:28:40 +00:00
textAlign: "right"
},
tableRow: {
cursor: "pointer"
},
textRight: {
textAlign: "right"
2019-06-19 14:40:52 +00:00
}
2019-12-03 15:28:40 +00:00
}),
{ name: "VoucherList" }
);
2019-06-19 14:40:52 +00:00
2019-08-09 11:14:35 +00:00
const numberOfColumns = 7;
2019-10-30 14:34:24 +00:00
const VoucherList: React.FC<VoucherListProps> = props => {
const {
2019-08-09 11:14:35 +00:00
settings,
2019-06-19 14:40:52 +00:00
defaultCurrency,
disabled,
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
pageInfo,
vouchers,
isChecked,
selected,
2019-12-17 17:13:56 +00:00
sort,
2019-06-19 14:40:52 +00:00
toggle,
toggleAll,
toolbar
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-12 10:06:28 +00:00
<TableHead
colSpan={numberOfColumns}
selected={selected}
disabled={disabled}
items={vouchers}
toggleAll={toggleAll}
toolbar={toolbar}
>
2019-12-17 17:13:56 +00:00
<TableCellHeader
direction={
sort.sort === VoucherListUrlSortField.code
? getArrowDirection(sort.asc)
: undefined
}
arrowPosition="right"
onClick={() => onSort(VoucherListUrlSortField.code)}
className={classes.colName}
>
2019-09-12 10:06:28 +00:00
<FormattedMessage defaultMessage="Code" description="voucher code" />
2019-12-17 17:13:56 +00:00
</TableCellHeader>
<TableCellHeader
direction={
sort.sort === VoucherListUrlSortField.minSpent
? getArrowDirection(sort.asc)
: undefined
}
onClick={() => onSort(VoucherListUrlSortField.minSpent)}
className={classes.colMinSpent}
>
2019-09-12 10:06:28 +00:00
<FormattedMessage
defaultMessage="Min. Spent"
description="minimum amount of spent money to activate voucher"
/>
2019-12-17 17:13:56 +00:00
</TableCellHeader>
<TableCellHeader
direction={
sort.sort === VoucherListUrlSortField.startDate
? getArrowDirection(sort.asc)
: undefined
}
onClick={() => onSort(VoucherListUrlSortField.startDate)}
className={classes.colStart}
>
2019-09-12 10:06:28 +00:00
<FormattedMessage
defaultMessage="Starts"
description="voucher is active from date"
/>
2019-12-17 17:13:56 +00:00
</TableCellHeader>
<TableCellHeader
direction={
sort.sort === VoucherListUrlSortField.endDate
? getArrowDirection(sort.asc)
: undefined
}
onClick={() => onSort(VoucherListUrlSortField.endDate)}
className={classes.colEnd}
>
2019-09-12 10:06:28 +00:00
<FormattedMessage
defaultMessage="Ends"
description="voucher is active until date"
/>
2019-12-17 17:13:56 +00:00
</TableCellHeader>
<TableCellHeader
direction={
sort.sort === VoucherListUrlSortField.value
? getArrowDirection(sort.asc)
: undefined
}
onClick={() => onSort(VoucherListUrlSortField.value)}
className={classes.colValue}
>
2019-09-12 10:06:28 +00:00
<FormattedMessage
defaultMessage="Value"
description="voucher value"
/>
2019-12-17 17:13:56 +00:00
</TableCellHeader>
<TableCellHeader
direction={
sort.sort === VoucherListUrlSortField.limit
? getArrowDirection(sort.asc)
: undefined
}
onClick={() => onSort(VoucherListUrlSortField.limit)}
className={classes.colUses}
>
2019-09-12 10:06:28 +00:00
<FormattedMessage defaultMessage="Uses" description="voucher uses" />
2019-12-17 17:13:56 +00:00
</TableCellHeader>
2019-09-12 10:06:28 +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(
vouchers,
voucher => {
const isSelected = voucher ? isChecked(voucher.id) : false;
2019-06-19 14:40:52 +00:00
2019-09-12 10:06:28 +00:00
return (
<TableRow
className={!!voucher ? classes.tableRow : undefined}
hover={!!voucher}
key={voucher ? voucher.id : "skeleton"}
selected={isSelected}
onClick={voucher ? onRowClick(voucher.id) : undefined}
>
<TableCell padding="checkbox">
<Checkbox
checked={isSelected}
disabled={disabled}
disableClickPropagation
onChange={() => toggle(voucher.id)}
/>
</TableCell>
<TableCell className={classes.colName}>
{maybe<React.ReactNode>(() => voucher.code, <Skeleton />)}
</TableCell>
<TableCell className={classes.colMinSpent}>
2020-01-09 11:13:24 +00:00
{voucher && voucher.minSpent ? (
<Money money={voucher.minSpent} />
) : voucher && voucher.minSpent === null ? (
2019-09-12 10:06:28 +00:00
"-"
) : (
<Skeleton />
)}
</TableCell>
<TableCell className={classes.colStart}>
{voucher && voucher.startDate ? (
<Date date={voucher.startDate} />
) : (
<Skeleton />
)}
</TableCell>
<TableCell className={classes.colEnd}>
{voucher && voucher.endDate ? (
<Date date={voucher.endDate} />
) : voucher && voucher.endDate === null ? (
"-"
) : (
<Skeleton />
)}
</TableCell>
<TableCell
className={classes.colValue}
2019-06-19 14:40:52 +00:00
onClick={voucher ? onRowClick(voucher.id) : undefined}
>
2019-09-12 10:06:28 +00:00
{voucher &&
voucher.discountValueType &&
voucher.discountValue ? (
voucher.discountValueType ===
DiscountValueTypeEnum.FIXED ? (
<Money
money={{
amount: voucher.discountValue,
currency: defaultCurrency
}}
/>
2019-06-19 14:40:52 +00:00
) : (
2019-09-12 10:06:28 +00:00
<Percent amount={voucher.discountValue} />
)
) : (
<Skeleton />
)}
</TableCell>
<TableCell className={classes.colUses}>
2019-11-26 14:49:39 +00:00
{maybe<React.ReactNode>(
() =>
voucher.usageLimit === null ? "-" : voucher.usageLimit,
2019-09-12 10:06:28 +00:00
<Skeleton />
)}
2019-08-09 11:14:35 +00:00
</TableCell>
2019-06-19 14:40:52 +00:00
</TableRow>
2019-09-12 10:06:28 +00:00
);
},
() => (
<TableRow>
<TableCell colSpan={numberOfColumns}>
<FormattedMessage defaultMessage="No vouchers 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
VoucherList.displayName = "VoucherList";
export default VoucherList;