Skip to content

Commit

Permalink
Prevent crash during assigning product when has no channel (#5300)
Browse files Browse the repository at this point in the history
* The Product is not available in voucher when it has no channels

* Add changeset
  • Loading branch information
poulch committed Dec 5, 2024
1 parent 749a09f commit 3e89b07
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-lemons-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Assign product dialog no more crash when product has no channels
14 changes: 13 additions & 1 deletion src/components/AssignProductDialog/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" } },
Expand Down Expand Up @@ -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);
});
});
8 changes: 7 additions & 1 deletion src/components/AssignProductDialog/utils.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand Down

0 comments on commit 3e89b07

Please sign in to comment.