Do not send availableForPurchaseDate if channel is unavailable (#2506)

This commit is contained in:
Krzysztof Żuraw 2022-11-09 17:41:11 +01:00 committed by GitHub
parent 5fdb51c0ee
commit 32cc9c9bdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 129 additions and 12 deletions

View file

@ -0,0 +1,98 @@
import { updateChannelsInput } from "./formChannels";
describe("ProductUpdatePage - fromChannels", () => {
describe("updateChannelsInput", () => {
const channel = {
channelId: "Q2hhbm5lbDox",
availableForPurchaseDate: null,
__typename: "ProductChannelListing",
isPublished: true,
publicationDate: "2020-01-01",
isAvailableForPurchase: false,
availableForPurchase: null,
visibleInListings: true,
channel: {
__typename: "Channel",
id: "Q2hhbm5lbDox",
name: "Default channel",
currencyCode: "USD",
},
};
it("should update availableForPurchaseDate if isAvailableForPurchase is set to true", () => {
const input = {
removeChannels: [],
updateChannels: [channel],
};
const data = {
availableForPurchase: "2020-10-01",
isAvailableForPurchase: true,
isPublished: true,
publicationDate: "2020-01-01",
visibleInListings: true,
};
const result = updateChannelsInput(input, data, channel.channelId);
expect(result).toEqual({
removeChannels: [],
updateChannels: [
{
...channel,
isAvailableForPurchase: true,
availableForPurchaseDate: "2020-10-01",
availableForPurchase: "2020-10-01",
},
],
});
});
it("should set availableForPurchaseDate to null if isAvailableForPurchase is set to false", () => {
const oldData = {
removeChannels: [],
updateChannels: [channel],
};
const newData = {
availableForPurchase: "2020-10-01",
isAvailableForPurchase: false,
isPublished: true,
publicationDate: "2020-01-01",
visibleInListings: true,
};
const result = updateChannelsInput(oldData, newData, channel.channelId);
expect(result).toEqual({
removeChannels: [],
updateChannels: [
{
...channel,
availableForPurchaseDate: null,
availableForPurchase: "2020-10-01",
},
],
});
});
it("should not update listing if channel id do not match", () => {
const input = {
removeChannels: [],
updateChannels: [channel],
};
const data = {
availableForPurchase: "2020-10-01",
isAvailableForPurchase: true,
isPublished: true,
publicationDate: "2020-01-01",
visibleInListings: true,
};
const result = updateChannelsInput(input, data, "42");
expect(result).toEqual({
removeChannels: [],
updateChannels: [channel],
});
});
});
});

View file

@ -19,6 +19,29 @@ const emptyListing: Omit<ProductChannelListingAddInput, "channelId"> = {
visibleInListings: false, visibleInListings: false,
}; };
export const updateChannelsInput = (
input: ProductChannelListingUpdateInput,
data: ChannelOpts,
id: string,
) => {
const mergeListings = (listing: ProductChannelListingAddInput) => {
if (listing.channelId === id) {
return {
...listing,
...data,
availableForPurchaseDate: data.isAvailableForPurchase
? data.availableForPurchase
: null,
};
}
return listing;
};
return {
...input,
updateChannels: input.updateChannels.map(mergeListings),
};
};
export function useProductChannelListingsForm( export function useProductChannelListingsForm(
product: Pick<ProductFragment, "channelListings">, product: Pick<ProductFragment, "channelListings">,
triggerChange: () => void, triggerChange: () => void,
@ -40,18 +63,14 @@ export function useProductChannelListingsForm(
touched.current = uniq([...touched.current, id]); touched.current = uniq([...touched.current, id]);
}; };
const handleChannelChange = useCallback((id: string, data: ChannelOpts) => { const handleChannelChange = useCallback(
setChannels(prevData => ({ (id: string, data: ChannelOpts) => {
...prevData, setChannels(input => updateChannelsInput(input, data, id));
updateChannels: prevData.updateChannels.map(prevListing => triggerChange();
prevListing.channelId === id touch(id);
? { ...prevListing, ...data } },
: prevListing, [setChannels, triggerChange],
), );
}));
triggerChange();
touch(id);
}, []);
const handleChannelListUpdate: ProductChannelsListingDialogSubmit = useCallback( const handleChannelListUpdate: ProductChannelsListingDialogSubmit = useCallback(
({ added, removed }) => { ({ added, removed }) => {