Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add voucher list components

dominik-zeglend0e8a3d7dce9
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherList/VoucherList.tsx b/saleor/static/dashboard-next/discounts/components/VoucherList/VoucherList.tsx
new file mode 100644
index 0000000000..83362e80c3
--- /dev/null
+++ b/saleor/static/dashboard-next/discounts/components/VoucherList/VoucherList.tsx
@@ -0,0 +1,186 @@
+import Card from "@material-ui/core/Card";
+import { createStyles, 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 TableHead from "@material-ui/core/TableHead";
+import TableRow from "@material-ui/core/TableRow";
+import * as React from "react";
+
+import DateFormatter from "../../../components/DateFormatter";
+import Money from "../../../components/Money";
+import Percent from "../../../components/Percent";
+import Skeleton from "../../../components/Skeleton";
+import TablePagination from "../../../components/TablePagination";
+import i18n from "../../../i18n";
+import { maybe, renderCollection } from "../../../misc";
+import { ListProps } from "../../../types";
+import { VoucherDiscountValueType } from "../../../types/globalTypes";
+import { VoucherList_vouchers_edges_node } from "../../types/VoucherList";
+
+export interface VoucherListProps extends ListProps {
+ defaultCurrency: string;
+ vouchers: VoucherList_vouchers_edges_node[];
+}
+
+const styles = createStyles({
+ tableRow: {
+ cursor: "pointer"
+ },
+ textRight: {
+ textAlign: "right"
+ },
+ wideColumn: {
+ width: "60%"
+ }
+});
+
+const VoucherList = withStyles(styles, {
+ name: "VoucherList"
+})(
+ ({
+ classes,
+ defaultCurrency,
+ disabled,
+ onNextPage,
+ onPreviousPage,
+ onRowClick,
+ pageInfo,
+ vouchers
+ }: VoucherListProps & WithStyles<typeof styles>) => (
+ <Card>
+ <Table>
+ <TableHead>
+ <TableRow>
+ <TableCell className={classes.wideColumn}>
+ {i18n.t("Name", {
+ context: "voucher list table header"
+ })}
+ </TableCell>
+ <TableCell className={classes.textRight}>
+ {i18n.t("Min. Spent", {
+ context: "voucher list table header"
+ })}
+ </TableCell>
+ <TableCell className={classes.textRight}>
+ {i18n.t("Starts", {
+ context: "voucher list table header"
+ })}
+ </TableCell>
+ <TableCell className={classes.textRight}>
+ {i18n.t("Ends", {
+ context: "voucher list table header"
+ })}
+ </TableCell>
+ <TableCell className={classes.textRight}>
+ {i18n.t("Value", {
+ context: "voucher list table header"
+ })}
+ </TableCell>
+ <TableCell className={classes.textRight}>
+ {i18n.t("Uses", {
+ context: "voucher list table header"
+ })}
+ </TableCell>
+ </TableRow>
+ </TableHead>
+ <TableFooter>
+ <TableRow>
+ <TablePagination
+ colSpan={6}
+ hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}
+ onNextPage={onNextPage}
+ hasPreviousPage={
+ pageInfo && !disabled ? pageInfo.hasPreviousPage : false
+ }
+ onPreviousPage={onPreviousPage}
+ />
+ </TableRow>
+ </TableFooter>
+ <TableBody>
+ {renderCollection(
+ vouchers,
+ voucher => (
+ <TableRow
+ className={!!voucher ? classes.tableRow : undefined}
+ hover={!!voucher}
+ key={voucher ? voucher.id : "skeleton"}
+ >
+ <TableCell
+ className={classes.textRight}
+ onClick={voucher ? onRowClick(voucher.id) : undefined}
+ >
+ {maybe<React.ReactNode>(() => voucher.name, <Skeleton />)}
+ </TableCell>
+ <TableCell className={classes.textRight}>
+ {voucher && voucher.minAmountSpent ? (
+ <Money money={voucher.minAmountSpent} />
+ ) : voucher && voucher.minAmountSpent === null ? (
+ "-"
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ <TableCell className={classes.textRight}>
+ {voucher && voucher.startDate ? (
+ <DateFormatter date={voucher.startDate} />
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ <TableCell className={classes.textRight}>
+ {voucher && voucher.endDate ? (
+ <DateFormatter date={voucher.endDate} />
+ ) : voucher && voucher.endDate === null ? (
+ "-"
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ <TableCell
+ className={classes.textRight}
+ onClick={voucher ? onRowClick(voucher.id) : undefined}
+ >
+ {voucher &&
+ voucher.discountValueType &&
+ voucher.discountValue ? (
+ voucher.discountValueType ===
+ VoucherDiscountValueType.FIXED ? (
+ <Money
+ money={{
+ amount: voucher.discountValue,
+ currency: defaultCurrency
+ }}
+ />
+ ) : (
+ <Percent amount={voucher.discountValue} />
+ )
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ <TableCell className={classes.textRight}>
+ {voucher && voucher.usageLimit ? (
+ voucher.usageLimit
+ ) : voucher && voucher.usageLimit === null ? (
+ "-"
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ </TableRow>
+ ),
+ () => (
+ <TableRow>
+ <TableCell colSpan={6}>{i18n.t("No vouchers found")}</TableCell>
+ </TableRow>
+ )
+ )}
+ </TableBody>
+ </Table>
+ </Card>
+ )
+);
+VoucherList.displayName = "VoucherList";
+export default VoucherList;
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherList/index.ts b/saleor/static/dashboard-next/discounts/components/VoucherList/index.ts
new file mode 100644
index 0000000000..c844096329
--- /dev/null
+++ b/saleor/static/dashboard-next/discounts/components/VoucherList/index.ts
@@ -0,0 +1,2 @@
+export { default } from "./VoucherList";
+export * from "./VoucherList";
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherListPage/VoucherListPage.tsx b/saleor/static/dashboard-next/discounts/components/VoucherListPage/VoucherListPage.tsx
new file mode 100644
index 0000000000..9d90b4a0f8
--- /dev/null
+++ b/saleor/static/dashboard-next/discounts/components/VoucherListPage/VoucherListPage.tsx
@@ -0,0 +1,46 @@
+import Button from "@material-ui/core/Button";
+import AddIcon from "@material-ui/icons/Add";
+import * as React from "react";
+
+import Container from "../../../components/Container";
+import PageHeader from "../../../components/PageHeader";
+import i18n from "../../../i18n";
+import { PageListProps } from "../../../types";
+import { VoucherList_vouchers_edges_node } from "../../types/VoucherList";
+import VoucherList from "../VoucherList";
+
+export interface VoucherListPageProps extends PageListProps {
+ defaultCurrency: string;
+ vouchers: VoucherList_vouchers_edges_node[];
+}
+
+const VoucherListPage: React.StatelessComponent<VoucherListPageProps> = ({
+ defaultCurrency,
+ disabled,
+ onAdd,
+ onNextPage,
+ onPreviousPage,
+ onRowClick,
+ pageInfo,
+ vouchers
+}) => (
+ <Container width="md">
+ <PageHeader title={i18n.t("Vouchers")}>
+ <Button onClick={onAdd} variant="contained" color="secondary">
+ {i18n.t("Add voucher")}
+ <AddIcon />
+ </Button>
+ </PageHeader>
+ <VoucherList
+ defaultCurrency={defaultCurrency}
+ disabled={disabled}
+ onNextPage={onNextPage}
+ onPreviousPage={onPreviousPage}
+ onRowClick={onRowClick}
+ pageInfo={pageInfo}
+ vouchers={vouchers}
+ />
+ </Container>
+);
+VoucherListPage.displayName = "VoucherListPage";
+export default VoucherListPage;
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherListPage/index.ts b/saleor/static/dashboard-next/discounts/components/VoucherListPage/index.ts
new file mode 100644
index 0000000000..84b3fa702e
--- /dev/null
+++ b/saleor/static/dashboard-next/discounts/components/VoucherListPage/index.ts
@@ -0,0 +1,2 @@
+export { default } from "./VoucherListPage";
+export * from "./VoucherListPage";
diff --git a/saleor/static/dashboard-next/discounts/fixtures.ts b/saleor/static/dashboard-next/discounts/fixtures.ts
index aea6d2f48d..d1b05d1fbe 100644
--- a/saleor/static/dashboard-next/discounts/fixtures.ts
+++ b/saleor/static/dashboard-next/discounts/fixtures.ts
@@ -1,5 +1,6 @@
-import { SaleType } from "../types/globalTypes";
+import { SaleType, VoucherDiscountValueType } from "../types/globalTypes";
import { SaleList_sales_edges_node } from "./types/SaleList";
+import { VoucherList_vouchers_edges_node } from "./types/VoucherList";
export const saleList: SaleList_sales_edges_node[] = [
{
@@ -48,3 +49,32 @@ export const saleList: SaleList_sales_edges_node[] = [
value: 10
}
];
+
+export const voucherList: VoucherList_vouchers_edges_node[] = [
+ {
+ __typename: "Voucher" as "Voucher",
+ discountValue: 100,
+ discountValueType: "PERCENTAGE" as VoucherDiscountValueType,
+ endDate: null,
+ id: "Vm91Y2hlcjox",
+ minAmountSpent: null,
+ name: "Free shipping",
+ startDate: "2019-01-03",
+ usageLimit: null
+ },
+ {
+ __typename: "Voucher" as "Voucher",
+ discountValue: 25,
+ discountValueType: "FIXED" as VoucherDiscountValueType,
+ endDate: null,
+ id: "Vm91Y2hlcjoy",
+ minAmountSpent: {
+ __typename: "Money" as "Money",
+ amount: 200,
+ currency: "USD"
+ },
+ name: "Big order discount",
+ startDate: "2019-01-03",
+ usageLimit: 150
+ }
+];
diff --git a/saleor/static/dashboard-next/discounts/queries.ts b/saleor/static/dashboard-next/discounts/queries.ts
index fdb3ffc5d7..87a891b845 100644
--- a/saleor/static/dashboard-next/discounts/queries.ts
+++ b/saleor/static/dashboard-next/discounts/queries.ts
@@ -2,6 +2,7 @@ import gql from "graphql-tag";
import { TypedQuery } from "../queries";
import { SaleList, SaleListVariables } from "./types/SaleList";
+import { VoucherList, VoucherListVariables } from "./types/VoucherList";
export const saleList = gql`
query SaleList($after: String, $before: String, $first: Int, $last: Int) {
@@ -20,3 +21,28 @@ export const saleList = gql`
}
`;
export const TypedSaleList = TypedQuery<SaleList, SaleListVariables>(saleList);
+
+export const voucherList = gql`
+ query VoucherList($after: String, $before: String, $first: Int, $last: Int) {
+ vouchers(after: $after, before: $before, first: $first, last: $last) {
+ edges {
+ node {
+ id
+ name
+ startDate
+ endDate
+ usageLimit
+ discountValueType
+ discountValue
+ minAmountSpent {
+ currency
+ amount
+ }
+ }
+ }
+ }
+ }
+`;
+export const TypedVoucherList = TypedQuery<VoucherList, VoucherListVariables>(
+ voucherList
+);
diff --git a/saleor/static/dashboard-next/discounts/types/VoucherList.ts b/saleor/static/dashboard-next/discounts/types/VoucherList.ts
new file mode 100644
index 0000000000..d881de4707
--- /dev/null
+++ b/saleor/static/dashboard-next/discounts/types/VoucherList.ts
@@ -0,0 +1,47 @@
+/* tslint:disable */
+// This file was automatically generated and should not be edited.
+
+import { VoucherDiscountValueType } from "./../../types/globalTypes";
+
+// ====================================================
+// GraphQL query operation: VoucherList
+// ====================================================
+
+export interface VoucherList_vouchers_edges_node_minAmountSpent {
+ __typename: "Money";
+ currency: string;
+ amount: number;
+}
+
+export interface VoucherList_vouchers_edges_node {
+ __typename: "Voucher";
+ id: string;
+ name: string | null;
+ startDate: any;
+ endDate: any | null;
+ usageLimit: number | null;
+ discountValueType: VoucherDiscountValueType;
+ discountValue: number;
+ minAmountSpent: VoucherList_vouchers_edges_node_minAmountSpent | null;
+}
+
+export interface VoucherList_vouchers_edges {
+ __typename: "VoucherCountableEdge";
+ node: VoucherList_vouchers_edges_node;
+}
+
+export interface VoucherList_vouchers {
+ __typename: "VoucherCountableConnection";
+ edges: VoucherList_vouchers_edges[];
+}
+
+export interface VoucherList {
+ vouchers: VoucherList_vouchers | null;
+}
+
+export interface VoucherListVariables {
+ after?: string | null;
+ before?: string | null;
+ first?: number | null;
+ last?: number | null;
+}
diff --git a/saleor/static/dashboard-next/storybook/config.js b/saleor/static/dashboard-next/storybook/config.js
index c9586f60f2..82a69563f2 100644
--- a/saleor/static/dashboard-next/storybook/config.js
+++ b/saleor/static/dashboard-next/storybook/config.js
@@ -55,6 +55,7 @@ function loadStories() {
// Discounts
require("./stories/discounts/SaleListPage");
+ require("./stories/discounts/VoucherListPage");
// Homepage
require("./stories/home/HomePage");
diff --git a/saleor/static/dashboard-next/storybook/stories/discounts/VoucherListPage.tsx b/saleor/static/dashboard-next/storybook/stories/discounts/VoucherListPage.tsx
new file mode 100644
index 0000000000..6a073fc430
--- /dev/null
+++ b/saleor/static/dashboard-next/storybook/stories/discounts/VoucherListPage.tsx
@@ -0,0 +1,21 @@
+import { storiesOf } from "@storybook/react";
+import * as React from "react";
+
+import VoucherListPage, {
+ VoucherListPageProps
+} from "../../../discounts/components/VoucherListPage";
+import { voucherList } from "../../../discounts/fixtures";
+import { pageListProps } from "../../../fixtures";
+import Decorator from "../../Decorator";
+
+const props: VoucherListPageProps = {
+ ...pageListProps.default,
+ defaultCurrency: "USD",
+ vouchers: voucherList
+};
+
+storiesOf("Views / Discounts / Voucher List", module)
+ .addDecorator(Decorator)
+ .add("default", () => <VoucherListPage {...props} />)
+ .add("loading", () => <VoucherListPage {...props} vouchers={undefined} />)
+ .add("no data", () => <VoucherListPage {...props} vouchers={[]} />);
diff --git a/saleor/static/dashboard-next/types/globalTypes.ts b/saleor/static/dashboard-next/types/globalTypes.ts
index ff048f51c0..a748014863 100644
--- a/saleor/static/dashboard-next/types/globalTypes.ts
+++ b/saleor/static/dashboard-next/types/globalTypes.ts
@@ -124,6 +124,11 @@ export enum TaxRateType {
WINE = "WINE",
}
+export enum VoucherDiscountValueType {
+ FIXED = "FIXED",
+ PERCENTAGE = "PERCENTAGE",
+}
+
export enum WeightUnitsEnum {
g = "g",
kg = "kg",