I tried doing this myself then gave up(more of a C# man myself). So I am currently following the Corvid tutorial on adding a wishlist to a site. See, I want the user to be able to add pages of a story I’m working on(it is broken up across multiple pages of the site, its meant to be explorative) to a “favorites” list in their profile. I have one Data list with the pages and the variables, another with the users’ “wishlists” aka the favorites.
Currently, I’m having trouble with converting the code to my needs; I can’t find out why it isn’t working other than on lines 17-20. What I’m trying to do in theory; is convert the data on that line of the data sheet i.e., the whole row of information(on the data sheet that holds the page data) and store that in the second sheet under a reference field type. I don’t know much java and any help would be useful or if I’m going about this the wholly wrong way, etcetera.
//-------------Imports-------------//
// Import the wix-data module for working with queries.
import wixData from 'wix-data';
// Import the wix-users module for working with users.
import wixUsers from 'wix-users';
//-------------Global Variables-------------//
// Current product.
let page;
// Current user.
let user = wixUsers.currentUser;
//-------------Page Setup-------------//
$w.onReady(async function () {
// Get the currently displayed product.
page = await $w('#Ch1Pages').getCurrentItem('logNumber', "000")._id;
// Check if the current product is in the wishlist and act accordingly.
checkWishlist();
// Set the action that occurs when the login message is clicked to be the loginMessageClick() function.
wixUsers.promptLogin();
});
// Check if the current product is in the wishlist and act accordingly.
async function checkWishlist() {
// If the current user is logged in:
if (wixUsers.currentUser.loggedIn) {
// Query the "products-wishlist" collection to find if the product was already added to the user's wishlist.
let wishListResult = await wixData.query("products-wishlist")
.eq("product", page._id)
.eq("userId", user.id)
.find();
// If the product was already added to the user's wishlist:
if(wishListResult.items.length > 0)
// Show the "inWishList" image with a fade effect.
$w('#Collected').show();
// If the product was not yet added to the user's wishlist:
else
// Show the "notInWishList" image with a fade effect.
$w('#Collect').show();
}
// If the current user is not logged in:
else {
// Show the "notInWishList" image with a fade effect.
$w('#Collect').show();
}
}
//-------------Event Handlers-------------//
// Set the action that occurs when the "inWishList" image is clicked.
export function inWishList_click(event, $w) {
// If the current user is logged in:
if (user.loggedIn)
// Remove the current product from the wishlist.
removeFromWishlist();
}
// Set the action that occurs when the "notInWishList" image is clicked.
export function notInWishList_click(event, $w) {
// If the current user is logged in:
if (user.loggedIn)
// Add the current product to the wishlist.
addToWishlist()
// If the current user is not logged in:
else
// Show the login message.
wixUsers.promptLogin();
}
// Set the action that occurs when the login message is clicked.
async function loginMessageClick() {
// Set the login options.
let options = {"mode": "login"};
// Hide the login message.
//$w('#loginMessage').hide();
// Prompt the user to login using the options created above.
await wixUsers.promptLogin(options);
}
//-------------Wishlist Functionality-------------//
// Add the current product to the current user's wishlist and update the page accordingly.
async function addToWishlist() {
// Create the wishlist item relating the current product to the current user.
let wishListItem = {
page: page._id,
userId: user.id
};
// Hide the "notInWishList" image with a fade effect.
$w('#Collect').hide();
// Show the "inWishList" image with a fade effect.
$w('#Collected').show();
// Insert the item created above into the "products-wishlist" collection.
let result = await wixData.insert("products-wishlist", wishListItem);
}
// Remove the current product to the current user's wishlist and update the page accordingly.
async function removeFromWishlist() {
// Query the "products-wishlist" collection to find the wishlist item corresponding to the current product and current user.
let wishListResult = await wixData.query("products-wishlist")
.eq("product", page._id)
.eq("userId", user.id)
.find();
// If a wishlist item was found:
if (wishListResult.length > 0) {
// Show the "notInWishList" image with a fade effect.
$w('#Collect').show();
// Hide the "inWishList" image with a fade effect.
$w('#Collected').hide();
// Remove the wishlist item from the "products-wishlist" collection.
await wixData.remove("products-wishlist", wishListResult.items[0]._id)
}
}