2021-08-16 13:44:00 +00:00
|
|
|
import { TableCell } from "@material-ui/core";
|
2021-09-14 13:57:02 +00:00
|
|
|
import DeleteIconButton from "@saleor/components/DeleteIconButton";
|
2021-08-16 13:44:00 +00:00
|
|
|
import TableCellHeader, {
|
2022-01-25 12:44:19 +00:00
|
|
|
TableCellHeaderArrowDirection,
|
2021-08-16 13:44:00 +00:00
|
|
|
TableCellHeaderProps
|
|
|
|
} from "@saleor/components/TableCellHeader";
|
|
|
|
import TableHead from "@saleor/components/TableHead";
|
2022-02-02 08:48:12 +00:00
|
|
|
import TooltipTableCellHeader from "@saleor/components/TooltipTableCellHeader";
|
|
|
|
import { commonTooltipMessages } from "@saleor/components/TooltipTableCellHeader/messages";
|
2021-08-16 13:44:00 +00:00
|
|
|
import Label, {
|
|
|
|
LabelSizes
|
|
|
|
} from "@saleor/orders/components/OrderHistory/Label";
|
2022-01-25 12:44:19 +00:00
|
|
|
import { getArrowDirection } from "@saleor/utils/sort";
|
2021-08-16 13:44:00 +00:00
|
|
|
import React from "react";
|
|
|
|
import { MessageDescriptor, useIntl } from "react-intl";
|
|
|
|
|
2022-02-02 08:48:12 +00:00
|
|
|
import { messages as filterLabels } from "../../GiftCardListSearchAndFilters/filters";
|
2021-09-14 13:57:02 +00:00
|
|
|
import { giftCardsListTableMessages as messages } from "../../messages";
|
|
|
|
import useGiftCardListDialogs from "../../providers/GiftCardListDialogsProvider/hooks/useGiftCardListDialogs";
|
2022-01-25 12:44:19 +00:00
|
|
|
import useGiftCardListSort from "../../providers/GiftCardListDialogsProvider/hooks/useGiftCardListSort";
|
2021-09-14 13:57:02 +00:00
|
|
|
import useGiftCardList from "../../providers/GiftCardListProvider/hooks/useGiftCardList";
|
|
|
|
import useGiftCardListBulkActions from "../../providers/GiftCardListProvider/hooks/useGiftCardListBulkActions";
|
2022-01-25 12:44:19 +00:00
|
|
|
import { canBeSorted } from "../../sort";
|
2021-09-14 13:57:02 +00:00
|
|
|
import { useTableStyles as useStyles } from "../../styles";
|
2022-01-25 12:44:19 +00:00
|
|
|
import { GiftCardUrlSortField } from "../../types";
|
2021-09-14 13:57:02 +00:00
|
|
|
import BulkEnableDisableSection from "./BulkEnableDisableSection";
|
2021-08-16 13:44:00 +00:00
|
|
|
|
|
|
|
interface HeaderItem {
|
|
|
|
title?: MessageDescriptor;
|
|
|
|
options?: TableCellHeaderProps;
|
2022-01-25 12:44:19 +00:00
|
|
|
onClick?: () => void;
|
|
|
|
direction?: TableCellHeaderArrowDirection;
|
2021-08-16 13:44:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 12:44:19 +00:00
|
|
|
interface GiftCardsListTableHeaderProps {
|
|
|
|
isCurrencySelected: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GiftCardsListTableHeader: React.FC<GiftCardsListTableHeaderProps> = ({
|
|
|
|
isCurrencySelected
|
|
|
|
}) => {
|
2021-08-16 13:44:00 +00:00
|
|
|
const intl = useIntl();
|
|
|
|
const classes = useStyles({});
|
2021-09-14 13:57:02 +00:00
|
|
|
|
2021-08-16 13:44:00 +00:00
|
|
|
const { giftCards, numberOfColumns, loading } = useGiftCardList();
|
|
|
|
const { toggleAll, listElements } = useGiftCardListBulkActions();
|
2021-09-14 13:57:02 +00:00
|
|
|
const { openDeleteDialog } = useGiftCardListDialogs();
|
2022-01-25 12:44:19 +00:00
|
|
|
const { onSort, sort } = useGiftCardListSort();
|
|
|
|
|
|
|
|
const getDirection = (sortField: GiftCardUrlSortField) =>
|
|
|
|
sort.sort === sortField ? getArrowDirection(sort.asc) : undefined;
|
2021-08-16 13:44:00 +00:00
|
|
|
|
|
|
|
const headerItems: HeaderItem[] = [
|
|
|
|
{
|
|
|
|
title: messages.giftCardsTableColumnGiftCardTitle,
|
|
|
|
options: {
|
|
|
|
className: classes.colCardCode,
|
|
|
|
textAlign: "left"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: messages.giftCardsTableColumnTagTitle
|
|
|
|
},
|
|
|
|
{
|
2022-01-25 12:44:19 +00:00
|
|
|
title: messages.giftCardsTableColumnProductTitle,
|
|
|
|
onClick: () => onSort(GiftCardUrlSortField.product),
|
|
|
|
direction: getDirection(GiftCardUrlSortField.product)
|
2021-08-16 13:44:00 +00:00
|
|
|
},
|
|
|
|
{
|
2022-01-25 12:44:19 +00:00
|
|
|
title: messages.giftCardsTableColumnCustomerTitle,
|
|
|
|
onClick: () => onSort(GiftCardUrlSortField.usedBy),
|
|
|
|
direction: getDirection(GiftCardUrlSortField.usedBy)
|
2021-08-16 13:44:00 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2022-02-02 08:48:12 +00:00
|
|
|
const headerTooltipItem: HeaderItem & {
|
|
|
|
disabled: boolean;
|
|
|
|
tooltip: string | React.ReactNodeArray;
|
|
|
|
} = {
|
|
|
|
title: messages.giftCardsTableColumnBalanceTitle,
|
|
|
|
options: {
|
|
|
|
className: classes.colBalance,
|
|
|
|
textAlign: "right"
|
|
|
|
},
|
|
|
|
onClick: () => onSort(GiftCardUrlSortField.balance),
|
|
|
|
direction: getDirection(GiftCardUrlSortField.balance),
|
|
|
|
disabled: !canBeSorted(GiftCardUrlSortField.balance, isCurrencySelected),
|
|
|
|
tooltip: intl.formatMessage(commonTooltipMessages.noFilterSelected, {
|
|
|
|
filterName: <b>{filterLabels.currencyLabel.defaultMessage}</b>
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
const { title, ...headerTooltipItemProps } = headerTooltipItem;
|
|
|
|
|
2021-08-16 13:44:00 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<colgroup>
|
|
|
|
<col />
|
|
|
|
<col className={classes.colCardCode} />
|
|
|
|
<col className={classes.colBase} />
|
|
|
|
<col className={classes.colBase} />
|
|
|
|
<col className={classes.colBase} />
|
|
|
|
<col className={classes.colBalance} />
|
|
|
|
<col className={classes.colDelete} />
|
|
|
|
</colgroup>
|
|
|
|
<TableHead
|
|
|
|
disabled={loading}
|
|
|
|
colSpan={numberOfColumns}
|
|
|
|
selected={listElements.length}
|
|
|
|
items={giftCards}
|
|
|
|
toggleAll={toggleAll}
|
2021-09-14 13:57:02 +00:00
|
|
|
toolbar={
|
2022-01-28 12:34:20 +00:00
|
|
|
<div className={classes.toolbar}>
|
2021-09-14 13:57:02 +00:00
|
|
|
<BulkEnableDisableSection />
|
2022-01-25 12:44:19 +00:00
|
|
|
<DeleteIconButton onClick={() => openDeleteDialog()} />
|
2022-01-28 12:34:20 +00:00
|
|
|
</div>
|
2021-09-14 13:57:02 +00:00
|
|
|
}
|
2021-08-16 13:44:00 +00:00
|
|
|
>
|
2022-02-02 08:48:12 +00:00
|
|
|
{headerItems.map(({ title, options, onClick, direction }) => (
|
|
|
|
<TableCellHeader
|
|
|
|
{...options}
|
|
|
|
onClick={onClick}
|
|
|
|
direction={direction}
|
|
|
|
key={title.defaultMessage}
|
|
|
|
>
|
|
|
|
<Label text={intl.formatMessage(title)} size={LabelSizes.md} />
|
|
|
|
</TableCellHeader>
|
|
|
|
))}
|
|
|
|
<TooltipTableCellHeader {...headerTooltipItemProps}>
|
|
|
|
<Label
|
|
|
|
text={intl.formatMessage(headerTooltipItem.title)}
|
|
|
|
size={LabelSizes.md}
|
|
|
|
/>
|
|
|
|
</TooltipTableCellHeader>
|
2021-08-16 13:44:00 +00:00
|
|
|
<TableCell className={classes.colDelete} />
|
|
|
|
</TableHead>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default GiftCardsListTableHeader;
|