Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add voucher create page component
dominik-zeglend185a2dc3fed
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherCreatePage/VoucherCreatePage.tsx b/saleor/static/dashboard-next/discounts/components/VoucherCreatePage/VoucherCreatePage.tsxnew file mode 100644index 0000000000..a1233a1a3d--- /dev/null+++ b/saleor/static/dashboard-next/discounts/components/VoucherCreatePage/VoucherCreatePage.tsx@@ -0,0 +1,99 @@+import * as React from "react";++import CardSpacer from "../../../components/CardSpacer";+import { ConfirmButtonTransitionState } from "../../../components/ConfirmButton";+import Container from "../../../components/Container";+import Form from "../../../components/Form";+import Grid from "../../../components/Grid";+import PageHeader from "../../../components/PageHeader";+import SaveButtonBar from "../../../components/SaveButtonBar";+import i18n from "../../../i18n";+import { UserError } from "../../../types";+import {+ VoucherDiscountValueType,+ VoucherType+} from "../../../types/globalTypes";+import VoucherInfo from "../VoucherInfo";+import VoucherOptions from "../VoucherOptions";++export interface FormData {+ applyOncePerOrder: boolean;+ code: string;+ discountType: VoucherDiscountValueType;+ endDate: string;+ minAmountSpent: number;+ name: string;+ startDate: string;+ type: VoucherType;+ usageLimit: number;+ value: number;+}++export interface VoucherCreatePageProps {+ defaultCurrency: string;+ disabled: boolean;+ errors: UserError[];+ saveButtonBarState: ConfirmButtonTransitionState;+ onBack: () => void;+ onSubmit: (data: FormData) => void;+}++const VoucherCreatePage: React.StatelessComponent<VoucherCreatePageProps> = ({+ defaultCurrency,+ disabled,+ errors,+ saveButtonBarState,+ onBack,+ onSubmit+}) => {+ const initialForm: FormData = {+ applyOncePerOrder: false,+ code: "",+ discountType: VoucherDiscountValueType.FIXED,+ endDate: "",+ minAmountSpent: 0,+ name: "",+ startDate: "",+ type: VoucherType.VALUE,+ usageLimit: 0,+ value: 0+ };++ return (+ <Form errors={errors} initial={initialForm} onSubmit={onSubmit}>+ {({ change, data, errors: formErrors, hasChanged, submit }) => (+ <Container width="md">+ <PageHeader title={i18n.t("Create Voucher")} onBack={onBack} />+ <Grid>+ <div>+ <VoucherInfo+ data={data}+ errors={formErrors}+ disabled={disabled}+ variant="create"+ onChange={change}+ />+ <CardSpacer />+ <VoucherOptions+ data={data}+ disabled={disabled}+ defaultCurrency={defaultCurrency}+ errors={formErrors}+ onChange={change}+ />+ <CardSpacer />+ </div>+ </Grid>+ <SaveButtonBar+ disabled={disabled || !hasChanged}+ onCancel={onBack}+ onSave={submit}+ state={saveButtonBarState}+ />+ </Container>+ )}+ </Form>+ );+};+VoucherCreatePage.displayName = "VoucherCreatePage";+export default VoucherCreatePage;diff --git a/saleor/static/dashboard-next/discounts/components/VoucherCreatePage/index.ts b/saleor/static/dashboard-next/discounts/components/VoucherCreatePage/index.tsnew file mode 100644index 0000000000..b38cb8083f--- /dev/null+++ b/saleor/static/dashboard-next/discounts/components/VoucherCreatePage/index.ts@@ -0,0 +1,2 @@+export { default } from './VoucherCreatePage';+export * from './VoucherCreatePage';\ No newline at end of filediff --git a/saleor/static/dashboard-next/discounts/components/VoucherDetailsPage/VoucherDetailsPage.tsx b/saleor/static/dashboard-next/discounts/components/VoucherDetailsPage/VoucherDetailsPage.tsxindex be0bc0d2e2..7503fc772a 100644--- a/saleor/static/dashboard-next/discounts/components/VoucherDetailsPage/VoucherDetailsPage.tsx+++ b/saleor/static/dashboard-next/discounts/components/VoucherDetailsPage/VoucherDetailsPage.tsx@@ -128,7 +128,13 @@ const VoucherDetailsPage: React.StatelessComponent<VoucherDetailsPageProps> = ({<PageHeader title={maybe(() => voucher.name)} onBack={onBack} /><Grid><div>- <VoucherInfo data={data} disabled={disabled} onChange={change} />+ <VoucherInfo+ data={data}+ disabled={disabled}+ errors={formErrors}+ variant="update"+ onChange={change}+ /><CardSpacer /><VoucherOptionsdata={data}diff --git a/saleor/static/dashboard-next/discounts/components/VoucherInfo/VoucherInfo.tsx b/saleor/static/dashboard-next/discounts/components/VoucherInfo/VoucherInfo.tsxindex 72aa2b69ca..1694925ac7 100644--- a/saleor/static/dashboard-next/discounts/components/VoucherInfo/VoucherInfo.tsx+++ b/saleor/static/dashboard-next/discounts/components/VoucherInfo/VoucherInfo.tsx@@ -13,13 +13,16 @@ import CardTitle from "../../../components/CardTitle";import FormSpacer from "../../../components/FormSpacer";import SingleSelectField from "../../../components/SingleSelectField";import i18n from "../../../i18n";+import { FormErrors } from "../../../types";import { VoucherType } from "../../../types/globalTypes";import { translateVoucherTypes } from "../../translations";import { FormData } from "../VoucherDetailsPage";interface VoucherInfoProps {data: FormData;+ errors: FormErrors<"name" | "code" | "type">;disabled: boolean;+ variant: "create" | "update";onChange: (event: React.ChangeEvent<any>) => void;}@@ -42,6 +45,8 @@ const VoucherInfo = withStyles(styles, {classes,data,disabled,+ errors,+ variant,onChange}: VoucherInfoProps & WithStyles<typeof styles>) => {const translatedVoucherTypes = translateVoucherTypes();@@ -57,7 +62,9 @@ const VoucherInfo = withStyles(styles, {<TextFieldclassName={classes.nameInput}disabled={disabled}+ error={!!errors.name}fullWidth+ helperText={errors.name}name={"name" as keyof FormData}label={i18n.t("Name")}value={data.name}@@ -67,7 +74,9 @@ const VoucherInfo = withStyles(styles, {<div className={classes.root}><TextFielddisabled={disabled}+ error={!!errors.code}fullWidth+ helperText={errors.code}name={"code" as keyof FormData}label={i18n.t("Discount Code")}value={data.code}@@ -75,7 +84,9 @@ const VoucherInfo = withStyles(styles, {/><SingleSelectFieldchoices={voucherTypeChoices}- disabled={disabled}+ disabled={disabled || variant === "update"}+ error={!!errors.type}+ hint={errors.type}name={"type" as keyof FormData}label={i18n.t("Type of Discount")}value={data.type}diff --git a/saleor/static/dashboard-next/storybook/config.js b/saleor/static/dashboard-next/storybook/config.jsindex 646ccdb2ab..92e0120461 100644--- a/saleor/static/dashboard-next/storybook/config.js+++ b/saleor/static/dashboard-next/storybook/config.js@@ -57,8 +57,9 @@ function loadStories() {require("./stories/discounts/SaleCreatePage");require("./stories/discounts/SaleDetailsPage");require("./stories/discounts/SaleListPage");- require("./stories/discounts/VoucherListPage");+ require("./stories/discounts/VoucherCreatePage");require("./stories/discounts/VoucherDetailsPage");+ require("./stories/discounts/VoucherListPage");// Homepagerequire("./stories/home/HomePage");diff --git a/saleor/static/dashboard-next/storybook/stories/discounts/VoucherCreatePage.tsx b/saleor/static/dashboard-next/storybook/stories/discounts/VoucherCreatePage.tsxnew file mode 100644index 0000000000..624f311e35--- /dev/null+++ b/saleor/static/dashboard-next/storybook/stories/discounts/VoucherCreatePage.tsx@@ -0,0 +1,39 @@+import { storiesOf } from "@storybook/react";+import * as React from "react";++import VoucherCreatePage, {+ FormData,+ VoucherCreatePageProps+} from "../../../discounts/components/VoucherCreatePage";+import Decorator from "../../Decorator";+import { formError } from "../../misc";++const props: VoucherCreatePageProps = {+ defaultCurrency: "USD",+ disabled: false,+ errors: [],+ onBack: () => undefined,+ onSubmit: () => undefined,+ saveButtonBarState: "default"+};++storiesOf("Views / Discounts / Voucher create", module)+ .addDecorator(Decorator)+ .add("default", () => <VoucherCreatePage {...props} />)+ .add("form errors", () => (+ <VoucherCreatePage+ {...props}+ errors={([+ "applyOncePerOrder",+ "code",+ "discountType",+ "endDate",+ "minAmountSpent",+ "name",+ "startDate",+ "type",+ "usageLimit",+ "value"+ ] as Array<keyof FormData>).map(formError)}+ />+ ));diff --git a/saleor/static/dashboard-next/storybook/stories/discounts/VoucherDetailsPage.tsx b/saleor/static/dashboard-next/storybook/stories/discounts/VoucherDetailsPage.tsxindex bdd86ac5f7..7e483218e1 100644--- a/saleor/static/dashboard-next/storybook/stories/discounts/VoucherDetailsPage.tsx+++ b/saleor/static/dashboard-next/storybook/stories/discounts/VoucherDetailsPage.tsx@@ -2,26 +2,35 @@ import { storiesOf } from "@storybook/react";import * as React from "react";import VoucherDetailsPage, {- VoucherDetailsPageProps+ FormData,+ VoucherDetailsPageProps,+ VoucherDetailsPageTab} from "../../../discounts/components/VoucherDetailsPage";import { voucherDetails } from "../../../discounts/fixtures";import { pageListProps } from "../../../fixtures";import Decorator from "../../Decorator";+import { formError } from "../../misc";const props: VoucherDetailsPageProps = {...pageListProps.default,+ activeTab: VoucherDetailsPageTab.products,defaultCurrency: "USD",+ errors: [],onBack: () => undefined,onCategoryAssign: () => undefined,onCategoryClick: () => undefined,+ onCategoryUnassign: () => undefined,onCollectionAssign: () => undefined,onCollectionClick: () => undefined,+ onCollectionUnassign: () => undefined,onCountryAssign: () => undefined,onCountryUnassign: () => undefined,onProductAssign: () => undefined,onProductClick: () => undefined,+ onProductUnassign: () => undefined,onRemove: () => undefined,onSubmit: () => undefined,+ onTabClick: () => undefined,saveButtonBarState: "default",voucher: voucherDetails};@@ -31,4 +40,21 @@ storiesOf("Views / Discounts / Voucher details", module).add("default", () => <VoucherDetailsPage {...props} />).add("loading", () => (<VoucherDetailsPage {...props} disabled={true} voucher={undefined} />+ ))+ .add("form errors", () => (+ <VoucherDetailsPage+ {...props}+ errors={([+ "applyOncePerOrder",+ "code",+ "discountType",+ "endDate",+ "minAmountSpent",+ "name",+ "startDate",+ "type",+ "usageLimit",+ "value"+ ] as Array<keyof FormData>).map(formError)}+ />));diff --git a/saleor/static/dashboard-next/types.ts b/saleor/static/dashboard-next/types.tsindex 05cc20dae4..fd5f5446d5 100644--- a/saleor/static/dashboard-next/types.ts+++ b/saleor/static/dashboard-next/types.ts@@ -26,3 +26,5 @@ export interface PartialMutationProviderOutput<opts: MutationResult<TData>;mutate: (variables: TVariables) => void;}++export type FormErrors<TKeys extends string> = Partial<Record<TKeys, string>>;