diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ae3b6e467c..c03e5b6040f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ All notable, unreleased changes to this project will be documented in this file. - Update CollectionBulkDelete error type - #1030 by @d-wysocki - Remove mailing settings - #1027 by @dominik-zeglen - Update schema to contain email plugin changes - #1029 by @dominik-zeglen +- Fix creating shipping voucher - #1031 by @orzechdev - Unconfirmed order manipulation - #967 by @tomaszszymanski129 - Add multiline field plugins - #974 by @dominik-zeglen - Handle limit reached error - #990 by @dominik-zeglen diff --git a/cypress/elements/discounts/vouchers.js b/cypress/elements/discounts/vouchers.js index c14dff4eae5..78ed0108442 100644 --- a/cypress/elements/discounts/vouchers.js +++ b/cypress/elements/discounts/vouchers.js @@ -2,8 +2,9 @@ export const VOUCHERS_SELECTORS = { createVoucherButton: "[data-test-id='create-voucher']", voucherCodeInput: "[name='code']", discountRadioButtons: "[name='discountType']", - percentageDiscountRadioButton: "[name='discountType'][value='PERCENTAGE']", - fixedDiscountRadioButton: "[name='discountType'][value='FIXED']", + percentageDiscountRadioButton: + "[name='discountType'][value='VALUE_PERCENTAGE']", + fixedDiscountRadioButton: "[name='discountType'][value='VALUE_FIXED']", shippingDiscountRadioButton: "[name='discountType'][value='SHIPPING']", discountValueInputs: "[name='value']" }; diff --git a/src/discounts/components/VoucherCreatePage/VoucherCreatePage.tsx b/src/discounts/components/VoucherCreatePage/VoucherCreatePage.tsx index 119267e21d1..6efceeb23ee 100644 --- a/src/discounts/components/VoucherCreatePage/VoucherCreatePage.tsx +++ b/src/discounts/components/VoucherCreatePage/VoucherCreatePage.tsx @@ -8,18 +8,18 @@ import Form from "@saleor/components/Form"; import Grid from "@saleor/components/Grid"; import PageHeader from "@saleor/components/PageHeader"; import SaveButtonBar from "@saleor/components/SaveButtonBar"; -import { createChannelsChangeHandler } from "@saleor/discounts/handlers"; +import { + createChannelsChangeHandler, + createDiscountTypeChangeHandler +} from "@saleor/discounts/handlers"; import { DiscountErrorFragment } from "@saleor/fragments/types/DiscountErrorFragment"; import { sectionNames } from "@saleor/intl"; import { validatePrice } from "@saleor/products/utils/validation"; import React from "react"; import { useIntl } from "react-intl"; -import { - DiscountValueTypeEnum, - VoucherTypeEnum -} from "../../../types/globalTypes"; -import { RequirementsPicker } from "../../types"; +import { VoucherTypeEnum } from "../../../types/globalTypes"; +import { DiscountTypeEnum, RequirementsPicker } from "../../types"; import VoucherDates from "../VoucherDates"; import VoucherInfo from "../VoucherInfo"; import VoucherLimits from "../VoucherLimits"; @@ -32,7 +32,7 @@ export interface FormData { applyOncePerOrder: boolean; channelListings: ChannelVoucherData[]; code: string; - discountType: DiscountValueTypeEnum; + discountType: DiscountTypeEnum; endDate: string; endTime: string; hasEndDate: boolean; @@ -78,7 +78,7 @@ const VoucherCreatePage: React.FC = ({ applyOncePerOrder: false, channelListings, code: "", - discountType: DiscountValueTypeEnum.FIXED, + discountType: DiscountTypeEnum.VALUE_FIXED, endDate: "", endTime: "", hasEndDate: false, @@ -95,17 +95,22 @@ const VoucherCreatePage: React.FC = ({ return (
{({ change, data, hasChanged, submit, triggerChange }) => { + const handleDiscountTypeChange = createDiscountTypeChangeHandler( + change + ); const handleChannelChange = createChannelsChangeHandler( data.channelListings, onChannelsChange, triggerChange ); - const formDisabled = data.channelListings?.some( - channel => - validatePrice(channel.discountValue) || - (data.requirementsPicker === RequirementsPicker.ORDER && - validatePrice(channel.minSpent)) - ); + const formDisabled = + data.discountType.toString() !== "SHIPPING" && + data.channelListings?.some( + channel => + validatePrice(channel.discountValue) || + (data.requirementsPicker === RequirementsPicker.ORDER && + validatePrice(channel.minSpent)) + ); return ( @@ -123,7 +128,7 @@ const VoucherCreatePage: React.FC = ({ data={data} errors={errors} disabled={disabled} - onChange={change} + onChange={event => handleDiscountTypeChange(data, event)} variant="create" /> diff --git a/src/discounts/components/VoucherDetailsPage/VoucherDetailsPage.tsx b/src/discounts/components/VoucherDetailsPage/VoucherDetailsPage.tsx index e0ea0ee3f87..7744973d825 100644 --- a/src/discounts/components/VoucherDetailsPage/VoucherDetailsPage.tsx +++ b/src/discounts/components/VoucherDetailsPage/VoucherDetailsPage.tsx @@ -11,8 +11,11 @@ import Grid from "@saleor/components/Grid"; import PageHeader from "@saleor/components/PageHeader"; import SaveButtonBar from "@saleor/components/SaveButtonBar"; import { Tab, TabContainer } from "@saleor/components/Tab"; -import { createChannelsChangeHandler } from "@saleor/discounts/handlers"; -import { RequirementsPicker } from "@saleor/discounts/types"; +import { + createChannelsChangeHandler, + createDiscountTypeChangeHandler +} from "@saleor/discounts/handlers"; +import { DiscountTypeEnum, RequirementsPicker } from "@saleor/discounts/types"; import { DiscountErrorFragment } from "@saleor/fragments/types/DiscountErrorFragment"; import { sectionNames } from "@saleor/intl"; import { validatePrice } from "@saleor/products/utils/validation"; @@ -56,7 +59,7 @@ export interface VoucherDetailsPageFormData { applyOncePerOrder: boolean; channelListings: ChannelVoucherData[]; code: string; - discountType: DiscountValueTypeEnum; + discountType: DiscountTypeEnum; endDate: string; endTime: string; hasEndDate: boolean; @@ -156,15 +159,19 @@ const VoucherDetailsPage: React.FC = ({ requirementsPickerInitValue = RequirementsPicker.NONE; } + const discountType = + voucher?.type === VoucherTypeEnum.SHIPPING + ? DiscountTypeEnum.SHIPPING + : voucher?.discountValueType === DiscountValueTypeEnum.PERCENTAGE + ? DiscountTypeEnum.VALUE_PERCENTAGE + : DiscountTypeEnum.VALUE_FIXED; + const initialForm: VoucherDetailsPageFormData = { applyOncePerCustomer: voucher?.applyOncePerCustomer || false, applyOncePerOrder: voucher?.applyOncePerOrder || false, channelListings, code: voucher?.code || "", - discountType: maybe( - () => voucher.discountValueType, - DiscountValueTypeEnum.FIXED - ), + discountType, endDate: splitDateTime(maybe(() => voucher.endDate, "")).date, endTime: splitDateTime(maybe(() => voucher.endDate, "")).time, hasEndDate: maybe(() => !!voucher.endDate), @@ -183,17 +190,22 @@ const VoucherDetailsPage: React.FC = ({ return ( {({ change, data, hasChanged, submit, triggerChange }) => { + const handleDiscountTypeChange = createDiscountTypeChangeHandler( + change + ); const handleChannelChange = createChannelsChangeHandler( data.channelListings, onChannelsChange, triggerChange ); - const formDisabled = data.channelListings?.some( - channel => - validatePrice(channel.discountValue) || - (data.requirementsPicker === RequirementsPicker.ORDER && - validatePrice(channel.minSpent)) - ); + const formDisabled = + data.discountType.toString() !== "SHIPPING" && + data.channelListings?.some( + channel => + validatePrice(channel.discountValue) || + (data.requirementsPicker === RequirementsPicker.ORDER && + validatePrice(channel.minSpent)) + ); return ( @@ -214,7 +226,7 @@ const VoucherDetailsPage: React.FC = ({ data={data} disabled={disabled} errors={errors} - onChange={change} + onChange={event => handleDiscountTypeChange(data, event)} /> {data.discountType.toString() !== "SHIPPING" ? ( diff --git a/src/discounts/components/VoucherTypes/VoucherTypes.tsx b/src/discounts/components/VoucherTypes/VoucherTypes.tsx index f2e5d75913d..35d5d1e5b3d 100644 --- a/src/discounts/components/VoucherTypes/VoucherTypes.tsx +++ b/src/discounts/components/VoucherTypes/VoucherTypes.tsx @@ -3,13 +3,13 @@ import CardContent from "@material-ui/core/CardContent"; import CardTitle from "@saleor/components/CardTitle"; import Grid from "@saleor/components/Grid"; import RadioGroupField from "@saleor/components/RadioGroupField"; +import { DiscountTypeEnum } from "@saleor/discounts/types"; import { DiscountErrorFragment } from "@saleor/fragments/types/DiscountErrorFragment"; import { getFormErrors } from "@saleor/utils/errors"; import getDiscountErrorMessage from "@saleor/utils/errors/discounts"; import React from "react"; import { useIntl } from "react-intl"; -import { DiscountValueTypeEnum } from "../../../types/globalTypes"; import { VoucherDetailsPageFormData } from "../VoucherDetailsPage"; interface VoucherTypesProps { @@ -35,21 +35,21 @@ const VoucherTypes = ({ defaultMessage: "Fixed Amount", description: "voucher discount type" }), - value: DiscountValueTypeEnum.FIXED + value: DiscountTypeEnum.VALUE_FIXED }, { label: intl.formatMessage({ defaultMessage: "Percentage", description: "voucher discount type" }), - value: DiscountValueTypeEnum.PERCENTAGE + value: DiscountTypeEnum.VALUE_PERCENTAGE }, { label: intl.formatMessage({ defaultMessage: "Free Shipping", description: "voucher discount type" }), - value: "SHIPPING" + value: DiscountTypeEnum.SHIPPING } ]; diff --git a/src/discounts/components/VoucherValue/VoucherValue.tsx b/src/discounts/components/VoucherValue/VoucherValue.tsx index 189645c492b..4b6105a2813 100644 --- a/src/discounts/components/VoucherValue/VoucherValue.tsx +++ b/src/discounts/components/VoucherValue/VoucherValue.tsx @@ -14,6 +14,7 @@ import Skeleton from "@saleor/components/Skeleton"; import TableHead from "@saleor/components/TableHead"; import TextFieldWithChoice from "@saleor/components/TextFieldWithChoice"; import { ChannelInput } from "@saleor/discounts/handlers"; +import { DiscountTypeEnum } from "@saleor/discounts/types"; import { DiscountErrorFragment } from "@saleor/fragments/types/DiscountErrorFragment"; import { renderCollection } from "@saleor/misc"; import { getFormErrors } from "@saleor/utils/errors"; @@ -21,7 +22,6 @@ import getDiscountErrorMessage from "@saleor/utils/errors/discounts"; import React from "react"; import { FormattedMessage, useIntl } from "react-intl"; -import { DiscountValueTypeEnum } from "../../../types/globalTypes"; import { translateVoucherTypes } from "../../translations"; import { VoucherDetailsPageFormData } from "../VoucherDetailsPage"; import { useStyles } from "./styles"; @@ -108,7 +108,7 @@ const VoucherValue: React.FC = props => { ChoiceProps={{ label: data.discountType === - DiscountValueTypeEnum.FIXED + DiscountTypeEnum.VALUE_FIXED ? listing.currency : "%", name: "discountType" as keyof FormData, diff --git a/src/discounts/fixtures.ts b/src/discounts/fixtures.ts index db6b8e164c5..82af48219c2 100644 --- a/src/discounts/fixtures.ts +++ b/src/discounts/fixtures.ts @@ -153,6 +153,7 @@ export const voucherList: VoucherList_vouchers_edges_node[] = [ country: "Germany" } ], + type: "ENTIRE_ORDER" as VoucherTypeEnum, discountValueType: "PERCENTAGE" as DiscountValueTypeEnum, endDate: null, id: "Vm91Y2hlcjox", @@ -183,6 +184,7 @@ export const voucherList: VoucherList_vouchers_edges_node[] = [ ], code: "FREE2020", countries: [], + type: "ENTIRE_ORDER" as VoucherTypeEnum, discountValueType: "FIXED" as DiscountValueTypeEnum, endDate: null, id: "Vm91Y2hlcjoy", diff --git a/src/discounts/handlers.ts b/src/discounts/handlers.ts index f2cf8e506bb..b65562c035e 100644 --- a/src/discounts/handlers.ts +++ b/src/discounts/handlers.ts @@ -1,8 +1,10 @@ import { ChannelSaleData, ChannelVoucherData } from "@saleor/channels/utils"; import { SaleDetailsPageFormData } from "@saleor/discounts/components/SaleDetailsPage"; import { VoucherDetailsPageFormData } from "@saleor/discounts/components/VoucherDetailsPage"; -import { RequirementsPicker } from "@saleor/discounts/types"; +import { DiscountTypeEnum, RequirementsPicker } from "@saleor/discounts/types"; +import { ChangeEvent, FormChange } from "@saleor/hooks/useForm"; import { RequireOnlyOne } from "@saleor/misc"; +import { VoucherTypeEnum } from "@saleor/types/globalTypes"; import { diff } from "fast-array-diff"; export interface ChannelArgs { discountValue: string; @@ -14,6 +16,29 @@ export type ChannelInput = RequireOnlyOne< "discountValue" | "minSpent" >; +export function createDiscountTypeChangeHandler(change: FormChange) { + return (formData: VoucherDetailsPageFormData, event: ChangeEvent) => { + if (formData.type === VoucherTypeEnum.SHIPPING) { + // if previously type was shipping + change({ + target: { + name: "type", + value: VoucherTypeEnum.ENTIRE_ORDER + } + }); + } else if (event.target.value === DiscountTypeEnum.SHIPPING) { + // if currently type should be shipping + change({ + target: { + name: "type", + value: VoucherTypeEnum.ENTIRE_ORDER + } + }); + } + change(event); + }; +} + export function createChannelsChangeHandler( channelListings: ChannelVoucherData[], updateChannels: (data: ChannelVoucherData[]) => void, diff --git a/src/discounts/types.ts b/src/discounts/types.ts index 9c15c2ed656..ada9c4bc731 100644 --- a/src/discounts/types.ts +++ b/src/discounts/types.ts @@ -3,3 +3,9 @@ export enum RequirementsPicker { ITEM = "ITEM", NONE = "NONE" } + +export enum DiscountTypeEnum { + VALUE_FIXED = "VALUE_FIXED", + VALUE_PERCENTAGE = "VALUE_PERCENTAGE", + SHIPPING = "SHIPPING" +} diff --git a/src/discounts/types/VoucherCataloguesAdd.ts b/src/discounts/types/VoucherCataloguesAdd.ts index 17e28de0967..2c3fc5c487f 100644 --- a/src/discounts/types/VoucherCataloguesAdd.ts +++ b/src/discounts/types/VoucherCataloguesAdd.ts @@ -3,7 +3,7 @@ // @generated // This file was automatically generated and should not be edited. -import { CatalogueInput, DiscountErrorCode, DiscountValueTypeEnum, VoucherTypeEnum } from "./../../types/globalTypes"; +import { CatalogueInput, DiscountErrorCode, VoucherTypeEnum, DiscountValueTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL mutation operation: VoucherCataloguesAdd @@ -172,11 +172,11 @@ export interface VoucherCataloguesAdd_voucherCataloguesAdd_voucher { startDate: any; endDate: any | null; usageLimit: number | null; + type: VoucherTypeEnum; discountValueType: DiscountValueTypeEnum; countries: (VoucherCataloguesAdd_voucherCataloguesAdd_voucher_countries | null)[] | null; minCheckoutItemsQuantity: number | null; channelListings: VoucherCataloguesAdd_voucherCataloguesAdd_voucher_channelListings[] | null; - type: VoucherTypeEnum; used: number; applyOncePerOrder: boolean; applyOncePerCustomer: boolean; diff --git a/src/discounts/types/VoucherCataloguesRemove.ts b/src/discounts/types/VoucherCataloguesRemove.ts index 131f364592c..05b2a5b7efe 100644 --- a/src/discounts/types/VoucherCataloguesRemove.ts +++ b/src/discounts/types/VoucherCataloguesRemove.ts @@ -3,7 +3,7 @@ // @generated // This file was automatically generated and should not be edited. -import { CatalogueInput, DiscountErrorCode, DiscountValueTypeEnum, VoucherTypeEnum } from "./../../types/globalTypes"; +import { CatalogueInput, DiscountErrorCode, VoucherTypeEnum, DiscountValueTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL mutation operation: VoucherCataloguesRemove @@ -172,11 +172,11 @@ export interface VoucherCataloguesRemove_voucherCataloguesRemove_voucher { startDate: any; endDate: any | null; usageLimit: number | null; + type: VoucherTypeEnum; discountValueType: DiscountValueTypeEnum; countries: (VoucherCataloguesRemove_voucherCataloguesRemove_voucher_countries | null)[] | null; minCheckoutItemsQuantity: number | null; channelListings: VoucherCataloguesRemove_voucherCataloguesRemove_voucher_channelListings[] | null; - type: VoucherTypeEnum; used: number; applyOncePerOrder: boolean; applyOncePerCustomer: boolean; diff --git a/src/discounts/types/VoucherChannelListingUpdate.ts b/src/discounts/types/VoucherChannelListingUpdate.ts index 90537b2dcd3..98821aca5f2 100644 --- a/src/discounts/types/VoucherChannelListingUpdate.ts +++ b/src/discounts/types/VoucherChannelListingUpdate.ts @@ -3,7 +3,7 @@ // @generated // This file was automatically generated and should not be edited. -import { VoucherChannelListingInput, DiscountErrorCode, DiscountValueTypeEnum } from "./../../types/globalTypes"; +import { VoucherChannelListingInput, DiscountErrorCode, VoucherTypeEnum, DiscountValueTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL mutation operation: VoucherChannelListingUpdate @@ -51,6 +51,7 @@ export interface VoucherChannelListingUpdate_voucherChannelListingUpdate_voucher startDate: any; endDate: any | null; usageLimit: number | null; + type: VoucherTypeEnum; discountValueType: DiscountValueTypeEnum; countries: (VoucherChannelListingUpdate_voucherChannelListingUpdate_voucher_countries | null)[] | null; minCheckoutItemsQuantity: number | null; diff --git a/src/discounts/types/VoucherCreate.ts b/src/discounts/types/VoucherCreate.ts index 8cbdd0e3019..75e5dc5e142 100644 --- a/src/discounts/types/VoucherCreate.ts +++ b/src/discounts/types/VoucherCreate.ts @@ -3,7 +3,7 @@ // @generated // This file was automatically generated and should not be edited. -import { VoucherInput, DiscountErrorCode, DiscountValueTypeEnum } from "./../../types/globalTypes"; +import { VoucherInput, DiscountErrorCode, VoucherTypeEnum, DiscountValueTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL mutation operation: VoucherCreate @@ -51,6 +51,7 @@ export interface VoucherCreate_voucherCreate_voucher { startDate: any; endDate: any | null; usageLimit: number | null; + type: VoucherTypeEnum; discountValueType: DiscountValueTypeEnum; countries: (VoucherCreate_voucherCreate_voucher_countries | null)[] | null; minCheckoutItemsQuantity: number | null; diff --git a/src/discounts/types/VoucherDetails.ts b/src/discounts/types/VoucherDetails.ts index cc4bcc4056f..c2020074a95 100644 --- a/src/discounts/types/VoucherDetails.ts +++ b/src/discounts/types/VoucherDetails.ts @@ -3,7 +3,7 @@ // @generated // This file was automatically generated and should not be edited. -import { DiscountValueTypeEnum, VoucherTypeEnum } from "./../../types/globalTypes"; +import { VoucherTypeEnum, DiscountValueTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL query operation: VoucherDetails @@ -165,11 +165,11 @@ export interface VoucherDetails_voucher { startDate: any; endDate: any | null; usageLimit: number | null; + type: VoucherTypeEnum; discountValueType: DiscountValueTypeEnum; countries: (VoucherDetails_voucher_countries | null)[] | null; minCheckoutItemsQuantity: number | null; channelListings: VoucherDetails_voucher_channelListings[] | null; - type: VoucherTypeEnum; used: number; applyOncePerOrder: boolean; applyOncePerCustomer: boolean; diff --git a/src/discounts/types/VoucherList.ts b/src/discounts/types/VoucherList.ts index 359f9120aa5..be264399178 100644 --- a/src/discounts/types/VoucherList.ts +++ b/src/discounts/types/VoucherList.ts @@ -3,7 +3,7 @@ // @generated // This file was automatically generated and should not be edited. -import { VoucherFilterInput, VoucherSortingInput, DiscountValueTypeEnum } from "./../../types/globalTypes"; +import { VoucherFilterInput, VoucherSortingInput, VoucherTypeEnum, DiscountValueTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL query operation: VoucherList @@ -44,6 +44,7 @@ export interface VoucherList_vouchers_edges_node { startDate: any; endDate: any | null; usageLimit: number | null; + type: VoucherTypeEnum; discountValueType: DiscountValueTypeEnum; countries: (VoucherList_vouchers_edges_node_countries | null)[] | null; minCheckoutItemsQuantity: number | null; diff --git a/src/discounts/types/VoucherUpdate.ts b/src/discounts/types/VoucherUpdate.ts index d626cbe9ddd..c3f6d9beeeb 100644 --- a/src/discounts/types/VoucherUpdate.ts +++ b/src/discounts/types/VoucherUpdate.ts @@ -3,7 +3,7 @@ // @generated // This file was automatically generated and should not be edited. -import { VoucherInput, DiscountErrorCode, DiscountValueTypeEnum } from "./../../types/globalTypes"; +import { VoucherInput, DiscountErrorCode, VoucherTypeEnum, DiscountValueTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL mutation operation: VoucherUpdate @@ -51,6 +51,7 @@ export interface VoucherUpdate_voucherUpdate_voucher { startDate: any; endDate: any | null; usageLimit: number | null; + type: VoucherTypeEnum; discountValueType: DiscountValueTypeEnum; countries: (VoucherUpdate_voucherUpdate_voucher_countries | null)[] | null; minCheckoutItemsQuantity: number | null; diff --git a/src/discounts/views/VoucherCreate/handlers.ts b/src/discounts/views/VoucherCreate/handlers.ts index 599484c6b8f..b1c6086b74b 100644 --- a/src/discounts/views/VoucherCreate/handlers.ts +++ b/src/discounts/views/VoucherCreate/handlers.ts @@ -1,6 +1,6 @@ import { VoucherDetailsPageFormData } from "@saleor/discounts/components/VoucherDetailsPage"; import { getChannelsVariables } from "@saleor/discounts/handlers"; -import { RequirementsPicker } from "@saleor/discounts/types"; +import { DiscountTypeEnum, RequirementsPicker } from "@saleor/discounts/types"; import { VoucherChannelListingUpdate, VoucherChannelListingUpdateVariables @@ -31,9 +31,11 @@ export function createHandler( applyOncePerOrder: formData.applyOncePerOrder, code: formData.code, discountValueType: - formData.discountType.toString() === "SHIPPING" + formData.discountType === DiscountTypeEnum.VALUE_PERCENTAGE ? DiscountValueTypeEnum.PERCENTAGE - : formData.discountType, + : formData.discountType === DiscountTypeEnum.VALUE_FIXED + ? DiscountValueTypeEnum.FIXED + : DiscountValueTypeEnum.PERCENTAGE, endDate: formData.hasEndDate ? joinDateTime(formData.endDate, formData.endTime) : null, @@ -43,7 +45,7 @@ export function createHandler( : parseFloat(formData.minCheckoutItemsQuantity), startDate: joinDateTime(formData.startDate, formData.startTime), type: - formData.discountType.toString() === "SHIPPING" + formData.discountType === DiscountTypeEnum.SHIPPING ? VoucherTypeEnum.SHIPPING : formData.type, usageLimit: formData.hasUsageLimit diff --git a/src/discounts/views/VoucherDetails/handlers.ts b/src/discounts/views/VoucherDetails/handlers.ts index 3814e7a063e..f64e05990fd 100644 --- a/src/discounts/views/VoucherDetails/handlers.ts +++ b/src/discounts/views/VoucherDetails/handlers.ts @@ -1,7 +1,7 @@ import { ChannelVoucherData } from "@saleor/channels/utils"; import { VoucherDetailsPageFormData } from "@saleor/discounts/components/VoucherDetailsPage"; import { getChannelsVariables } from "@saleor/discounts/handlers"; -import { RequirementsPicker } from "@saleor/discounts/types"; +import { DiscountTypeEnum, RequirementsPicker } from "@saleor/discounts/types"; import { VoucherChannelListingUpdate, VoucherChannelListingUpdateVariables @@ -28,17 +28,19 @@ export function createUpdateHandler( variables: VoucherChannelListingUpdateVariables; }) => Promise> ) { - return (formData: VoucherDetailsPageFormData) => { + return async (formData: VoucherDetailsPageFormData) => { const { id } = voucher; - updateVoucher({ + await updateVoucher({ id, input: { applyOncePerCustomer: formData.applyOncePerCustomer, applyOncePerOrder: formData.applyOncePerOrder, discountValueType: - formData.discountType.toString() === "SHIPPING" + formData.discountType === DiscountTypeEnum.VALUE_PERCENTAGE ? DiscountValueTypeEnum.PERCENTAGE - : formData.discountType, + : formData.discountType === DiscountTypeEnum.VALUE_FIXED + ? DiscountValueTypeEnum.FIXED + : DiscountValueTypeEnum.PERCENTAGE, endDate: formData.hasEndDate ? joinDateTime(formData.endDate, formData.endTime) : null, @@ -48,7 +50,7 @@ export function createUpdateHandler( : parseFloat(formData.minCheckoutItemsQuantity), startDate: joinDateTime(formData.startDate, formData.startTime), type: - formData.discountType.toString() === "SHIPPING" + formData.discountType === DiscountTypeEnum.SHIPPING ? VoucherTypeEnum.SHIPPING : formData.type, usageLimit: formData.hasUsageLimit diff --git a/src/fragments/discounts.ts b/src/fragments/discounts.ts index dc613a35c33..0ef48f48278 100644 --- a/src/fragments/discounts.ts +++ b/src/fragments/discounts.ts @@ -91,6 +91,7 @@ export const voucherFragment = gql` startDate endDate usageLimit + type discountValueType countries { code @@ -120,7 +121,6 @@ export const voucherDetailsFragment = gql` ${channelListingProductWithoutPricingFragment} fragment VoucherDetailsFragment on Voucher { ...VoucherFragment - type code usageLimit used diff --git a/src/fragments/types/VoucherDetailsFragment.ts b/src/fragments/types/VoucherDetailsFragment.ts index 2131d9ab364..2afdb3d15b4 100644 --- a/src/fragments/types/VoucherDetailsFragment.ts +++ b/src/fragments/types/VoucherDetailsFragment.ts @@ -3,7 +3,7 @@ // @generated // This file was automatically generated and should not be edited. -import { DiscountValueTypeEnum, VoucherTypeEnum } from "./../../types/globalTypes"; +import { VoucherTypeEnum, DiscountValueTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL fragment: VoucherDetailsFragment @@ -165,11 +165,11 @@ export interface VoucherDetailsFragment { startDate: any; endDate: any | null; usageLimit: number | null; + type: VoucherTypeEnum; discountValueType: DiscountValueTypeEnum; countries: (VoucherDetailsFragment_countries | null)[] | null; minCheckoutItemsQuantity: number | null; channelListings: VoucherDetailsFragment_channelListings[] | null; - type: VoucherTypeEnum; used: number; applyOncePerOrder: boolean; applyOncePerCustomer: boolean; diff --git a/src/fragments/types/VoucherFragment.ts b/src/fragments/types/VoucherFragment.ts index cae8ce67640..4ef7334a272 100644 --- a/src/fragments/types/VoucherFragment.ts +++ b/src/fragments/types/VoucherFragment.ts @@ -3,7 +3,7 @@ // @generated // This file was automatically generated and should not be edited. -import { DiscountValueTypeEnum } from "./../../types/globalTypes"; +import { VoucherTypeEnum, DiscountValueTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL fragment: VoucherFragment @@ -44,6 +44,7 @@ export interface VoucherFragment { startDate: any; endDate: any | null; usageLimit: number | null; + type: VoucherTypeEnum; discountValueType: DiscountValueTypeEnum; countries: (VoucherFragment_countries | null)[] | null; minCheckoutItemsQuantity: number | null; diff --git a/src/storybook/__snapshots__/Stories.test.ts.snap b/src/storybook/__snapshots__/Stories.test.ts.snap index e3dd0f7d0b2..97cd099b9c7 100644 --- a/src/storybook/__snapshots__/Stories.test.ts.snap +++ b/src/storybook/__snapshots__/Stories.test.ts.snap @@ -90535,7 +90535,7 @@ exports[`Storyshots Views / Discounts / Voucher create default 1`] = ` class="PrivateSwitchBase-input-id" name="discountType" type="radio" - value="FIXED" + value="VALUE_FIXED" />