I have my data on a dynamic page with repeater. I want to add a heart icon that users can click and save the item from repeater as favorite. I also want to add a heart counter to show which users have clicked it, something like how this forum’s heart button works.
I started with modifying the Wix tutorial - ’ Corvid Tutorial: Adding a Wishlist to a Wix Stores Site ’ and was able to get it running in a broken way.
My problem is when I load the page, all items show clicked heart state. How do I change this to show full heart for only the items that user have added to favorite?
I think I am going wrong somewhere in this section of code:
$w.onReady( function () {
//TODO: write your page related code here…
checkWishlist()
});
as
ync function checkWishlist() {
if (wixUsers.currentUser.loggedIn) {
let wishListResult = wixData.query( “Wishlist” )
.eq( “product” , product._id)
.eq( “userId” , user.id)
.find();
$w( “#listRepeater” ).forItems( [wishListResult], ($item, itemData, index) => {
$w( ‘#inWishList’ ).show( ‘fade’ , {duration: 100 });
})
}
else {
$w( ‘#notInWishList’ ).show( ‘fade’ , {duration: 100 });
}
}
Here is my complete code:
import wixData from ‘wix-data’ ;
import wixUsers from ‘wix-users’ ;
let product;
let user = wixUsers.currentUser;
$w.onReady( function () {
//TODO: write your page related code here…
checkWishlist()
});
async function addToWishlist() {
let wishListItem = {
“video” : product._id,
“User Id” : user.id
};
let result = await wixData.insert( “Wishlist” , wishListItem);
}
export function notInWishList_click_1(event,$w) {
//Add your code for this event here:
product = $w( ‘#dynamicDataset’ ).getCurrentItem();
if (user.loggedIn) {
addToWishlist();
$w( ‘#notInWishList’ ).hide( ‘fade’ , {duration: 100 });
$w( ‘#inWishList’ ).show( ‘fade’ , {duration: 100 });
}
else
$w( ‘#loginMessage’ ).show();
}
async function removeFromWishlist() {
let wishListResult = await wixData.query( “Wishlist” )
.eq( “video” , product._id)
.eq( “User Id” , user.id)
.find();
if (wishListResult.length > 0 ) {
$w( ‘#notInWishList’ ).show( ‘fade’ , {duration: 100 });
$w( ‘#inWishList’ ).hide( ‘fade’ , {duration: 100 });
await wixData.remove( “Wishlist” , wishListResult.items[ 0 ]._id)
}
}
export function inWishList_click(event,$w) {
//Add your code for this event here:
product = $w( ‘#dynamicDataset’ ).getCurrentItem();
if (user.loggedIn)
removeFromWishlist();
}
async function checkWishlist() {
if (wixUsers.currentUser.loggedIn) {
let wishListResult = wixData.query( “Wishlist” )
.eq( “product” , product._id)
.eq( “userId” , user.id)
.find();
$w( “#listRepeater” ).forItems( [wishListResult], ($item, itemData, index) => {
$w( ‘#inWishList’ ).show( ‘fade’ , {duration: 100 });
})
}
else {
$w( ‘#notInWishList’ ).show( ‘fade’ , {duration: 100 });
I have tried many different things but I dont know how do I do this for a repeater. Any idea how I can achieve this?
Thank you in advance and any pointers are appreciated.