From c30429f7f4602d171c76f8f25cc04a8299f02b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Szyma=C5=84ski?= Date: Tue, 23 Feb 2021 09:58:25 +0100 Subject: [PATCH] Implement inclusion feature of postal codes (#983) * Temporary merged schema and types update * Update typing for zipCodeRules -> postalCodeRules * Refactor zipCodes to postalCodes * Fix new schema webhooks * Delete postal code unassign dialog * Query inclusion type from backend * Clean up schema from old mutations * Proper new mutations structure, all postal code related changes * Linter changes * Fix bug with save not being refreshed after codes are added / deleted / inclusion change * Tests and translations * Fix warnings across app, minor variables refactor * Linting * Trigger deployment * CR changes, cleanups and refactors * Update snapshots * Resolve bug with radio not shows correct value on page refresh * Fix price and weight creation of codes * Reducer * Revert "Reducer" This reverts commit 07a3aed9c88332bde7d9be61b6dbc29e34e4edba. --- locale/defaultMessages.json | 111 ++- schema.graphql | 47 +- .../ChannelDeleteDialog.stories.tsx | 2 +- .../ChannelDeleteDialog.tsx | 16 +- src/channels/fixtures.ts | 2 +- .../CustomerAddress/CustomerAddress.tsx | 20 +- .../CustomerAddressListPage.tsx | 16 +- .../CustomerDetailsPage.tsx | 2 +- src/customers/fixtures.ts | 6 +- src/fragments/shipping.ts | 11 +- src/fragments/types/ShippingMethodFragment.ts | 9 +- ...ppingMethodWithExcludedProductsFragment.ts | 9 +- .../ShippingMethodWithPostalCodesFragment.ts | 23 + .../ShippingMethodWithZipCodesFragment.ts | 9 +- .../types/ShippingZoneDetailsFragment.ts | 9 +- src/index.tsx | 4 +- .../ShippingRateZipCodeRangeRemoveDialog.tsx | 42 -- .../index.ts | 2 - ...pingZonePostalCodeRangeDialog.stories.tsx} | 4 +- .../ShippingZonePostalCodeRangeDialog.tsx} | 11 +- .../index.ts | 2 + .../ShippingZonePostalCodes.tsx} | 166 +++-- .../ShippingZonePostalCodes/index.ts | 2 + .../ShippingZoneRatesCreatePage.stories.tsx | 26 +- .../ShippingZoneRatesCreatePage.tsx | 46 +- .../ShippingZoneRatesPage.stories.tsx | 5 +- .../ShippingZoneRatesPage.tsx | 52 +- .../ShippingZoneZipCodeRangeDialog/index.ts | 2 - .../components/ShippingZoneZipCodes/index.ts | 2 - src/shipping/fixtures.ts | 74 +- src/shipping/handlers.ts | 86 ++- src/shipping/mutations.ts | 52 -- src/shipping/types/CreateShippingRate.ts | 16 +- src/shipping/types/DeleteShippingRate.ts | 9 +- .../ShippingMethodChannelListingUpdate.ts | 9 +- .../types/ShippingMethodZipCodeRangeAssign.ts | 44 -- .../ShippingMethodZipCodeRangeUnassign.ts | 43 -- src/shipping/types/ShippingZone.ts | 9 +- src/shipping/types/UpdateShippingRate.ts | 9 +- .../PriceRatesCreate/PriceRatesCreate.tsx | 72 +- .../PriceRatesUpdate/PriceRatesUpdate.tsx | 161 ++-- .../WeightRatesCreate/WeightRatesCreate.tsx | 75 +- .../WeightRatesUpdate/WeightRatesUpdate.tsx | 161 ++-- src/shipping/views/utils.tsx | 7 + .../__snapshots__/Stories.test.ts.snap | 692 +++++++++--------- src/types/globalTypes.ts | 22 +- .../WebhookEvents/WebhookEvents.tsx | 12 + 47 files changed, 1088 insertions(+), 1123 deletions(-) create mode 100644 src/fragments/types/ShippingMethodWithPostalCodesFragment.ts delete mode 100644 src/shipping/components/ShippingRateZipCodeRangeRemoveDialog/ShippingRateZipCodeRangeRemoveDialog.tsx delete mode 100644 src/shipping/components/ShippingRateZipCodeRangeRemoveDialog/index.ts rename src/shipping/components/{ShippingZoneZipCodeRangeDialog/ShippingZoneZipCodeRangeDialog.stories.tsx => ShippingZonePostalCodeRangeDialog/ShippingZonePostalCodeRangeDialog.stories.tsx} (74%) rename src/shipping/components/{ShippingZoneZipCodeRangeDialog/ShippingZoneZipCodeRangeDialog.tsx => ShippingZonePostalCodeRangeDialog/ShippingZonePostalCodeRangeDialog.tsx} (89%) create mode 100644 src/shipping/components/ShippingZonePostalCodeRangeDialog/index.ts rename src/shipping/components/{ShippingZoneZipCodes/ShippingZoneZipCodes.tsx => ShippingZonePostalCodes/ShippingZonePostalCodes.tsx} (57%) create mode 100644 src/shipping/components/ShippingZonePostalCodes/index.ts delete mode 100644 src/shipping/components/ShippingZoneZipCodeRangeDialog/index.ts delete mode 100644 src/shipping/components/ShippingZoneZipCodes/index.ts delete mode 100644 src/shipping/types/ShippingMethodZipCodeRangeAssign.ts delete mode 100644 src/shipping/types/ShippingMethodZipCodeRangeUnassign.ts create mode 100644 src/shipping/views/utils.tsx diff --git a/locale/defaultMessages.json b/locale/defaultMessages.json index 694aeabbc..c4aa3efa4 100644 --- a/locale/defaultMessages.json +++ b/locale/defaultMessages.json @@ -5603,13 +5603,6 @@ "context": "label", "string": "Shipping rate name" }, - "src_dot_shipping_dot_components_dot_ShippingRateZipCodeRangeRemoveDialog_dot_3640694505": { - "string": "Are you sure you want to remove this postal code rule?" - }, - "src_dot_shipping_dot_components_dot_ShippingRateZipCodeRangeRemoveDialog_dot_76039652": { - "context": "header", - "string": "Remove postal codes from Shipping Rate" - }, "src_dot_shipping_dot_components_dot_ShippingWeightUnitForm_dot_2863708228": { "string": "This unit will be used as default shipping weight" }, @@ -5693,6 +5686,54 @@ "context": "label", "string": "Shipping zone name" }, + "src_dot_shipping_dot_components_dot_ShippingZonePostalCodeRangeDialog_dot_3070993206": { + "context": "range input label", + "string": "Postal codes (end)" + }, + "src_dot_shipping_dot_components_dot_ShippingZonePostalCodeRangeDialog_dot_3099331554": { + "context": "add postal code range, button", + "string": "Add" + }, + "src_dot_shipping_dot_components_dot_ShippingZonePostalCodeRangeDialog_dot_3419096551": { + "context": "range input label", + "string": "Postal codes (start)" + }, + "src_dot_shipping_dot_components_dot_ShippingZonePostalCodeRangeDialog_dot_3668595137": { + "string": "Please provide range of postal codes you want to add to the include/exclude list." + }, + "src_dot_shipping_dot_components_dot_ShippingZonePostalCodeRangeDialog_dot_3849853790": { + "context": "dialog header", + "string": "Add postal codes" + }, + "src_dot_shipping_dot_components_dot_ShippingZonePostalCodes_dot_1301350004": { + "string": "This shipping rate has no postal codes assigned" + }, + "src_dot_shipping_dot_components_dot_ShippingZonePostalCodes_dot_1680649143": { + "context": "action", + "string": "Include postal codes" + }, + "src_dot_shipping_dot_components_dot_ShippingZonePostalCodes_dot_1779803917": { + "string": "Added postal codes will be excluded from using this delivery methods. If none are added all postal codes will be able to use that shipping rate" + }, + "src_dot_shipping_dot_components_dot_ShippingZonePostalCodes_dot_1909179974": { + "context": "button", + "string": "Add postal code range" + }, + "src_dot_shipping_dot_components_dot_ShippingZonePostalCodes_dot_2274108851": { + "context": "number of postal code ranges", + "string": "{number} postal code ranges" + }, + "src_dot_shipping_dot_components_dot_ShippingZonePostalCodes_dot_2965119249": { + "string": "Only added postal codes will be able to use this shipping rate" + }, + "src_dot_shipping_dot_components_dot_ShippingZonePostalCodes_dot_3782353530": { + "context": "postal codes, header", + "string": "Postal codes" + }, + "src_dot_shipping_dot_components_dot_ShippingZonePostalCodes_dot_399764149": { + "context": "action", + "string": "Exclude postal codes" + }, "src_dot_shipping_dot_components_dot_ShippingZoneRatesCreatePage_dot_1161979494": { "context": "page title", "string": "Price Rate Create" @@ -5753,54 +5794,6 @@ "context": "input placeholder", "string": "Select Warehouse" }, - "src_dot_shipping_dot_components_dot_ShippingZoneZipCodeRangeDialog_dot_3070993206": { - "context": "range input label", - "string": "Postal codes (end)" - }, - "src_dot_shipping_dot_components_dot_ShippingZoneZipCodeRangeDialog_dot_3099331554": { - "context": "add postal code range, button", - "string": "Add" - }, - "src_dot_shipping_dot_components_dot_ShippingZoneZipCodeRangeDialog_dot_3419096551": { - "context": "range input label", - "string": "Postal codes (start)" - }, - "src_dot_shipping_dot_components_dot_ShippingZoneZipCodeRangeDialog_dot_3668595137": { - "string": "Please provide range of postal codes you want to add to the include/exclude list." - }, - "src_dot_shipping_dot_components_dot_ShippingZoneZipCodeRangeDialog_dot_3849853790": { - "context": "dialog header", - "string": "Add postal codes" - }, - "src_dot_shipping_dot_components_dot_ShippingZoneZipCodes_dot_1301350004": { - "string": "This shipping rate has no postal codes assigned" - }, - "src_dot_shipping_dot_components_dot_ShippingZoneZipCodes_dot_1680649143": { - "context": "action", - "string": "Include postal codes" - }, - "src_dot_shipping_dot_components_dot_ShippingZoneZipCodes_dot_1779803917": { - "string": "Added postal codes will be excluded from using this delivery methods. If none are added all postal codes will be able to use that shipping rate" - }, - "src_dot_shipping_dot_components_dot_ShippingZoneZipCodes_dot_1909179974": { - "context": "button", - "string": "Add postal code range" - }, - "src_dot_shipping_dot_components_dot_ShippingZoneZipCodes_dot_2274108851": { - "context": "number of postal code ranges", - "string": "{number} postal code ranges" - }, - "src_dot_shipping_dot_components_dot_ShippingZoneZipCodes_dot_2965119249": { - "string": "Only added postal codes will be able to use this shipping rate" - }, - "src_dot_shipping_dot_components_dot_ShippingZoneZipCodes_dot_3782353530": { - "context": "postal codes, header", - "string": "Postal codes" - }, - "src_dot_shipping_dot_components_dot_ShippingZoneZipCodes_dot_399764149": { - "context": "action", - "string": "Exclude postal codes" - }, "src_dot_shipping_dot_components_dot_ShippingZonesListPage_dot_1325966144": { "context": "header", "string": "Shipping" @@ -5860,10 +5853,6 @@ "src_dot_shipping_dot_views_dot_PriceRatesUpdate_dot_3823295269": { "string": "Manage Channel Availability" }, - "src_dot_shipping_dot_views_dot_PriceRatesUpdate_dot_4243341946": { - "context": "postal code range add error text", - "string": "Cannot add specified postal codes range." - }, "src_dot_shipping_dot_views_dot_PriceRatesUpdate_dot_870815507": { "context": "unassign products from shipping method, button", "string": "Unassign" @@ -5886,10 +5875,6 @@ "src_dot_shipping_dot_views_dot_WeightRatesUpdate_dot_3014453080": { "string": "Manage Channels Availability" }, - "src_dot_shipping_dot_views_dot_WeightRatesUpdate_dot_4243341946": { - "context": "postal code range add error text", - "string": "Cannot add specified postal codes range." - }, "src_dot_shipping_dot_views_dot_WeightRatesUpdate_dot_870815507": { "context": "unassign products from shipping method, button", "string": "Unassign" diff --git a/schema.graphql b/schema.graphql index d497db28f..9eb934068 100644 --- a/schema.graphql +++ b/schema.graphql @@ -2607,8 +2607,6 @@ type Mutation { shopAddressUpdate(input: AddressInput): ShopAddressUpdate orderSettingsUpdate(input: OrderSettingsUpdateInput!): OrderSettingsUpdate shippingMethodChannelListingUpdate(id: ID!, input: ShippingMethodChannelListingInput!): ShippingMethodChannelListingUpdate - shippingMethodZipCodeRulesCreate(input: ShippingZipCodeRulesCreateInput!, shippingMethodId: ID!): ShippingZipCodeRulesCreate - shippingMethodZipCodeRulesDelete(id: ID!): ShippingZipCodeRulesDelete shippingPriceCreate(input: ShippingPriceInput!): ShippingPriceCreate shippingPriceDelete(id: ID!): ShippingPriceDelete shippingPriceBulkDelete(ids: [ID]!): ShippingPriceBulkDelete @@ -3824,6 +3822,11 @@ input PluginUpdateInput { scalar PositiveDecimal +enum PostalCodeRuleInclusionTypeEnum { + INCLUDE + EXCLUDE +} + input PriceRangeInput { gte: Float lte: Float @@ -4746,7 +4749,7 @@ type ShippingMethod implements Node & ObjectWithMetadata { price: Money maximumOrderPrice: Money minimumOrderPrice: Money - zipCodeRules: [ShippingMethodZipCodeRule] + postalCodeRules: [ShippingMethodPostalCodeRule] excludedProducts(before: String, after: String, first: Int, last: Int): ProductCountableConnection } @@ -4776,6 +4779,13 @@ type ShippingMethodChannelListingUpdate { shippingErrors: [ShippingError!]! } +type ShippingMethodPostalCodeRule implements Node { + start: String + end: String + inclusionType: PostalCodeRuleInclusionTypeEnum + id: ID! +} + type ShippingMethodTranslatableContent implements Node { id: ID! name: String! @@ -4794,10 +4804,9 @@ enum ShippingMethodTypeEnum { WEIGHT } -type ShippingMethodZipCodeRule implements Node { - start: String +input ShippingPostalCodeRulesCreateInputRange { + start: String! end: String - id: ID! } type ShippingPriceBulkDelete { @@ -4838,6 +4847,9 @@ input ShippingPriceInput { minimumDeliveryDays: Int type: ShippingMethodTypeEnum shippingZone: ID + addPostalCodeRules: [ShippingPostalCodeRulesCreateInputRange!] + deletePostalCodeRules: [ID!] + inclusionType: PostalCodeRuleInclusionTypeEnum } type ShippingPriceRemoveProductFromExclude { @@ -4859,29 +4871,6 @@ type ShippingPriceUpdate { shippingErrors: [ShippingError!]! } -type ShippingZipCodeRulesCreate { - errors: [Error!]! @deprecated(reason: "Use typed errors with error codes. This field will be removed after 2020-07-31.") - zipCodeRules: [ShippingMethodZipCodeRule] - shippingMethod: ShippingMethod - shippingErrors: [ShippingError!]! -} - -input ShippingZipCodeRulesCreateInput { - zipCodeRules: [ShippingZipCodeRulesCreateInputRange]! -} - -input ShippingZipCodeRulesCreateInputRange { - start: String! - end: String -} - -type ShippingZipCodeRulesDelete { - errors: [Error!]! @deprecated(reason: "Use typed errors with error codes. This field will be removed after 2020-07-31.") - shippingMethod: ShippingMethod - shippingErrors: [ShippingError!]! - shippingMethodZipCodeRule: ShippingMethodZipCodeRule -} - type ShippingZone implements Node & ObjectWithMetadata { id: ID! name: String! diff --git a/src/channels/components/ChannelDeleteDialog/ChannelDeleteDialog.stories.tsx b/src/channels/components/ChannelDeleteDialog/ChannelDeleteDialog.stories.tsx index 0c0785f4e..871f077af 100644 --- a/src/channels/components/ChannelDeleteDialog/ChannelDeleteDialog.stories.tsx +++ b/src/channels/components/ChannelDeleteDialog/ChannelDeleteDialog.stories.tsx @@ -10,8 +10,8 @@ import ChannelDeleteDialog, { const props: ChannelDeleteDialogProps = { channelsChoices: mapNodeToChoice(channelsList), - hasOrders: true, confirmButtonState: "default", + hasOrders: true, onBack: () => undefined, onClose: () => undefined, onConfirm: () => undefined, diff --git a/src/channels/components/ChannelDeleteDialog/ChannelDeleteDialog.tsx b/src/channels/components/ChannelDeleteDialog/ChannelDeleteDialog.tsx index c97df56eb..87e9ad9ae 100644 --- a/src/channels/components/ChannelDeleteDialog/ChannelDeleteDialog.tsx +++ b/src/channels/components/ChannelDeleteDialog/ChannelDeleteDialog.tsx @@ -13,16 +13,20 @@ import { defineMessages, useIntl } from "react-intl"; import { useStyles } from "../styles"; const messages = defineMessages({ - needToBeMoved: { - defaultMessage: - "All order information from this channel need to be moved to a different channel. Please select channel orders need to be moved to:.", - description: "delete channel" + deleteChannel: { + defaultMessage: "Delete Channel", + description: "dialog header" }, deletingAllProductData: { defaultMessage: "Deleting channel will delete all product data regarding this channel. Are you sure you want to delete this channel?", description: "delete channel" }, + needToBeMoved: { + defaultMessage: + "All order information from this channel need to be moved to a different channel. Please select channel orders need to be moved to:.", + description: "delete channel" + }, noAvailableChannel: { defaultMessage: "There is no available channel to move order information to. Please create a channel with same currency so that information can be moved to it.", @@ -31,10 +35,6 @@ const messages = defineMessages({ selectChannel: { defaultMessage: "Select Channel", description: "dialog header" - }, - deleteChannel: { - defaultMessage: "Delete Channel", - description: "dialog header" } }); diff --git a/src/channels/fixtures.ts b/src/channels/fixtures.ts index f040c41cf..2a4617f63 100644 --- a/src/channels/fixtures.ts +++ b/src/channels/fixtures.ts @@ -72,8 +72,8 @@ export const channelsList: Channels_channels[] = [ { __typename: "Channel", currencyCode: "euro", - id: "Q2hhbm5lbDo5w0z", hasOrders: false, + id: "Q2hhbm5lbDo5w0z", isActive: true, name: "Channel USD", slug: "channel-usd1" diff --git a/src/customers/components/CustomerAddress/CustomerAddress.tsx b/src/customers/components/CustomerAddress/CustomerAddress.tsx index 900ccb803..5d0e8dddb 100644 --- a/src/customers/components/CustomerAddress/CustomerAddress.tsx +++ b/src/customers/components/CustomerAddress/CustomerAddress.tsx @@ -26,26 +26,26 @@ const messages = defineMessages({ defaultAddress: { defaultMessage: "Default Address" }, - defaultShippingAddress: { - defaultMessage: "Default Shipping Address" - }, defaultBillingAddress: { defaultMessage: "Default Billing Address" }, - setDefaultShipping: { - defaultMessage: "Set as default shipping address", - description: "button" + defaultShippingAddress: { + defaultMessage: "Default Shipping Address" }, - setDefaultBilling: { - defaultMessage: "Set as default billing address", + deleteAddress: { + defaultMessage: "Delete Address", description: "button" }, editAddress: { defaultMessage: "Edit Address", description: "button" }, - deleteAddress: { - defaultMessage: "Delete Address", + setDefaultBilling: { + defaultMessage: "Set as default billing address", + description: "button" + }, + setDefaultShipping: { + defaultMessage: "Set as default shipping address", description: "button" } }); diff --git a/src/customers/components/CustomerAddressListPage/CustomerAddressListPage.tsx b/src/customers/components/CustomerAddressListPage/CustomerAddressListPage.tsx index 731e3db38..e77239552 100644 --- a/src/customers/components/CustomerAddressListPage/CustomerAddressListPage.tsx +++ b/src/customers/components/CustomerAddressListPage/CustomerAddressListPage.tsx @@ -23,6 +23,14 @@ export interface CustomerAddressListPageProps { } const messages = defineMessages({ + addAddress: { + defaultMessage: "Add address", + description: "button" + }, + doesntHaveAddresses: { + defaultMessage: + "This customer doesn’t have any adresses added to his address book. You can add address using the button below." + }, fullNameAddress: { defaultMessage: "{fullName}'s Address Book", description: "customer's address book, header" @@ -31,16 +39,8 @@ const messages = defineMessages({ defaultMessage: "{fullName} Details", description: "customer details, header" }, - addAddress: { - defaultMessage: "Add address", - description: "button" - }, noAddressToShow: { defaultMessage: "There is no address to show for this customer" - }, - doesntHaveAddresses: { - defaultMessage: - "This customer doesn’t have any adresses added to his address book. You can add address using the button below." } }); diff --git a/src/customers/components/CustomerDetailsPage/CustomerDetailsPage.tsx b/src/customers/components/CustomerDetailsPage/CustomerDetailsPage.tsx index f4a2d8ae3..16836042d 100644 --- a/src/customers/components/CustomerDetailsPage/CustomerDetailsPage.tsx +++ b/src/customers/components/CustomerDetailsPage/CustomerDetailsPage.tsx @@ -64,8 +64,8 @@ const CustomerDetailsPage: React.FC = ({ firstName: customer?.firstName || "", isActive: customer?.isActive || false, lastName: customer?.lastName || "", - note: customer?.note || "", metadata: customer?.metadata.map(mapMetadataItemToInput), + note: customer?.note || "", privateMetadata: customer?.privateMetadata.map(mapMetadataItemToInput) }; diff --git a/src/customers/fixtures.ts b/src/customers/fixtures.ts index 018da8138..19b882ceb 100644 --- a/src/customers/fixtures.ts +++ b/src/customers/fixtures.ts @@ -946,8 +946,6 @@ export const customerList: ListCustomers_customers_edges_node[] = [ ]; export const customer: CustomerDetails_user & CustomerAddresses_user = { __typename: "User", - metadata: [], - privateMetadata: [], addresses: [ { __typename: "Address", @@ -1046,6 +1044,7 @@ export const customer: CustomerDetails_user & CustomerAddresses_user = { } ] }, + metadata: [], note: null, orders: { __typename: "OrderCountableConnection", @@ -1069,5 +1068,6 @@ export const customer: CustomerDetails_user & CustomerAddresses_user = { } } ] - } + }, + privateMetadata: [] }; diff --git a/src/fragments/shipping.ts b/src/fragments/shipping.ts index a77a19bb7..4d483024c 100644 --- a/src/fragments/shipping.ts +++ b/src/fragments/shipping.ts @@ -17,11 +17,12 @@ export const shippingZoneFragment = gql` } `; -export const shippingMethodWithZipCodesFragment = gql` - fragment ShippingMethodWithZipCodesFragment on ShippingMethod { +export const shippingMethodWithPostalCodesFragment = gql` + fragment ShippingMethodWithPostalCodesFragment on ShippingMethod { id - zipCodeRules { + postalCodeRules { id + inclusionType start end } @@ -30,9 +31,9 @@ export const shippingMethodWithZipCodesFragment = gql` export const shippingMethodFragment = gql` ${metadataFragment} ${fragmentMoney} - ${shippingMethodWithZipCodesFragment} + ${shippingMethodWithPostalCodesFragment} fragment ShippingMethodFragment on ShippingMethod { - ...ShippingMethodWithZipCodesFragment + ...ShippingMethodWithPostalCodesFragment ...MetadataFragment minimumOrderWeight { unit diff --git a/src/fragments/types/ShippingMethodFragment.ts b/src/fragments/types/ShippingMethodFragment.ts index 18f00b309..fc1740df5 100644 --- a/src/fragments/types/ShippingMethodFragment.ts +++ b/src/fragments/types/ShippingMethodFragment.ts @@ -2,15 +2,16 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. -import { WeightUnitsEnum, ShippingMethodTypeEnum } from "./../../types/globalTypes"; +import { PostalCodeRuleInclusionTypeEnum, WeightUnitsEnum, ShippingMethodTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL fragment: ShippingMethodFragment // ==================================================== -export interface ShippingMethodFragment_zipCodeRules { - __typename: "ShippingMethodZipCodeRule"; +export interface ShippingMethodFragment_postalCodeRules { + __typename: "ShippingMethodPostalCodeRule"; id: string; + inclusionType: PostalCodeRuleInclusionTypeEnum | null; start: string | null; end: string | null; } @@ -76,7 +77,7 @@ export interface ShippingMethodFragment_channelListings { export interface ShippingMethodFragment { __typename: "ShippingMethod"; id: string; - zipCodeRules: (ShippingMethodFragment_zipCodeRules | null)[] | null; + postalCodeRules: (ShippingMethodFragment_postalCodeRules | null)[] | null; metadata: (ShippingMethodFragment_metadata | null)[]; privateMetadata: (ShippingMethodFragment_privateMetadata | null)[]; minimumOrderWeight: ShippingMethodFragment_minimumOrderWeight | null; diff --git a/src/fragments/types/ShippingMethodWithExcludedProductsFragment.ts b/src/fragments/types/ShippingMethodWithExcludedProductsFragment.ts index c980fec94..c011e4aa0 100644 --- a/src/fragments/types/ShippingMethodWithExcludedProductsFragment.ts +++ b/src/fragments/types/ShippingMethodWithExcludedProductsFragment.ts @@ -2,15 +2,16 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. -import { WeightUnitsEnum, ShippingMethodTypeEnum } from "./../../types/globalTypes"; +import { PostalCodeRuleInclusionTypeEnum, WeightUnitsEnum, ShippingMethodTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL fragment: ShippingMethodWithExcludedProductsFragment // ==================================================== -export interface ShippingMethodWithExcludedProductsFragment_zipCodeRules { - __typename: "ShippingMethodZipCodeRule"; +export interface ShippingMethodWithExcludedProductsFragment_postalCodeRules { + __typename: "ShippingMethodPostalCodeRule"; id: string; + inclusionType: PostalCodeRuleInclusionTypeEnum | null; start: string | null; end: string | null; } @@ -107,7 +108,7 @@ export interface ShippingMethodWithExcludedProductsFragment_excludedProducts { export interface ShippingMethodWithExcludedProductsFragment { __typename: "ShippingMethod"; id: string; - zipCodeRules: (ShippingMethodWithExcludedProductsFragment_zipCodeRules | null)[] | null; + postalCodeRules: (ShippingMethodWithExcludedProductsFragment_postalCodeRules | null)[] | null; metadata: (ShippingMethodWithExcludedProductsFragment_metadata | null)[]; privateMetadata: (ShippingMethodWithExcludedProductsFragment_privateMetadata | null)[]; minimumOrderWeight: ShippingMethodWithExcludedProductsFragment_minimumOrderWeight | null; diff --git a/src/fragments/types/ShippingMethodWithPostalCodesFragment.ts b/src/fragments/types/ShippingMethodWithPostalCodesFragment.ts new file mode 100644 index 000000000..26f79fa6c --- /dev/null +++ b/src/fragments/types/ShippingMethodWithPostalCodesFragment.ts @@ -0,0 +1,23 @@ +/* tslint:disable */ +/* eslint-disable */ +// This file was automatically generated and should not be edited. + +import { PostalCodeRuleInclusionTypeEnum } from "./../../types/globalTypes"; + +// ==================================================== +// GraphQL fragment: ShippingMethodWithPostalCodesFragment +// ==================================================== + +export interface ShippingMethodWithPostalCodesFragment_postalCodeRules { + __typename: "ShippingMethodPostalCodeRule"; + id: string; + inclusionType: PostalCodeRuleInclusionTypeEnum | null; + start: string | null; + end: string | null; +} + +export interface ShippingMethodWithPostalCodesFragment { + __typename: "ShippingMethod"; + id: string; + postalCodeRules: (ShippingMethodWithPostalCodesFragment_postalCodeRules | null)[] | null; +} diff --git a/src/fragments/types/ShippingMethodWithZipCodesFragment.ts b/src/fragments/types/ShippingMethodWithZipCodesFragment.ts index 52a7d6567..87fab4dcf 100644 --- a/src/fragments/types/ShippingMethodWithZipCodesFragment.ts +++ b/src/fragments/types/ShippingMethodWithZipCodesFragment.ts @@ -2,13 +2,16 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. +import { PostalCodeRuleInclusionTypeEnum } from "./../../types/globalTypes"; + // ==================================================== // GraphQL fragment: ShippingMethodWithZipCodesFragment // ==================================================== -export interface ShippingMethodWithZipCodesFragment_zipCodeRules { - __typename: "ShippingMethodZipCodeRule"; +export interface ShippingMethodWithZipCodesFragment_postalCodeRules { + __typename: "ShippingMethodPostalCodeRule"; id: string; + inclusionType: PostalCodeRuleInclusionTypeEnum | null; start: string | null; end: string | null; } @@ -16,5 +19,5 @@ export interface ShippingMethodWithZipCodesFragment_zipCodeRules { export interface ShippingMethodWithZipCodesFragment { __typename: "ShippingMethod"; id: string; - zipCodeRules: (ShippingMethodWithZipCodesFragment_zipCodeRules | null)[] | null; + postalCodeRules: (ShippingMethodWithZipCodesFragment_postalCodeRules | null)[] | null; } diff --git a/src/fragments/types/ShippingZoneDetailsFragment.ts b/src/fragments/types/ShippingZoneDetailsFragment.ts index 1600675ae..9e1aea63c 100644 --- a/src/fragments/types/ShippingZoneDetailsFragment.ts +++ b/src/fragments/types/ShippingZoneDetailsFragment.ts @@ -2,7 +2,7 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. -import { WeightUnitsEnum, ShippingMethodTypeEnum } from "./../../types/globalTypes"; +import { PostalCodeRuleInclusionTypeEnum, WeightUnitsEnum, ShippingMethodTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL fragment: ShippingZoneDetailsFragment @@ -26,9 +26,10 @@ export interface ShippingZoneDetailsFragment_countries { country: string; } -export interface ShippingZoneDetailsFragment_shippingMethods_zipCodeRules { - __typename: "ShippingMethodZipCodeRule"; +export interface ShippingZoneDetailsFragment_shippingMethods_postalCodeRules { + __typename: "ShippingMethodPostalCodeRule"; id: string; + inclusionType: PostalCodeRuleInclusionTypeEnum | null; start: string | null; end: string | null; } @@ -94,7 +95,7 @@ export interface ShippingZoneDetailsFragment_shippingMethods_channelListings { export interface ShippingZoneDetailsFragment_shippingMethods { __typename: "ShippingMethod"; id: string; - zipCodeRules: (ShippingZoneDetailsFragment_shippingMethods_zipCodeRules | null)[] | null; + postalCodeRules: (ShippingZoneDetailsFragment_shippingMethods_postalCodeRules | null)[] | null; metadata: (ShippingZoneDetailsFragment_shippingMethods_metadata | null)[]; privateMetadata: (ShippingZoneDetailsFragment_shippingMethods_privateMetadata | null)[]; minimumOrderWeight: ShippingZoneDetailsFragment_shippingMethods_minimumOrderWeight | null; diff --git a/src/index.tsx b/src/index.tsx index eb9a26f03..13edb0f0a 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -167,8 +167,8 @@ const Routes: React.FC = () => { dispatchAppState({ payload: { - errorId, - error: "unhandled" + error: "unhandled", + errorId }, type: "displayError" }); diff --git a/src/shipping/components/ShippingRateZipCodeRangeRemoveDialog/ShippingRateZipCodeRangeRemoveDialog.tsx b/src/shipping/components/ShippingRateZipCodeRangeRemoveDialog/ShippingRateZipCodeRangeRemoveDialog.tsx deleted file mode 100644 index f04e41163..000000000 --- a/src/shipping/components/ShippingRateZipCodeRangeRemoveDialog/ShippingRateZipCodeRangeRemoveDialog.tsx +++ /dev/null @@ -1,42 +0,0 @@ -import DialogContentText from "@material-ui/core/DialogContentText"; -import ActionDialog from "@saleor/components/ActionDialog"; -import { ConfirmButtonTransitionState } from "@saleor/components/ConfirmButton"; -import { DialogProps } from "@saleor/types"; -import React from "react"; -import { FormattedMessage, useIntl } from "react-intl"; - -export interface ShippingRateZipCodeRangeRemoveDialogProps extends DialogProps { - confirmButtonState: ConfirmButtonTransitionState; - onConfirm: () => void; -} - -const ShippingRateZipCodeRangeRemoveDialog: React.FC = ({ - confirmButtonState, - open, - onClose, - onConfirm -}) => { - const intl = useIntl(); - - return ( - - - - - - ); -}; - -ShippingRateZipCodeRangeRemoveDialog.displayName = - "ShippingRateZipCodeRangeRemoveDialog"; -export default ShippingRateZipCodeRangeRemoveDialog; diff --git a/src/shipping/components/ShippingRateZipCodeRangeRemoveDialog/index.ts b/src/shipping/components/ShippingRateZipCodeRangeRemoveDialog/index.ts deleted file mode 100644 index 0d9ae9266..000000000 --- a/src/shipping/components/ShippingRateZipCodeRangeRemoveDialog/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./ShippingRateZipCodeRangeRemoveDialog"; -export { default } from "./ShippingRateZipCodeRangeRemoveDialog"; diff --git a/src/shipping/components/ShippingZoneZipCodeRangeDialog/ShippingZoneZipCodeRangeDialog.stories.tsx b/src/shipping/components/ShippingZonePostalCodeRangeDialog/ShippingZonePostalCodeRangeDialog.stories.tsx similarity index 74% rename from src/shipping/components/ShippingZoneZipCodeRangeDialog/ShippingZoneZipCodeRangeDialog.stories.tsx rename to src/shipping/components/ShippingZonePostalCodeRangeDialog/ShippingZonePostalCodeRangeDialog.stories.tsx index 84825d06c..ba9cb7125 100644 --- a/src/shipping/components/ShippingZoneZipCodeRangeDialog/ShippingZoneZipCodeRangeDialog.stories.tsx +++ b/src/shipping/components/ShippingZonePostalCodeRangeDialog/ShippingZonePostalCodeRangeDialog.stories.tsx @@ -2,12 +2,12 @@ import Decorator from "@saleor/storybook/Decorator"; import { storiesOf } from "@storybook/react"; import React from "react"; -import ShippingZoneZipCodeRangeDialog from "./ShippingZoneZipCodeRangeDialog"; +import ShippingZonePostalCodeRangeDialog from "./ShippingZonePostalCodeRangeDialog"; storiesOf("Shipping / Add postal code range", module) .addDecorator(Decorator) .add("default", () => ( - undefined} diff --git a/src/shipping/components/ShippingZoneZipCodeRangeDialog/ShippingZoneZipCodeRangeDialog.tsx b/src/shipping/components/ShippingZonePostalCodeRangeDialog/ShippingZonePostalCodeRangeDialog.tsx similarity index 89% rename from src/shipping/components/ShippingZoneZipCodeRangeDialog/ShippingZoneZipCodeRangeDialog.tsx rename to src/shipping/components/ShippingZonePostalCodeRangeDialog/ShippingZonePostalCodeRangeDialog.tsx index 02a2f498c..4baf45d25 100644 --- a/src/shipping/components/ShippingZoneZipCodeRangeDialog/ShippingZoneZipCodeRangeDialog.tsx +++ b/src/shipping/components/ShippingZonePostalCodeRangeDialog/ShippingZonePostalCodeRangeDialog.tsx @@ -16,7 +16,7 @@ import { DialogProps, MinMax } from "@saleor/types"; import React from "react"; import { FormattedMessage, useIntl } from "react-intl"; -export interface ShippingZoneZipCodeRangeDialogProps extends DialogProps { +export interface ShippingZonePostalCodeRangeDialogProps extends DialogProps { confirmButtonState: ConfirmButtonTransitionState; onSubmit: (range: MinMax) => void; } @@ -28,11 +28,11 @@ const useStyles = makeStyles( } }), { - name: "ShippingZoneZipCodeRangeDialog" + name: "ShippingZonePostalCodeRangeDialog" } ); -const ShippingZoneZipCodeRangeDialog: React.FC = ({ +const ShippingZonePostalCodeRangeDialog: React.FC = ({ confirmButtonState, open, onClose, @@ -105,5 +105,6 @@ const ShippingZoneZipCodeRangeDialog: React.FC; +export interface ShippingZonePostalCodesProps { disabled: boolean; initialExpanded?: boolean; - zipCodes: ShippingMethodFragment_zipCodeRules[] | undefined; - onZipCodeInclusionChange: FormChange; - onZipCodeDelete: (id: string) => void; - onZipCodeRangeAdd: () => void; + initialInclusionType?: PostalCodeRuleInclusionTypeEnum; + postalCodes: ShippingMethodFragment_postalCodeRules[] | undefined; + onPostalCodeInclusionChange: ( + inclusion: PostalCodeRuleInclusionTypeEnum + ) => void; + onPostalCodeDelete: (code: ShippingMethodFragment_postalCodeRules) => void; + onPostalCodeRangeAdd: () => void; } const useStyles = makeStyles( @@ -60,23 +57,45 @@ const useStyles = makeStyles( } }), { - name: "ShippingZoneZipCodes" + name: "ShippingZonePostalCodes" } ); -const ShippingZoneZipCodes: React.FC = ({ - data, +const ShippingZonePostalCodes: React.FC = ({ disabled, - initialExpanded, - zipCodes, - onZipCodeDelete, - onZipCodeInclusionChange, - onZipCodeRangeAdd + initialExpanded = true, + initialInclusionType = PostalCodeRuleInclusionTypeEnum.EXCLUDE, + postalCodes, + onPostalCodeDelete, + onPostalCodeInclusionChange, + onPostalCodeRangeAdd }) => { const [expanded, setExpanded] = React.useState(initialExpanded); + const [inclusionType, setInclusionType] = React.useState(null); const intl = useIntl(); const classes = useStyles({}); + const onInclusionRadioChange = (event: React.ChangeEvent) => { + const value = event.target.value; + setInclusionType(value); + onPostalCodeInclusionChange(value); + }; + + const getPostalCodeRangeLabel = ( + postalCodeRange: ShippingMethodFragment_postalCodeRules + ) => { + if (!postalCodeRange?.start) { + return ; + } + if (postalCodeRange?.end) { + return `${postalCodeRange.start} - ${postalCodeRange.end}`; + } + return postalCodeRange.start; + }; + + const getInlcusionType = () => + inclusionType === null ? initialInclusionType : inclusionType; + return ( = ({ toolbar={ } - onZipCodeAssign={() => openModal("add-range")} - onZipCodeUnassign={id => - openModal("remove-range", { - id - }) - } + onPostalCodeInclusionChange={onPostalCodeInclusionChange} + onPostalCodeAssign={() => openModal("add-range")} + onPostalCodeUnassign={onPostalCodeUnassign} /> - - assignZipCodeRange({ - variables: { - id: rateId, - input: { - zipCodeRules: [ - { - end: data.max || null, - start: data.min - } - ] - } - } - }) - } + onSubmit={code => { + onPostalCodeAssign(code); + setHavePostalCodesChanged(true); + }} open={params.action === "add-range"} /> - - unassignZipCodeRange({ - variables: { - id: params.id - } - }) - } - open={params.action === "remove-range"} - /> ); }; diff --git a/src/shipping/views/WeightRatesCreate/WeightRatesCreate.tsx b/src/shipping/views/WeightRatesCreate/WeightRatesCreate.tsx index da233ae65..eccc1e15d 100644 --- a/src/shipping/views/WeightRatesCreate/WeightRatesCreate.tsx +++ b/src/shipping/views/WeightRatesCreate/WeightRatesCreate.tsx @@ -5,13 +5,11 @@ import { } from "@saleor/channels/utils"; import ChannelsAvailabilityDialog from "@saleor/components/ChannelsAvailabilityDialog"; import { WindowTitle } from "@saleor/components/WindowTitle"; -import { ShippingMethodFragment_zipCodeRules } from "@saleor/fragments/types/ShippingMethodFragment"; import useChannels from "@saleor/hooks/useChannels"; import useNavigator from "@saleor/hooks/useNavigator"; import { sectionNames } from "@saleor/intl"; -import ShippingRateZipCodeRangeRemoveDialog from "@saleor/shipping/components/ShippingRateZipCodeRangeRemoveDialog"; +import ShippingZonePostalCodeRangeDialog from "@saleor/shipping/components/ShippingZonePostalCodeRangeDialog"; import ShippingZoneRatesCreatePage from "@saleor/shipping/components/ShippingZoneRatesCreatePage"; -import ShippingZoneZipCodeRangeDialog from "@saleor/shipping/components/ShippingZoneZipCodeRangeDialog"; import { useShippingRateCreator } from "@saleor/shipping/handlers"; import { ShippingRateCreateUrlDialog, @@ -19,10 +17,13 @@ import { shippingWeightRatesUrl, shippingZoneUrl } from "@saleor/shipping/urls"; +import filterPostalCodes from "@saleor/shipping/views/utils"; import { MinMax } from "@saleor/types"; -import { ShippingMethodTypeEnum } from "@saleor/types/globalTypes"; +import { + PostalCodeRuleInclusionTypeEnum, + ShippingMethodTypeEnum +} from "@saleor/types/globalTypes"; import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers"; -import { remove } from "@saleor/utils/lists"; import React from "react"; import { useIntl } from "react-intl"; @@ -38,9 +39,10 @@ export const WeightRatesCreate: React.FC = ({ const navigate = useNavigator(); const intl = useIntl(); - const [zipCodes, setZipCodes] = React.useState< - ShippingMethodFragment_zipCodeRules[] - >([]); + const [postalCodes, setPostalCodes] = React.useState([]); + const [radioInclusionType, setRadioInclusionType] = React.useState( + PostalCodeRuleInclusionTypeEnum.EXCLUDE + ); const { data: channelsData, loading: channelsLoading } = useChannelsList({}); @@ -70,30 +72,38 @@ export const WeightRatesCreate: React.FC = ({ createShippingRate, errors, status - } = useShippingRateCreator(id, ShippingMethodTypeEnum.WEIGHT, zipCodes); + } = useShippingRateCreator( + id, + ShippingMethodTypeEnum.WEIGHT, + postalCodes, + radioInclusionType + ); const handleBack = () => navigate(shippingZoneUrl(id)); - const handleZipCodeRangeAdd = (data: MinMax) => { - setZipCodes(zipCodes => [ - ...zipCodes, + const handlePostalCodeRangeAdd = (data: MinMax) => { + setPostalCodes(postalCodes => [ + ...postalCodes, { - __typename: "ShippingMethodZipCodeRule", + __typename: "ShippingMethodPostalCodeRule", end: data.max, - id: zipCodes.length.toString(), + id: postalCodes.length.toString(), + inclusionType: postalCodes?.[0]?.inclusionType, start: data.min } ]); closeModal(); }; - const handleZipCodeRangeDelete = (id: string) => { - setZipCodes(zipCodes => - remove( - zipCodes.find(zipCode => zipCode.id === id), - zipCodes, - (a, b) => a.id === b.id - ) - ); + + const onPostalCodeInclusionChange = ( + inclusion: PostalCodeRuleInclusionTypeEnum + ) => { + setRadioInclusionType(inclusion); + setPostalCodes([]); + }; + + const onPostalCodeUnassign = code => { + setPostalCodes(filterPostalCodes(postalCodes, code)); closeModal(); }; @@ -126,29 +136,20 @@ export const WeightRatesCreate: React.FC = ({ onBack={handleBack} errors={errors} channelErrors={channelErrors} - zipCodes={zipCodes} + postalCodes={postalCodes} openChannelsModal={handleChannelsModalOpen} onChannelsChange={setCurrentChannels} - onZipCodeAssign={() => openModal("add-range")} - onZipCodeUnassign={id => - openModal("remove-range", { - id - }) - } + onPostalCodeAssign={() => openModal("add-range")} + onPostalCodeUnassign={onPostalCodeUnassign} + onPostalCodeInclusionChange={onPostalCodeInclusionChange} variant={ShippingMethodTypeEnum.WEIGHT} /> - - handleZipCodeRangeDelete(params.id)} - open={params.action === "remove-range"} - /> ); }; diff --git a/src/shipping/views/WeightRatesUpdate/WeightRatesUpdate.tsx b/src/shipping/views/WeightRatesUpdate/WeightRatesUpdate.tsx index 53193e688..aa7984c31 100644 --- a/src/shipping/views/WeightRatesUpdate/WeightRatesUpdate.tsx +++ b/src/shipping/views/WeightRatesUpdate/WeightRatesUpdate.tsx @@ -20,11 +20,10 @@ import { commonMessages } from "@saleor/intl"; import useProductSearch from "@saleor/searches/useProductSearch"; import DeleteShippingRateDialog from "@saleor/shipping/components/DeleteShippingRateDialog"; import ShippingMethodProductsAddDialog from "@saleor/shipping/components/ShippingMethodProductsAddDialog"; -import ShippingRateZipCodeRangeRemoveDialog from "@saleor/shipping/components/ShippingRateZipCodeRangeRemoveDialog"; +import ShippingZonePostalCodeRangeDialog from "@saleor/shipping/components/ShippingZonePostalCodeRangeDialog"; import ShippingZoneRatesPage, { FormData } from "@saleor/shipping/components/ShippingZoneRatesPage"; -import ShippingZoneZipCodeRangeDialog from "@saleor/shipping/components/ShippingZoneZipCodeRangeDialog"; import UnassignDialog from "@saleor/shipping/components/UnassignDialog"; import { getShippingMethodChannelVariables, @@ -32,8 +31,6 @@ import { } from "@saleor/shipping/handlers"; import { useShippingMethodChannelListingUpdate, - useShippingMethodZipCodeRangeAssign, - useShippingMethodZipCodeRangeUnassign, useShippingPriceExcludeProduct, useShippingPriceRemoveProductsFromExclude, useShippingRateDelete, @@ -46,7 +43,12 @@ import { shippingWeightRatesEditUrl, shippingZoneUrl } from "@saleor/shipping/urls"; -import { ShippingMethodTypeEnum } from "@saleor/types/globalTypes"; +import filterPostalCodes from "@saleor/shipping/views/utils"; +import { MinMax } from "@saleor/types"; +import { + PostalCodeRuleInclusionTypeEnum, + ShippingMethodTypeEnum +} from "@saleor/types/globalTypes"; import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers"; import createMetadataUpdateHandler from "@saleor/utils/handlers/metadataUpdateHandler"; import { @@ -84,6 +86,51 @@ export const WeightRatesUpdate: React.FC = ({ ShippingRateUrlQueryParams >(navigate, params => shippingWeightRatesEditUrl(id, rateId, params), params); + const [codesToDelete, setCodesToDelete] = React.useState([]); + const [havePostalCodesChanged, setHavePostalCodesChanged] = React.useState( + false + ); + const [originalCodes, setOriginalCodes] = React.useState([]); + const [inclusionType, setInclusionType] = React.useState( + PostalCodeRuleInclusionTypeEnum.EXCLUDE + ); + + const onPostalCodeInclusionChange = ( + inclusion: PostalCodeRuleInclusionTypeEnum + ) => { + setInclusionType(inclusion); + setCodesToDelete( + rate.postalCodeRules + .filter(code => code.id !== undefined) + .map(code => code.id) + ); + setHavePostalCodesChanged(true); + rate.postalCodeRules = []; + }; + + const onPostalCodeAssign = (rule: MinMax) => { + if (!originalCodes.length) { + setOriginalCodes([...rate.postalCodeRules]); + } + if ( + rate.postalCodeRules.filter( + item => item.start === rule.min && item.end === rule.max + ).length > 0 + ) { + closeModal(); + return; + } + const newCode = { + __typename: undefined, + end: rule.max, + id: undefined, + inclusionType, + start: rule.min + }; + rate.postalCodeRules.push(newCode); + closeModal(); + }; + const { loadMore, search: productsSearch, @@ -171,50 +218,21 @@ export const WeightRatesUpdate: React.FC = ({ } }); - const [ - assignZipCodeRange, - assignZipCodeRangeOpts - ] = useShippingMethodZipCodeRangeAssign({ - onCompleted: data => { - if (data.shippingMethodZipCodeRulesCreate.errors.length === 0) { - notify({ - status: "success", - text: intl.formatMessage(commonMessages.savedChanges) - }); - closeModal(); - } else { - notify({ - status: "error", - text: intl.formatMessage({ - defaultMessage: "Cannot add specified postal codes range.", - description: "postal code range add error text" - }) - }); - } - } - }); - const [ - unassignZipCodeRange, - unassignZipCodeRangeOpts - ] = useShippingMethodZipCodeRangeUnassign({ - onCompleted: data => { - if (data.shippingMethodZipCodeRulesDelete.errors.length === 0) { - notify({ - status: "success", - text: intl.formatMessage(commonMessages.savedChanges) - }); - closeModal(); - } - } - }); - const [updateMetadata] = useMetadataUpdate({}); const [updatePrivateMetadata] = usePrivateMetadataUpdate({}); const updateData = async (data: FormData) => { const response = await updateShippingRate({ - variables: getUpdateShippingWeightRateVariables(data, id, rateId) + variables: getUpdateShippingWeightRateVariables( + data, + id, + rateId, + rate.postalCodeRules, + codesToDelete + ) }); + setCodesToDelete([]); + setHavePostalCodesChanged(false); const errors = response.data.shippingPriceUpdate.errors; if (errors.length === 0) { handleSuccess(); @@ -231,6 +249,18 @@ export const WeightRatesUpdate: React.FC = ({ return errors; }; + const onPostalCodeUnassign = code => { + if (code.id !== undefined) { + setCodesToDelete([...codesToDelete, code.id]); + rate.postalCodeRules = rate.postalCodeRules.filter( + rule => rule.id !== code.id + ); + } else { + rate.postalCodeRules = filterPostalCodes(rate.postalCodeRules, code); + } + setHavePostalCodesChanged(true); + }; + const handleSubmit = createMetadataUpdateHandler( rate, updateData, @@ -316,6 +346,7 @@ export const WeightRatesUpdate: React.FC = ({ assignProductOpts?.status === "loading" } hasChannelChanged={shippingChannels?.length !== currentChannels?.length} + havePostalCodesChanged={havePostalCodesChanged} saveButtonBarState={updateShippingRateOpts.status} onDelete={() => openModal("remove")} onSubmit={handleSubmit} @@ -346,45 +377,19 @@ export const WeightRatesUpdate: React.FC = ({ /> } - onZipCodeAssign={() => openModal("add-range")} - onZipCodeUnassign={id => - openModal("remove-range", { - id - }) - } + onPostalCodeInclusionChange={onPostalCodeInclusionChange} + onPostalCodeAssign={() => openModal("add-range")} + onPostalCodeUnassign={onPostalCodeUnassign} /> - - assignZipCodeRange({ - variables: { - id: rateId, - input: { - zipCodeRules: [ - { - end: data.max || null, - start: data.min - } - ] - } - } - }) - } + onSubmit={code => { + onPostalCodeAssign(code); + setHavePostalCodesChanged(true); + }} open={params.action === "add-range"} /> - - unassignZipCodeRange({ - variables: { - id: params.id - } - }) - } - open={params.action === "remove-range"} - /> ); }; diff --git a/src/shipping/views/utils.tsx b/src/shipping/views/utils.tsx new file mode 100644 index 000000000..077c4dcb6 --- /dev/null +++ b/src/shipping/views/utils.tsx @@ -0,0 +1,7 @@ +const filterPostalCodes = (postalCodes, codeToFilterOut) => + postalCodes.filter( + rule => + rule.start !== codeToFilterOut.start && rule.end !== codeToFilterOut.end + ); + +export default filterPostalCodes; diff --git a/src/storybook/__snapshots__/Stories.test.ts.snap b/src/storybook/__snapshots__/Stories.test.ts.snap index f0dac3fd8..5166159e0 100644 --- a/src/storybook/__snapshots__/Stories.test.ts.snap +++ b/src/storybook/__snapshots__/Stories.test.ts.snap @@ -17131,7 +17131,7 @@ exports[`Storyshots Shipping / ShippingZoneRatesCreatePage page create price 1`] > + + + @@ -208915,7 +208951,7 @@ exports[`Storyshots Views / Shipping / Shipping rate loading 1`] = ` >