Skip to content

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

Add checkbox, fix type issues

Krzysztof Bialoglowiczb3bd2c24ccaf
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherDates/VoucherDates.tsx b/saleor/static/dashboard-next/discounts/components/VoucherDates/VoucherDates.tsx
index 852b4fcc45..91a234ec81 100644
--- a/saleor/static/dashboard-next/discounts/components/VoucherDates/VoucherDates.tsx
+++ b/saleor/static/dashboard-next/discounts/components/VoucherDates/VoucherDates.tsx
@@ -42,9 +42,6 @@ const VoucherDates = withStyles(styles, {
errors,
onChange
}: VoucherDatesProps & WithStyles<typeof styles>) => {
- const [showEndDate, setshowEndDate] = React.useState(false);
- const handleOnChange = () => setshowEndDate(!showEndDate);
-
return (
<Card>
<CardTitle title={i18n.t("Active Dates")} />
@@ -74,12 +71,12 @@ const VoucherDates = withStyles(styles, {
/>
</div>
<ControlledCheckbox
- checked={showEndDate}
+ checked={data.hasEndDate}
label={i18n.t("Set end date")}
- name="setEndDate"
- onChange={handleOnChange}
+ name={"hasEndDate" as keyof FormData}
+ onChange={onChange}
/>
- {showEndDate ? (
+ {data.hasEndDate ? (
<div className={classes.root}>
<TextField
disabled={disabled}
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherDetailsPage/VoucherDetailsPage.tsx b/saleor/static/dashboard-next/discounts/components/VoucherDetailsPage/VoucherDetailsPage.tsx
index f5ea3d7e6e..039e341036 100644
--- a/saleor/static/dashboard-next/discounts/components/VoucherDetailsPage/VoucherDetailsPage.tsx
+++ b/saleor/static/dashboard-next/discounts/components/VoucherDetailsPage/VoucherDetailsPage.tsx
@@ -15,8 +15,8 @@ import i18n from "../../../i18n";
import { maybe, splitDateTime } from "../../../misc";
import { ListProps, TabListActions, UserError } from "../../../types";
import {
- VoucherDiscountValueType,
- VoucherType
+ DiscountValueTypeEnum,
+ VoucherTypeEnum
} from "../../../types/globalTypes";
import { VoucherDetails_voucher } from "../../types/VoucherDetails";
import DiscountCategories from "../DiscountCategories";
@@ -46,13 +46,15 @@ export function voucherDetailsPageTab(tab: string): VoucherDetailsPageTab {
export interface FormData {
applyOncePerOrder: boolean;
code: string;
- discountType: VoucherDiscountValueType;
+ discountType: DiscountValueTypeEnum;
endDate: string;
endTime: string;
+ hasEndDate: boolean;
+ hasUsageLimit: boolean;
minAmountSpent: number;
startDate: string;
startTime: string;
- type: VoucherType;
+ type: VoucherTypeEnum;
usageLimit: number;
value: number;
}
@@ -126,14 +128,16 @@ const VoucherDetailsPage: React.StatelessComponent<VoucherDetailsPageProps> = ({
code: maybe(() => voucher.code, ""),
discountType: maybe(
() => voucher.discountValueType,
- VoucherDiscountValueType.FIXED
+ DiscountValueTypeEnum.FIXED
),
endDate: splitDateTime(maybe(() => voucher.endDate, "")).date,
endTime: splitDateTime(maybe(() => voucher.endDate, "")).time,
+ hasEndDate: maybe(() => !!voucher.endDate),
+ hasUsageLimit: maybe(() => !!voucher.usageLimit),
minAmountSpent: maybe(() => voucher.minAmountSpent.amount, 0),
startDate: splitDateTime(maybe(() => voucher.startDate, "")).date,
startTime: splitDateTime(maybe(() => voucher.startDate, "")).time,
- type: maybe(() => voucher.type, VoucherType.ENTIRE_ORDER),
+ type: maybe(() => voucher.type, VoucherTypeEnum.ENTIRE_ORDER),
usageLimit: maybe(() => voucher.usageLimit || 0, 0),
value: maybe(() => voucher.discountValue, 0)
};
@@ -151,6 +155,7 @@ const VoucherDetailsPage: React.StatelessComponent<VoucherDetailsPageProps> = ({
disabled={disabled}
errors={formErrors}
onChange={change}
+ variant="update"
/>
<CardSpacer />
<VoucherTypes
@@ -160,17 +165,19 @@ const VoucherDetailsPage: React.StatelessComponent<VoucherDetailsPageProps> = ({
onChange={change}
/>
<CardSpacer />
- {data.discountType !== VoucherDiscountValueType.SHIPPING ? (
+ {data.discountType.toString() !== "SHIPPING" ? (
<VoucherValue
data={data}
disabled={disabled}
defaultCurrency={defaultCurrency}
errors={formErrors}
onChange={change}
+ variant="update"
/>
) : null}
<CardSpacer />
- {data.type === VoucherType.SPECIFIC_PRODUCT ? (
+ {data.type === VoucherTypeEnum.SPECIFIC_PRODUCT &&
+ data.discountType.toString() !== "SHIPPING" ? (
<>
<TabContainer>
<CategoriesTab
@@ -258,7 +265,9 @@ const VoucherDetailsPage: React.StatelessComponent<VoucherDetailsPageProps> = ({
/>
)}
</>
- ) : data.discountType === VoucherDiscountValueType.SHIPPING ? (
+ ) : null}
+ <CardSpacer />
+ {data.discountType.toString() === "SHIPPING" ? (
<CountryList
countries={maybe(() => voucher.countries)}
disabled={disabled}
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherInfo/VoucherInfo.tsx b/saleor/static/dashboard-next/discounts/components/VoucherInfo/VoucherInfo.tsx
index 05dc24ec0d..fd2addf44d 100644
--- a/saleor/static/dashboard-next/discounts/components/VoucherInfo/VoucherInfo.tsx
+++ b/saleor/static/dashboard-next/discounts/components/VoucherInfo/VoucherInfo.tsx
@@ -17,8 +17,9 @@ import { FormData } from "../VoucherDetailsPage";
interface VoucherInfoProps {
data: FormData;
- errors: FormErrors<"name" | "code" | "type">;
+ errors: FormErrors<"code">;
disabled: boolean;
+ variant: string;
onChange: (event: React.ChangeEvent<any>) => void;
}
@@ -42,6 +43,7 @@ const VoucherInfo = withStyles(styles, {
data,
disabled,
errors,
+ variant,
onChange
}: VoucherInfoProps & WithStyles<typeof styles>) => {
const [generateCode, setGenerateCode] = React.useState();
@@ -65,15 +67,17 @@ const VoucherInfo = withStyles(styles, {
<CardTitle
title={i18n.t("General Information")}
toolbar={
- <Button color="primary" onClick={onGenerateCode}>
- {i18n.t("Generate Code")}
- </Button>
+ variant === "create" ? (
+ <Button color="primary" onClick={onGenerateCode}>
+ {i18n.t("Generate Code")}
+ </Button>
+ ) : null
}
/>
<CardContent>
<div className={classes.root}>
<TextField
- disabled={disabled}
+ disabled={variant === "update" || disabled}
error={!!errors.code}
fullWidth
helperText={errors.code}
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherLimits/VoucherLimits.tsx b/saleor/static/dashboard-next/discounts/components/VoucherLimits/VoucherLimits.tsx
index 62fb1439b3..ba3f375d6c 100644
--- a/saleor/static/dashboard-next/discounts/components/VoucherLimits/VoucherLimits.tsx
+++ b/saleor/static/dashboard-next/discounts/components/VoucherLimits/VoucherLimits.tsx
@@ -19,14 +19,7 @@ interface VoucherLimitsProps {
data: FormData;
defaultCurrency: string;
disabled: boolean;
- errors: FormErrors<
- | "discountType"
- | "discountValue"
- | "endDate"
- | "minAmountSpent"
- | "startDate"
- | "usageLimit"
- >;
+ errors: FormErrors<"usageLimit">;
onChange: (event: React.ChangeEvent<any>) => void;
}
@@ -49,21 +42,19 @@ const VoucherLimits = withStyles(styles, {
errors,
onChange
}: VoucherLimitsProps & WithStyles<typeof styles>) => {
- const [showInput, setshowInput] = React.useState(false);
- const handleOnChange = () => setshowInput(!showInput);
return (
<Card>
<CardTitle title={i18n.t("Usage Limit ")} />
<CardContent className={classes.root}>
<ControlledCheckbox
- checked={showInput}
+ checked={data.hasUsageLimit}
label={i18n.t(
"Limit number of times this discount can be used in total"
)}
- name="usageLimitCheck"
- onChange={handleOnChange}
+ name={"hasUsageLimit" as keyof FormData}
+ onChange={onChange}
/>
- {showInput ? (
+ {data.hasUsageLimit ? (
<TextField
disabled={disabled}
error={!!errors.usageLimit}
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherList/VoucherList.tsx b/saleor/static/dashboard-next/discounts/components/VoucherList/VoucherList.tsx
index 31fd16af7e..d1c4633f62 100644
--- a/saleor/static/dashboard-next/discounts/components/VoucherList/VoucherList.tsx
+++ b/saleor/static/dashboard-next/discounts/components/VoucherList/VoucherList.tsx
@@ -22,7 +22,7 @@ import TablePagination from "@saleor/components/TablePagination";
import i18n from "../../../i18n";
import { maybe, renderCollection } from "../../../misc";
import { ListActions, ListProps } from "../../../types";
-import { VoucherDiscountValueType } from "../../../types/globalTypes";
+import { DiscountValueTypeEnum } from "../../../types/globalTypes";
import { VoucherList_vouchers_edges_node } from "../../types/VoucherList";
export interface VoucherListProps extends ListProps, ListActions {
@@ -202,7 +202,7 @@ const VoucherList = withStyles(styles, {
voucher.discountValueType &&
voucher.discountValue ? (
voucher.discountValueType ===
- VoucherDiscountValueType.FIXED ? (
+ DiscountValueTypeEnum.FIXED ? (
<Money
money={{
amount: voucher.discountValue,
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherRequirements/VoucherRequirements.tsx b/saleor/static/dashboard-next/discounts/components/VoucherRequirements/VoucherRequirements.tsx
index 9a078f3448..43f265c137 100644
--- a/saleor/static/dashboard-next/discounts/components/VoucherRequirements/VoucherRequirements.tsx
+++ b/saleor/static/dashboard-next/discounts/components/VoucherRequirements/VoucherRequirements.tsx
@@ -19,12 +19,7 @@ interface VoucherRequirementsProps {
defaultCurrency: string;
disabled: boolean;
errors: FormErrors<
- | "discountType"
- | "discountValue"
- | "endDate"
| "minAmountSpent"
- | "startDate"
- | "usageLimit"
>;
onChange: (event: React.ChangeEvent<any>) => void;
}
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherSummary/VoucherSummary.tsx b/saleor/static/dashboard-next/discounts/components/VoucherSummary/VoucherSummary.tsx
index 4ff5d0a3bf..fca29dbf1f 100644
--- a/saleor/static/dashboard-next/discounts/components/VoucherSummary/VoucherSummary.tsx
+++ b/saleor/static/dashboard-next/discounts/components/VoucherSummary/VoucherSummary.tsx
@@ -13,7 +13,7 @@ import Percent from "@saleor/components/Percent";
import Skeleton from "@saleor/components/Skeleton";
import i18n from "../../../i18n";
import { maybe } from "../../../misc";
-import { VoucherDiscountValueType } from "../../../types/globalTypes";
+import { DiscountValueTypeEnum } from "../../../types/globalTypes";
import { translateVoucherTypes } from "../../translations";
import { VoucherDetails_voucher } from "../../types/VoucherDetails";
@@ -51,7 +51,7 @@ const VoucherSummary: React.StatelessComponent<VoucherSummaryProps> = ({
<Typography>
{maybe<React.ReactNode>(
() =>
- voucher.discountValueType === VoucherDiscountValueType.FIXED ? (
+ voucher.discountValueType === DiscountValueTypeEnum.FIXED ? (
<Money
money={{
amount: voucher.discountValue,
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherTypes/VoucherTypes.tsx b/saleor/static/dashboard-next/discounts/components/VoucherTypes/VoucherTypes.tsx
index 047e896d17..556f72e9b8 100644
--- a/saleor/static/dashboard-next/discounts/components/VoucherTypes/VoucherTypes.tsx
+++ b/saleor/static/dashboard-next/discounts/components/VoucherTypes/VoucherTypes.tsx
@@ -12,15 +12,12 @@ import CardTitle from "@saleor/components/CardTitle";
import RadioGroupField from "@saleor/components/RadioGroupField";
import i18n from "../../../i18n";
import { FormErrors } from "../../../types";
-import {
- DiscountValueTypeEnum,
- VoucherDiscountValueType
-} from "../../../types/globalTypes";
+import { DiscountValueTypeEnum } from "../../../types/globalTypes";
import { FormData } from "../VoucherDetailsPage";
interface VoucherTypesProps {
data: FormData;
- errors: FormErrors<"name" | "code" | "type">;
+ errors: FormErrors<"discountType">;
disabled: boolean;
onChange: (event: React.ChangeEvent<any>) => void;
}
@@ -44,7 +41,7 @@ const VoucherTypes = withStyles(styles, {
errors,
onChange
}: VoucherTypesProps & WithStyles<typeof styles>) => {
- const voucherTypeChoices = Object.values(VoucherDiscountValueType).map(
+ const voucherTypeChoices = Object.values(DiscountValueTypeEnum).map(
type => {
switch (type.toString()) {
case DiscountValueTypeEnum.FIXED:
@@ -59,16 +56,16 @@ const VoucherTypes = withStyles(styles, {
label: i18n.t("Percentage"),
value: type
};
- case DiscountValueTypeEnum.SHIPPING:
- return {
- hidden: false,
- label: i18n.t("Free Shipping"),
- value: type
- };
}
}
);
+ voucherTypeChoices.push({
+ hidden: false,
+ label: i18n.t("Free Shipping"),
+ value: "SHIPPING"
+ });
+
return (
<Card>
<CardTitle title={i18n.t("Discount Type")} />
@@ -77,8 +74,8 @@ const VoucherTypes = withStyles(styles, {
<RadioGroupField
choices={voucherTypeChoices}
disabled={disabled}
- error={!!errors.type}
- hint={errors.type}
+ error={!!errors.discountType}
+ hint={errors.discountType}
name={"discountType" as keyof FormData}
value={data.discountType}
onChange={onChange}
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherValue/VoucherValue.tsx b/saleor/static/dashboard-next/discounts/components/VoucherValue/VoucherValue.tsx
index c1e3e222b9..888175e3ce 100644
--- a/saleor/static/dashboard-next/discounts/components/VoucherValue/VoucherValue.tsx
+++ b/saleor/static/dashboard-next/discounts/components/VoucherValue/VoucherValue.tsx
@@ -17,7 +17,7 @@ import RadioGroupField from "@saleor/components/RadioGroupField";
import TextFieldWithChoice from "@saleor/components/TextFieldWithChoice";
import i18n from "../../../i18n";
import { FormErrors } from "../../../types";
-import { VoucherDiscountValueType } from "../../../types/globalTypes";
+import { DiscountValueTypeEnum } from "../../../types/globalTypes";
import { translateVoucherTypes } from "../../translations";
import { FormData } from "../VoucherDetailsPage";
@@ -26,10 +26,11 @@ interface VoucherValueProps {
defaultCurrency: string;
errors: FormErrors<"discountValue" | "type">;
disabled: boolean;
+ variant: string;
onChange: (event: React.ChangeEvent<any>) => void;
}
-enum VoucherTypeEnum {
+export enum VoucherTypeEnum {
ENTIRE_ORDER = "ENTIRE_ORDER",
SHIPPING = "SHIPPING",
SPECIFIC_PRODUCT = "SPECIFIC_PRODUCT"
@@ -53,6 +54,7 @@ const VoucherValue = withStyles(styles, {
defaultCurrency,
disabled,
errors,
+ variant,
onChange
}: VoucherValueProps & WithStyles<typeof styles>) => {
const translatedVoucherTypes = translateVoucherTypes();
@@ -72,10 +74,11 @@ const VoucherValue = withStyles(styles, {
error={!!errors.discountValue}
ChoiceProps={{
label:
- data.discountType === VoucherDiscountValueType.FIXED
+ data.discountType === DiscountValueTypeEnum.FIXED
? defaultCurrency
: "%",
- name: "discountType" as keyof FormData
+ name: "discountType" as keyof FormData,
+ values: null
}}
helperText={errors.discountValue}
name={"value" as keyof FormData}
@@ -89,17 +92,21 @@ const VoucherValue = withStyles(styles, {
}}
/>
<FormSpacer />
- <RadioGroupField
- choices={voucherTypeChoices}
- disabled={disabled}
- error={!!errors.type}
- hint={errors.type}
- label="Discount Specific Information"
- name={"type" as keyof FormData}
- value={data.type}
- onChange={onChange}
- />
- <FormSpacer />
+ {variant === "update" ? (
+ <>
+ <RadioGroupField
+ choices={voucherTypeChoices}
+ disabled={disabled}
+ error={!!errors.type}
+ hint={errors.type}
+ label="Discount Specific Information"
+ name={"type" as keyof FormData}
+ value={data.type}
+ onChange={onChange}
+ />
+ <FormSpacer />
+ </>
+ ) : null}
<Hr />
<FormSpacer />
<ControlledSwitch
diff --git a/saleor/static/dashboard-next/discounts/fixtures.ts b/saleor/static/dashboard-next/discounts/fixtures.ts
index 43df70b5cc..bc75b0ab6a 100644
--- a/saleor/static/dashboard-next/discounts/fixtures.ts
+++ b/saleor/static/dashboard-next/discounts/fixtures.ts
@@ -1,8 +1,8 @@
import placeholderImage from "../../images/placeholder60x60.png";
import {
+ DiscountValueTypeEnum,
SaleType,
- VoucherDiscountValueType,
- VoucherType
+ VoucherTypeEnum
} from "../types/globalTypes";
import { SaleDetails_sale } from "./types/SaleDetails";
import { SaleList_sales_edges_node } from "./types/SaleList";
@@ -69,7 +69,7 @@ export const voucherList: VoucherList_vouchers_edges_node[] = [
}
],
discountValue: 100,
- discountValueType: "PERCENTAGE" as VoucherDiscountValueType,
+ discountValueType: "PERCENTAGE" as DiscountValueTypeEnum,
endDate: null,
id: "Vm91Y2hlcjox",
minAmountSpent: null,
@@ -81,7 +81,7 @@ export const voucherList: VoucherList_vouchers_edges_node[] = [
code: "FREE2020",
countries: [],
discountValue: 25,
- discountValueType: "FIXED" as VoucherDiscountValueType,
+ discountValueType: "FIXED" as DiscountValueTypeEnum,
endDate: null,
id: "Vm91Y2hlcjoy",
minAmountSpent: {
@@ -275,7 +275,7 @@ export const voucherDetails: VoucherDetails_voucher = {
}
],
discountValue: 25,
- discountValueType: VoucherDiscountValueType.FIXED,
+ discountValueType: DiscountValueTypeEnum.FIXED,
endDate: null,
id: "Vm91Y2hlcjoy",
minAmountSpent: {
@@ -296,7 +296,7 @@ export const voucherDetails: VoucherDetails_voucher = {
totalCount: 0
},
startDate: "2018-11-27",
- type: VoucherType.ENTIRE_ORDER,
+ type: VoucherTypeEnum.ENTIRE_ORDER,
usageLimit: null,
used: 0
};