Hi All!
I have users posts being shown on #repeater1 on a dynamic page. I have two collections, ’ marks’ where the users uploaded content for their post is saved.
The other collection being ‘favourites’, where the users favourites posts are saved. This collection has a userID field and a mark field.
-
There is an empty heart icon (#saveSong) within the repeater container for the user to click to save the item as a favourite. There is also a filled heart (#songSaved) shown to show that the item has been saved. If the user has saved an item but refresh’s the page, or returns back to this page, or see’s this post again, the #saveSong icon is shown rather than #songSaved? If the user has already saved an item, the filled heart is shown and the empty heart is hidden.
-
When a user clicked on the favourite button (#saveSong) in the repeater, it creates a new line in the #favourites collection. How do I set it so that only one line is created per user and the marks field contains all of their favourited items?
-
Lastly, is there any coding to remove/unfavourite an item from the marks field in the #favourites collection?
Here is my relevant current code:
$w("#repeater1").onItemReady(($item, itemData, index) => {
wixData.query("favourites")
.find()
.then((results) => {
if (results.items._id === itemData._id) {
$item("#saveSong").hide();
$item("#songSaved").show();
} else {
$item("#saveSong").show();
$item("#songSaved").hide();
}
///other code
}
$item("#saveSong").onClick((event) => {
const idOfItem = event.context.itemId;
let rowData;
let userId = wixUsers.currentUser.id;
let userphoto = itemData.mymarks[0].profilePhoto;
let name = itemData.mymarks[0].fullName;
$w('#repeater1').data.forEach((item) => {
if (item._id === idOfItem) {
rowData = item;
} else {
let toSave = {
"mark": item,
"userId": userId,
"userPhoto": userphoto,
"name": name
};
wixData.save("favourites", toSave);
}
});
console.log(rowData); // your item
$item("#saveSong").hide();
$item("#songSaved").show();
$item("#copiedBox").show();
$item("#InfoTextBox").label = "Added to Favourites";
setTimeout(function () {
$item("#copiedBox").hide();
},
3000); // <-- time in milliseconds
})
$item("#songSaved").onClick(() => {
$item("#saveSong").show();
$item("#songSaved").hide();
$item("#copiedBox").show();
$item("#InfoTextBox").label = "Removed from Favourites";
setTimeout(function () {
$item("#copiedBox").hide();
},
3000); // <-- time in milliseconds
})
Thanks in advance!