saleor-dashboard/src/collections/utils.ts
Michał Droń 5dbd6fed8a
Fix assigning products to collection (#2320)
* Reset modal state in collection details

* cover initial state

* modal updates

* cover case when there is no items to add

Co-authored-by: andrzejewsky <vox3r69@gmail.com>
2022-09-21 15:35:05 +02:00

36 lines
1.1 KiB
TypeScript

import { ChannelCollectionData } from "@saleor/channels/utils";
import { CollectionDetailsQuery, SearchProductsQuery } from "@saleor/graphql";
export const createChannelsChangeHandler = (
channelListings: ChannelCollectionData[],
updateChannels: (data: ChannelCollectionData[]) => void,
triggerChange: () => void,
) => (id: string, data: Omit<ChannelCollectionData, "name" | "id">) => {
const channelIndex = channelListings.findIndex(channel => channel.id === id);
const channel = channelListings[channelIndex];
const updatedChannels = [
...channelListings.slice(0, channelIndex),
{
...channel,
...data,
},
...channelListings.slice(channelIndex + 1),
];
updateChannels(updatedChannels);
triggerChange();
};
export const getAssignedProductIdsToCollection = (
collection: CollectionDetailsQuery["collection"],
queryData: SearchProductsQuery["search"],
) => {
if (!queryData || !collection) {
return {};
}
return queryData.edges
.filter(e => e.node.collections.some(s => collection.id === s.id))
.map(e => ({ [e.node.id]: true }))
.reduce((p, c) => ({ ...p, ...c }), {});
};