Skip to content

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

Add voucher create view

dominik-zeglenfa0d8782333d
diff --git a/saleor/static/dashboard-next/discounts/index.tsx b/saleor/static/dashboard-next/discounts/index.tsx
index 7aa977da7d..0614949611 100644
--- a/saleor/static/dashboard-next/discounts/index.tsx
+++ b/saleor/static/dashboard-next/discounts/index.tsx
@@ -10,6 +10,7 @@ import {
saleAddPath,
saleListPath,
salePath,
+ voucherAddPath,
voucherListPath,
voucherPath
} from "./urls";
@@ -18,6 +19,7 @@ import SaleDetailsViewComponent, {
SaleDetailsQueryParams
} from "./views/SaleDetails";
import SaleListViewComponent, { SaleListQueryParams } from "./views/SaleList";
+import VoucherCreateView from "./views/VoucherCreate";
import VoucherDetailsViewComponent, {
VoucherDetailsQueryParams
} from "./views/VoucherDetails";
@@ -77,6 +79,7 @@ export const DiscountSection: React.StatelessComponent<{}> = () => (
<Switch>
<Route exact path={saleListPath} component={SaleListView} />
<Route exact path={saleAddPath} component={SaleCreateView} />
+ <Route exact path={voucherAddPath} component={VoucherCreateView} />
<Route path={salePath(":id")} component={SaleDetailsView} />
<Route exact path={voucherListPath} component={VoucherListView} />
<Route path={voucherPath(":id")} component={VoucherDetailsView} />
diff --git a/saleor/static/dashboard-next/discounts/views/VoucherCreate.tsx b/saleor/static/dashboard-next/discounts/views/VoucherCreate.tsx
new file mode 100644
index 0000000000..66d5a629e9
--- /dev/null
+++ b/saleor/static/dashboard-next/discounts/views/VoucherCreate.tsx
@@ -0,0 +1,100 @@
+import * as React from "react";
+
+import Messages from "../../components/messages";
+import Navigator from "../../components/Navigator";
+import Shop from "../../components/Shop";
+import { WindowTitle } from "../../components/WindowTitle";
+import i18n from "../../i18n";
+import { getMutationState, maybe } from "../../misc";
+import {
+ DiscountValueTypeEnum,
+ VoucherDiscountValueType,
+ VoucherType,
+ VoucherTypeEnum
+} from "../../types/globalTypes";
+import VoucherCreatePage from "../components/VoucherCreatePage";
+import { TypedVoucherCreate } from "../mutations";
+import { VoucherCreate } from "../types/VoucherCreate";
+import { voucherListUrl, voucherUrl } from "../urls";
+
+function discountValueTypeEnum(
+ type: VoucherDiscountValueType
+): DiscountValueTypeEnum {
+ return type.toString() === DiscountValueTypeEnum.FIXED
+ ? DiscountValueTypeEnum.FIXED
+ : DiscountValueTypeEnum.PERCENTAGE;
+}
+
+export const VoucherDetails: React.StatelessComponent = () => (
+ <>
+ <WindowTitle title={i18n.t("Vouchers")} />
+ <Shop>
+ {shop => (
+ <Messages>
+ {pushMessage => (
+ <Navigator>
+ {navigate => {
+ const handleVoucherCreate = (data: VoucherCreate) => {
+ if (data.voucherCreate.errors.length === 0) {
+ pushMessage({
+ text: i18n.t("Successfully created voucher", {
+ context: "notification"
+ })
+ });
+ navigate(voucherUrl(data.voucherCreate.voucher.id), true);
+ }
+ };
+
+ return (
+ <TypedVoucherCreate onCompleted={handleVoucherCreate}>
+ {(voucherCreate, voucherCreateOpts) => {
+ const formTransitionState = getMutationState(
+ voucherCreateOpts.called,
+ voucherCreateOpts.loading,
+ maybe(() => voucherCreateOpts.data.voucherCreate.errors)
+ );
+
+ return (
+ <VoucherCreatePage
+ defaultCurrency={maybe(() => shop.defaultCurrency)}
+ disabled={voucherCreateOpts.loading}
+ errors={maybe(
+ () => voucherCreateOpts.data.voucherCreate.errors
+ )}
+ onBack={() => navigate(voucherListUrl)}
+ onSubmit={formData =>
+ voucherCreate({
+ variables: {
+ input: {
+ code: formData.code,
+ discountValue: formData.value,
+ discountValueType: discountValueTypeEnum(
+ formData.discountType
+ ),
+ endDate:
+ formData.endDate === ""
+ ? null
+ : formData.endDate,
+ minAmountSpent: formData.minAmountSpent,
+ name: formData.name,
+ startDate: formData.startDate,
+ type: VoucherTypeEnum[formData.type]
+ }
+ }
+ })
+ }
+ saveButtonBarState={formTransitionState}
+ />
+ );
+ }}
+ </TypedVoucherCreate>
+ );
+ }}
+ </Navigator>
+ )}
+ </Messages>
+ )}
+ </Shop>
+ </>
+);
+export default VoucherDetails;