Do not send availableForPurchaseDate if channel is unavailable (#2506)
This commit is contained in:
parent
5fdb51c0ee
commit
32cc9c9bdb
2 changed files with 129 additions and 12 deletions
|
@ -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],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
|
@ -19,6 +19,29 @@ const emptyListing: Omit<ProductChannelListingAddInput, "channelId"> = {
|
|||
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(
|
||||
product: Pick<ProductFragment, "channelListings">,
|
||||
triggerChange: () => void,
|
||||
|
@ -40,18 +63,14 @@ export function useProductChannelListingsForm(
|
|||
touched.current = uniq([...touched.current, id]);
|
||||
};
|
||||
|
||||
const handleChannelChange = useCallback((id: string, data: ChannelOpts) => {
|
||||
setChannels(prevData => ({
|
||||
...prevData,
|
||||
updateChannels: prevData.updateChannels.map(prevListing =>
|
||||
prevListing.channelId === id
|
||||
? { ...prevListing, ...data }
|
||||
: prevListing,
|
||||
),
|
||||
}));
|
||||
const handleChannelChange = useCallback(
|
||||
(id: string, data: ChannelOpts) => {
|
||||
setChannels(input => updateChannelsInput(input, data, id));
|
||||
triggerChange();
|
||||
touch(id);
|
||||
}, []);
|
||||
},
|
||||
[setChannels, triggerChange],
|
||||
);
|
||||
|
||||
const handleChannelListUpdate: ProductChannelsListingDialogSubmit = useCallback(
|
||||
({ added, removed }) => {
|
||||
|
|
Loading…
Reference in a new issue