import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixUsers from 'wix-users';
let product;
let user = wixUsers.currentUser;
$w.onReady(async function () {
loadRelatedProducts();
checkWishlist();
product = await $w('#productPage').getProduct();
$w('#loginMessage').onClick(loginMessageClick);
});
async function loadRelatedProducts() {
product = await $w('#productPage').getProduct();
let relatedProductResults = await Promise.all([
relatedProductsByTable(product),
]);
if (relatedProductResults[0].length > 0)
showRelatedProducts(relatedProductResults[0]);
else
showRelatedProducts(relatedProductResults[1]);
}
async function relatedProductsByTable(product) {
let productId = product._id;
// find related products by relation table
let relatedByTable = await Promise.all([
wixData.query('related-products')
.eq('productA', productId)
.include('productB')
.find(),
wixData.query('related-products')
.eq('productB', productId)
.include('productA')
.find()
]);
let relatedProducts = [
...relatedByTable[0].items.map(_ => _.productB),
...relatedByTable[1].items.map(_ => _.productA)
];
return relatedProducts;
}
function showRelatedProducts(relatedProducts){
if(relatedProducts.length > 0){
relatedProducts.splice(4, relatedProducts.length);
$w('#relatedItemsRepeater').onItemReady(relatedItemReady);
$w("#relatedItemsRepeater").data = relatedProducts;
$w("#relatedItems").expand();
}
else {
$w("#relatedItems").collapse();
}
}
function relatedItemReady($w, product){
$w("#productImage").src = product.mainMedia;
$w("#productName").text = product.name;
$w("#productPrice").text = product.formattedPrice;
$w('#productImage').onClick(() => {
wixLocation.to(product.productPageUrl);
});
}
export function inWishList_click(event, $w) {
if (user.loggedIn)
removeFromWishlist();
}
export function notInWishList_click(event, $w) {
if (user.loggedIn)
addToWishlist()
else
$w('#loginMessage').show();
}
async function loginMessageClick() {
let options = {"mode": "login"};
$w('#loginMessage').hide();
await wixUsers.promptLogin(options);
}
//Wishlist
async function addToWishlist() {
let wishListItem = {
product: product._id,
userId: user.id
};
$w('#notInWishList').hide('fade', {duration: 100});
$w('#inWishList').show('fade', {duration: 100});
let result = await wixData.insert("products-wishlist", wishListItem);
}
async function removeFromWishlist() {
let wishListResult = await wixData.query("products-wishlist")
.eq("product", product._id)
.eq("userId", user.id)
.find();
if (wishListResult.length > 0) {
$w('#notInWishList').show('fade', {duration: 100});
$w('#inWishList').hide('fade', {duration: 100});
await wixData.remove("products-wishlist", wishListResult.items[0]._id)
}
}
async function checkWishlist() {
if (wixUsers.currentUser.loggedIn) {
let wishListResult = await wixData.query("products-wishlist")
.eq("product", product._id)
.eq("userId", user.id)
.find();
if(wishListResult.items.length > 0)
$w('#inWishList').show('fade', {duration: 100});
else
$w('#notInWishList').show('fade', {duration: 100});
}
else {
$w('#notInWishList').show('fade', {duration: 100});
}
}