From 2e5fc94826972df992643c136f1ed699b4033836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chy=C5=82a?= Date: Wed, 4 Dec 2024 10:47:07 +0100 Subject: [PATCH 1/2] The Product is not available in voucher when it has no channels --- src/components/AssignProductDialog/utils.test.ts | 14 +++++++++++++- src/components/AssignProductDialog/utils.ts | 8 +++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/components/AssignProductDialog/utils.test.ts b/src/components/AssignProductDialog/utils.test.ts index 03c96b45f27..a5831b5c161 100644 --- a/src/components/AssignProductDialog/utils.test.ts +++ b/src/components/AssignProductDialog/utils.test.ts @@ -2,7 +2,7 @@ import { ProductChannels, SelectedChannel } from "./types"; import { isProductAvailableInVoucherChannels } from "./utils"; describe("isProductAvailableInVoucherChannels", () => { - it("should return trun when product has at least one channel common with voucher", () => { + it("should return true when product has at least one channel common with voucher", () => { // Arrange const mockProductChannels = [ { channel: { id: "1" } }, @@ -83,4 +83,16 @@ describe("isProductAvailableInVoucherChannels", () => { // Assert expect(result).toBe(true); }); + + it("should return false when no products channels", () => { + // Arrange + const mockProductChannels = undefined; + const mockVariantChannels = [] as SelectedChannel[]; + + // Act + const result = isProductAvailableInVoucherChannels(mockProductChannels, mockVariantChannels); + + // Assert + expect(result).toBe(false); + }); }); diff --git a/src/components/AssignProductDialog/utils.ts b/src/components/AssignProductDialog/utils.ts index 9b5da570fa0..b7ef4807af8 100644 --- a/src/components/AssignProductDialog/utils.ts +++ b/src/components/AssignProductDialog/utils.ts @@ -1,13 +1,19 @@ import { ProductChannels, SelectedChannel } from "./types"; export const isProductAvailableInVoucherChannels = ( - productChannels: ProductChannels, + productChannels?: ProductChannels, selectedChannels?: SelectedChannel[], ) => { + // If there are no selected channels, the product is available in all channels if (!selectedChannels) { return true; } + // If there are no product channels, the product is not available in any channel + if (!productChannels) { + return false; + } + const selectedChannelsIds = selectedChannels.map(chan => chan.id); const productChannelsIds = productChannels.map(chan => chan.channel.id); From 317fc427716d306fbeab8b2dc27602faaa5b5f1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chy=C5=82a?= Date: Wed, 4 Dec 2024 10:48:44 +0100 Subject: [PATCH 2/2] Add changeset --- .changeset/kind-lemons-shop.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/kind-lemons-shop.md diff --git a/.changeset/kind-lemons-shop.md b/.changeset/kind-lemons-shop.md new file mode 100644 index 00000000000..4d28988653a --- /dev/null +++ b/.changeset/kind-lemons-shop.md @@ -0,0 +1,5 @@ +--- +"saleor-dashboard": patch +--- + +Assign product dialog no more crash when product has no channels