-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix improper availability on variant details page * Fix improper availability on variant details page * Fix strict typing * Add changeset * Fix multiple issues regarding availabilitiy channel list rendering * Disable create variant when no name provided * Simplify test * Trigger deployment * Fix naming * Extract mapping outside filtering * Remove unrelated messages
- Loading branch information
Showing
11 changed files
with
263 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
Fix variant availability |
31 changes: 23 additions & 8 deletions
31
src/products/components/ProductVariantChannels/ChannelsAvailabilityCard/AvailabilityCard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
...ucts/components/ProductVariantChannels/ChannelsAvailabilityCard/availabilityCount.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { | ||
ChannelPriceAndPreorderData, | ||
IChannelPriceAndPreorderArgs, | ||
} from "@dashboard/channels/utils"; | ||
import { | ||
ProductVariantCreateDataQuery, | ||
ProductVariantFragment, | ||
} from "@dashboard/graphql"; | ||
import { FormsetData } from "@dashboard/hooks/useFormset"; | ||
|
||
import { | ||
getAvailabilityCountForProduct, | ||
getAvailabilityCountForVariant, | ||
} from "./availabilityCount"; | ||
|
||
const mockedListings = [ | ||
{ | ||
id: "1", | ||
label: "Channel-PLN", | ||
}, | ||
{ | ||
id: "2", | ||
label: "Channel-USD", | ||
}, | ||
] as unknown as FormsetData< | ||
ChannelPriceAndPreorderData, | ||
IChannelPriceAndPreorderArgs | ||
>; | ||
|
||
const mockedVariant = { | ||
product: { | ||
channelListings: [ | ||
{ | ||
channel: { id: "1" }, | ||
isPublished: true, | ||
}, | ||
{ | ||
channel: { id: "2" }, | ||
isPublished: true, | ||
}, | ||
], | ||
}, | ||
} as unknown as ProductVariantFragment; | ||
|
||
const mockedProduct = { | ||
channelListings: [ | ||
{ | ||
channel: { id: "1" }, | ||
isPublished: true, | ||
}, | ||
{ | ||
channel: { id: "2" }, | ||
isPublished: true, | ||
}, | ||
], | ||
} as unknown as ProductVariantCreateDataQuery["product"]; | ||
|
||
describe("getAvailabilityCountForVariant", () => { | ||
it("should return correct counts when all channels are selected", () => { | ||
// Arrange | ||
const item = mockedVariant; | ||
const listings = mockedListings; | ||
|
||
// Act | ||
const result = getAvailabilityCountForVariant(item, listings); | ||
|
||
// Assert | ||
expect(result).toEqual({ | ||
publishedInChannelsCount: 2, | ||
availableChannelsCount: 2, | ||
}); | ||
}); | ||
|
||
it("should return correct counts when some channels are selected", () => { | ||
// Arrange | ||
const item = mockedVariant; | ||
const listings = mockedListings.slice(0, 1); | ||
|
||
// Act | ||
const result = getAvailabilityCountForVariant(item, listings); | ||
|
||
// Assert | ||
expect(result).toEqual({ | ||
publishedInChannelsCount: 1, | ||
availableChannelsCount: 2, | ||
}); | ||
}); | ||
|
||
it("should return correct counts when no channels are available", () => { | ||
// Arrange | ||
const item = mockedVariant; | ||
const listings = [] as FormsetData< | ||
ChannelPriceAndPreorderData, | ||
IChannelPriceAndPreorderArgs | ||
>; | ||
|
||
// Act | ||
const result = getAvailabilityCountForVariant(item, listings); | ||
|
||
// Assert | ||
expect(result).toEqual({ | ||
publishedInChannelsCount: 0, | ||
availableChannelsCount: 2, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("getAvailabilityCountForProduct", () => { | ||
it("should return correct counts when all channels are selected", () => { | ||
// Arrange | ||
const item = mockedProduct; | ||
const listings = mockedListings; | ||
|
||
// Act | ||
const result = getAvailabilityCountForProduct(item, listings); | ||
|
||
// Assert | ||
expect(result).toEqual({ | ||
publishedInChannelsCount: 2, | ||
availableChannelsCount: 2, | ||
}); | ||
}); | ||
|
||
it("should return correct counts when some channels are selected", () => { | ||
// Arrange | ||
const item = mockedProduct; | ||
const listings = mockedListings.slice(0, 1); | ||
|
||
// Act | ||
const result = getAvailabilityCountForProduct(item, listings); | ||
|
||
// Assert | ||
expect(result).toEqual({ | ||
publishedInChannelsCount: 1, | ||
availableChannelsCount: 2, | ||
}); | ||
}); | ||
|
||
it("should return correct counts when no channels are available", () => { | ||
// Arrange | ||
const item = mockedProduct; | ||
const listings = [] as FormsetData< | ||
ChannelPriceAndPreorderData, | ||
IChannelPriceAndPreorderArgs | ||
>; | ||
|
||
// Act | ||
const result = getAvailabilityCountForProduct(item, listings); | ||
|
||
// Assert | ||
expect(result).toEqual({ | ||
publishedInChannelsCount: 0, | ||
availableChannelsCount: 2, | ||
}); | ||
}); | ||
}); |
37 changes: 23 additions & 14 deletions
37
src/products/components/ProductVariantChannels/ChannelsAvailabilityCard/availabilityCount.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,48 @@ | ||
// @ts-strict-ignore | ||
import { | ||
ChannelPriceAndPreorderData, | ||
IChannelPriceAndPreorderArgs, | ||
} from "@dashboard/channels/utils"; | ||
import { | ||
ProductVariantCreateDataQuery, | ||
ProductVariantFragment, | ||
} from "@dashboard/graphql"; | ||
import { FormsetData } from "@dashboard/hooks/useFormset"; | ||
|
||
export const getAvailabilityCountForVariant = ( | ||
item: ProductVariantFragment, | ||
listings: FormsetData< | ||
ChannelPriceAndPreorderData, | ||
IChannelPriceAndPreorderArgs | ||
>, | ||
) => { | ||
const variantChannelListingsChannelsIds = item.channelListings.map( | ||
const allAvailableChannelsListings = item?.product?.channelListings?.map( | ||
({ channel: { id } }) => id, | ||
); | ||
|
||
const allAvailableChannelsListings = item.product.channelListings.filter( | ||
({ channel }) => variantChannelListingsChannelsIds.includes(channel.id), | ||
); | ||
|
||
const publishedInChannelsListings = allAvailableChannelsListings.filter( | ||
({ isPublished }) => isPublished, | ||
const selectedChannelListings = allAvailableChannelsListings?.filter( | ||
listing => listings.some(lst => lst.id === listing), | ||
); | ||
|
||
return { | ||
publishedInChannelsCount: publishedInChannelsListings.length, | ||
availableChannelsCount: allAvailableChannelsListings.length, | ||
publishedInChannelsCount: selectedChannelListings?.length, | ||
availableChannelsCount: allAvailableChannelsListings?.length, | ||
}; | ||
}; | ||
|
||
export const getAvailabilityCountForProduct = ( | ||
item: ProductVariantCreateDataQuery["product"], | ||
listings: FormsetData< | ||
ChannelPriceAndPreorderData, | ||
IChannelPriceAndPreorderArgs | ||
>, | ||
) => { | ||
const publishedInChannelsListings = item.channelListings.filter( | ||
({ isPublished }) => isPublished, | ||
const listingsIds = listings.map(({ id }) => id); | ||
const publishedInChannelsListings = item?.channelListings?.filter(lst => | ||
listingsIds.includes(lst.channel.id), | ||
); | ||
|
||
return { | ||
publishedInChannelsCount: publishedInChannelsListings.length, | ||
availableChannelsCount: item.channelListings.length, | ||
publishedInChannelsCount: publishedInChannelsListings?.length, | ||
availableChannelsCount: item?.channelListings?.length, | ||
}; | ||
}; |
Oops, something went wrong.