Skip to content

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

Add sale create view

dominik-zeglena23ee1091306
diff --git a/saleor/static/dashboard-next/discounts/index.tsx b/saleor/static/dashboard-next/discounts/index.tsx
index 585f4c7212..f2ed4eb1df 100644
--- a/saleor/static/dashboard-next/discounts/index.tsx
+++ b/saleor/static/dashboard-next/discounts/index.tsx
@@ -5,7 +5,8 @@ import { Route, RouteComponentProps, Switch } from "react-router-dom";
import { WindowTitle } from "../components/WindowTitle";
import i18n from "../i18n";
import { saleDetailsPageTab } from "./components/SaleDetailsPage";
-import { saleListPath, salePath, voucherListPath } from "./urls";
+import { saleAddPath, saleListPath, salePath, voucherListPath } from "./urls";
+import SaleCreateView from "./views/SaleCreate";
import SaleDetailsViewComponent, {
SaleDetailsQueryParams
} from "./views/SaleDetails";
@@ -53,6 +54,7 @@ export const DiscountSection: React.StatelessComponent<{}> = () => (
<WindowTitle title={i18n.t("Discounts")} />
<Switch>
<Route exact path={saleListPath} component={SaleListView} />
+ <Route exact path={saleAddPath} component={SaleCreateView} />
<Route path={salePath(":id")} component={SaleDetailsView} />
<Route exact path={voucherListPath} component={VoucherListView} />
</Switch>
diff --git a/saleor/static/dashboard-next/discounts/views/SaleCreate.tsx b/saleor/static/dashboard-next/discounts/views/SaleCreate.tsx
index e69de29bb2..93075be785 100644
--- a/saleor/static/dashboard-next/discounts/views/SaleCreate.tsx
+++ b/saleor/static/dashboard-next/discounts/views/SaleCreate.tsx
@@ -0,0 +1,88 @@
+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, SaleType } from "../../types/globalTypes";
+import SaleCreatePage from "../components/SaleCreatePage";
+import { TypedSaleCreate } from "../mutations";
+import { SaleCreate } from "../types/SaleCreate";
+import { saleListUrl, saleUrl } from "../urls";
+
+function discountValueTypeEnum(type: SaleType): DiscountValueTypeEnum {
+ return type.toString() === DiscountValueTypeEnum.FIXED
+ ? DiscountValueTypeEnum.FIXED
+ : DiscountValueTypeEnum.PERCENTAGE;
+}
+
+export const SaleDetails: React.StatelessComponent = () => (
+ <>
+ <WindowTitle title={i18n.t("Sales")} />
+ <Shop>
+ {shop => (
+ <Messages>
+ {pushMessage => (
+ <Navigator>
+ {navigate => {
+ const handleSaleCreate = (data: SaleCreate) => {
+ if (data.saleCreate.errors.length === 0) {
+ pushMessage({
+ text: i18n.t("Successfully created sale", {
+ context: "notification"
+ })
+ });
+ navigate(saleUrl(data.saleCreate.sale.id), true);
+ }
+ };
+
+ return (
+ <TypedSaleCreate onCompleted={handleSaleCreate}>
+ {(saleCreate, saleCreateOpts) => {
+ const formTransitionState = getMutationState(
+ saleCreateOpts.called,
+ saleCreateOpts.loading,
+ maybe(() => saleCreateOpts.data.saleCreate.errors)
+ );
+
+ return (
+ <SaleCreatePage
+ defaultCurrency={maybe(() => shop.defaultCurrency)}
+ disabled={saleCreateOpts.loading}
+ errors={maybe(
+ () => saleCreateOpts.data.saleCreate.errors
+ )}
+ onBack={() => navigate(saleListUrl)}
+ onSubmit={formData =>
+ saleCreate({
+ variables: {
+ input: {
+ endDate:
+ formData.endDate === ""
+ ? null
+ : formData.endDate,
+ name: formData.name,
+ startDate: formData.startDate,
+ type: discountValueTypeEnum(formData.type),
+ value: formData.value
+ }
+ }
+ })
+ }
+ saveButtonBarState={formTransitionState}
+ />
+ );
+ }}
+ </TypedSaleCreate>
+ );
+ }}
+ </Navigator>
+ )}
+ </Messages>
+ )}
+ </Shop>
+ </>
+);
+export default SaleDetails;