Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

onSelection is not working when type is collection in Resource Picker

onSelection is not working when type is collection in Resource Picker

ubaidhussain
Shopify Partner
1 0 0

When I open Resource picker with type collection initially id doesn't work. I have to close and reopen it to work again. So if I am going to select collection I have to open Resource picker two times and it works perfectly for Product type.

Replies 3 (3)

olavoasantos
Shopify Staff
21 5 15

Hi @ubaidhussain. How are you?

 

My name is Olavo, I'm part of Shopify's App Bridge team. Thank you for reaching out.

 

I tried to reproduce this issue but couldn't. Could you provide:

 

* The exact version of App Bridge you're using

* A code sample of how you're using it

To learn more visit the Shopify Help Center or the Community Blog.

mohitpatel
Shopify Partner
15 0 0

can you please share the code I need to help you? 

mohitpatel
Shopify Partner
15 0 0
export default function Page() {
async function selectCollection() {
const existingCollectionIds = formData.collectionId.split(',');
console.log(existingCollectionIds);
console.log('test');
const selectionIds = existingCollectionIds.map(id => ({
id: id.trim(),
}));
console.log(selectionIds);
console.log('teste');
const selected = await window.shopify.resourcePicker({
type: 'collection',
selectionIds: selectionIds,
multiple: true,
filter: {
hidden: true,
draft: false,
archived: false,
},
});
 
if (selected.length > 0) {
const collectionId = [];
const collectionTitles = [];
const collectionHandles = [];
const collectionImages = [];
 
selected.forEach(collection => {
collectionId.push(collection.id);
collectionTitles.push(collection.title);
collectionHandles.push(collection.handle);
collectionImages.push(collection.image?.originalSrc || '');
});
 
setFormData({
...formData,
collectionId: collectionId.join(','),
collectionTitle: collectionTitles.join(','),
collectionHandle: collectionHandles.join(','),
collectionImage: collectionImages.join(','),
});
}
}
}
below code is prisma 

return (
<Box padding="400">
<Button onClick={selectCollection}>Select Collection</Button>
{formData.collectionId && (
<Box>
<DataTable
columnContentTypes={['text', 'text', 'text']}
headings={['Collection Image', 'Collection Title', 'Actions']}
rows={formData.collectionTitle.split(',').map((title, index) => [
formData.collectionImage.split(',')[index] ? (
<Thumbnail
source={formData.collectionImage.split(',')[index]}
alt={`Collection: ${title}`}
/>
) : null,
<Text as="span" variant="headingMd" fontWeight="semibold">
{title}
</Text>,
 
<Button icon={DeleteIcon} tone="critical" onClick={() => handleDeleteCollection(index)} />
])}
/>
<input type="hidden" name="collectionid" value={formData.collectionId} />
<input type="hidden" name="collectiontitle" value={formData.collectionTitle} />
<input type="hidden" name="collectionhandle" value={formData.collectionHandle} />
<input type="hidden" name="collectionimage" value={formData.collectionImage || ''} />
</Box>
)}
</Box>

)

above code is work for me my custom app for collection.Screenshot from 2024-09-26 11-46-03.pngScreenshot from 2024-09-26 11-45-51.png