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 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);