Fix product channels update error (#2528)

* Fix product channels update error

Fixed error occured after trying to update availability of variant
channel in datagrid, which (channel) was later on removed from being available on
product. Now variant channel availability shouldn't be appended to channel listing
update input when channel has been disabled on product.

* Add test of inferProductChannelsAfterUpdate
This commit is contained in:
Dawid 2022-11-17 12:47:11 +01:00 committed by GitHub
parent ac063c6410
commit 03f2b5c544
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 96 additions and 4 deletions

View file

@ -0,0 +1,76 @@
import { ProductFragment } from "@saleor/graphql";
import { ProductUpdateSubmitData } from "@saleor/products/components/ProductUpdatePage/types";
import { inferProductChannelsAfterUpdate } from "./utils";
describe("Product update utils", () => {
it("should infer product channels after update with data", () => {
const product = {
channelListings: [
{
channel: {
id: "1",
},
},
{
channel: {
id: "2",
},
},
{
channel: {
id: "3",
},
},
],
} as ProductFragment;
const submitData = {
channels: {
updateChannels: [
{
channelId: "1",
},
{
channelId: "4",
},
],
removeChannels: ["2"],
},
} as ProductUpdateSubmitData;
const result = inferProductChannelsAfterUpdate(product, submitData);
expect(result).toEqual(["1", "3", "4"]);
});
it("should infer product channels after update without data", () => {
const product = {
channelListings: [
{
channel: {
id: "1",
},
},
{
channel: {
id: "2",
},
},
{
channel: {
id: "3",
},
},
],
} as ProductFragment;
const submitData = {
channels: {},
} as ProductUpdateSubmitData;
const result = inferProductChannelsAfterUpdate(product, submitData);
expect(result).toEqual(["1", "2", "3"]);
});
});

View file

@ -61,14 +61,30 @@ const hasChannel = (
return variant.channelListings.some(c => c.channel.id === channelId);
};
export function inferProductChannelsAfterUpdate(
product: ProductFragment,
data: ProductUpdateSubmitData,
) {
const productChannelsIds = product.channelListings.map(
listing => listing.channel.id,
);
const updatedChannelsIds =
data.channels.updateChannels?.map(listing => listing.channelId) || [];
const removedChannelsIds = data.channels.removeChannels || [];
return uniq([
...productChannelsIds.filter(
channelId => !removedChannelsIds.includes(channelId),
),
...updatedChannelsIds,
]);
}
export function getProductChannelsUpdateVariables(
product: ProductFragment,
data: ProductUpdateSubmitData,
): ProductChannelListingUpdateMutationVariables {
const channels = uniq([
...product.channelListings.map(listing => listing.channel.id),
...data.channels.updateChannels.map(listing => listing.channelId),
]);
const channels = inferProductChannelsAfterUpdate(product, data);
const dataUpdated = new Map<string, ProductChannelListingAddInput>();
data.channels.updateChannels