Hey all, I’m trying to create a like button on a repeater that requires a login to press and its limited to one click per container. I’ve made one where you don’t need to log in and is limited to one click but when you refresh you can click again. I want to add a limit and a login check/limit to said button. If anyone can point me in the right direction that would help.
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixWindow from 'wix-window';
import wixUsers from 'wix-users';
var userId;
// disables items previous liked by the same user using IP address
function repeaterSettings(){
$w("#dynamicDataset").onReady(() => {
// stop user from liking the same pages again
wixData.query("LikesforPictures")
.eq("userId", userId)
.limit(1000)
.find()
.then((result) => {
if (result.items.length > 0) {
// check each repeater item to see if it has been liked already
$w("#repeater2").onItemReady( ($item, itemData, index) => {
var i;
for (i = 0; i < result.items.length; i++) {
console.log(result.items[i].title);
if(itemData._id === result.items[i].pageId ){
$item("#btnLike").hide(); $item("#greyLike").show();
}
}
});
}
})
.catch((err) => {
console.log(err);
});
});
}
// registers a new item like
export function btnLike_click(event) {
let $item = $w.at(event.context);
let clickedItemData = $item("#dynamicDataset").getCurrentItem();
// update the repeater in real time
let likes_calc = clickedItemData.likes + 1;
$item("#likesCounter").text = likes_calc.toString();
// hide the like button so it can't be clicked again
$item("#greyLike").show(); $item("#btnLike").hide();
// actually add the like to the collection in the back end
wixData.query("ArtworkAccepted")
.eq("_id", clickedItemData._id)
.limit(1)
.find()
.then((results) => {
let item = results.items[0];
item.likes = item.likes + 1;
wixData.update("ArtworkAccepted", item)
.then(() => {
console.log("Like added");
})
.catch((err) => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
});
}
// takes a like away
export function greyLike_click(event) {
let $item = $w.at(event.context);
let clickedItemData = $item("#dynamicDataset").getCurrentItem();
// update the repeater in real time
let likes_calc = clickedItemData.likes - 1;
$item("#likesCounter").text = likes_calc.toString();
// show the like button so it can be clicked again
$item("#greyLike").hide(); $item("#btnLike").show();
// actually take the like from the collection in the back end
wixData.query("ArtCollectionOld")
.eq("_id", clickedItemData._id)
.limit(1)
.find()
.then((results) => {
let item = results.items[0];
item.likes = item.likes - 1;
wixData.update("ArtCollectionOld", item)
.then(() => {
console.log("Like added");
})
.catch((err) => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
});
}
// saves like history by UserID to disable on next visit
function saveLikes(title, artworkTitle)
{
var toInsert;
toInsert = {
"title": title,
"artworkTitle": artworkTitle,
"UserId": userId
};
// add the item to the collection
wixData.insert("LikesforPictures", toInsert)
.catch((err) => {
console.log(err);
});
}
@admin67412 You’ll have to make appropriate adjustments to the code, but you certainly should be able to use the data so that users can follow who liked them.