I wanna let the current url decide what info shall be displayed on a specific page and to combine that with code I need to know how to lay in this as a filter in the dataset:
let url = wixLocation.url;
let pageId = url.substr(url.lastIndexOf("/") + 1);
$w.onReady(async function () {
let videoInfo = await getVideoInfo(pageId);
Is a Dynamic page unsuitable for your need?
This is on a dynamic page and I have a very long and advanced code there which makes it complicated for me to know how to combine the info from the url with the earlier code.
My question is pretty simple; how to lay in the url-info as a filter in the dataset.
I have this gigantic code which works perfectly as long as I don´t have to combine it with the url code above (now I must do that). The big code is connected with 2 datasets and therefore I need to use the url-code as a filter in one of this datasets:
import { session } from 'wix-storage';
let filterWords = new FilterWords();
let bounceCenter = {
"duration": 500,
"direction": "center",
"intensity": "medium",
};
function login() {
wixUsers.promptLogin({ "mode": "login" })
}
$w.onReady(() => {
filterComments()
$w("#logout").onClick(logoutAndRedirect);
});
function logoutAndRedirect(event) {
Promise.all([wixLocation.to('/login'), wixUsers.logout()]);
}
function filterComments() {
$w('#dataset4').onReady(() => {
$w('#dataset4').setFilter(
wixData.filter()
.eq("pageId", $w("#dataset1").getCurrentItem()._id)
).then(() => {
let currentItem = $w("#dataset1").getCurrentItem();
let totalCount = $w("#dataset4").getTotalCount();
$w('#text344').text = totalCount.toString() + " ratings and comments"
wixData.aggregate("Pensiocomments")
.filter(wixData.filter().eq("pageId", $w('#dataset1').getCurrentItem()._id))
.sum("rating", "rating")
.run()
.then((results) => {
if (results.items.length < 1) {
$w('#ratingsDisplay').rating = 0
} else {
let sumAmount = results.items[0].rating
let rating = Number(sumAmount / totalCount).toFixed(1)
$w('#ratingsDisplay').rating = Number(rating)
}
});
likeFunctionComment()
addComment()
deleteComment()
})
})
}
function deleteComment() {
$w('#repeater1').forEachItem(($item, itemData, index) => {
console.log(itemData.currentUserId + " === " + wixUsers.currentUser.id)
if (itemData.currentUserId === wixUsers.currentUser.id) {
$item("#deleteComment").show()
} else {
$item("#deleteComment").hide()
}
})
$w('#deleteComment').onClick((event) => {
let $item = $w.at(event.context)
let currentItem = $item("#dataset4").getCurrentItem()
$item('#deleteComment').disable()
$item('#deleteComment').label = "Deleting..."
wixData.remove("Pensiocomments", currentItem._id)
.then(() => {
$w('#dataset4').refresh()
setTimeout(() => {
let totalCount = $w("#dataset4").getTotalCount();
$w('#text344').text = totalCount.toString() + " ratings and comments"
setTimeout(() => {
wixData.aggregate("Pensiocomments")
.filter(wixData.filter().eq("pageId", $w('#dataset1').getCurrentItem()._id))
.sum("rating", "rating")
.run()
.then((results) => {
if (results.items.length < 1) {
$w('#ratingsDisplay').rating = 0
} else {
let sumAmount = results.items[0].rating
let rating = Number(sumAmount / totalCount).toFixed(1)
$w('#ratingsDisplay').rating = Number(rating)
}
});
}, 2500)
}, 2000)
})
})
}
function addComment() {
$w('#button3').onClick(() => {
$w("#image63").hide();
$w("#image62").show();
$w("#audioPlayer").play();
setTimeout(() => {
$w("#image62").hide();
$w("#image63").show();
}, 150);
let value = $w('#textBox2').value.replace(/\s+/g, ' ')
if (value === ' ' || value === '') {
$w('#button3').enable()
$w('#infotext').text = 'Please enter a written comment'
$w('#infotext').show()
setTimeout(() => {
$w('#infotext').hide()
}, 5000)
} else {
if ($w('#ratingsInput').valid) {
insertComment()
} else {
$w('#infotext').text = 'Please first add a star rating for this video!'
$w('#infotext').show()
setTimeout(() => {
$w('#infotext').hide()
}, 9000)
}
}
})
}
function likeFunctionComment() {
let likes_array = [];
$w('#repeater1').onItemReady(($item, itemData, index) => {
let likes = itemData.likes;
if (likes) {
let parsed_likes = JSON.parse(likes);
$item("#likes").text = parsed_likes.length.toLocaleString();
} else {
$item("#likes").text = (0).toLocaleString();
}
if (itemData.likes) {
if (itemData.likes.includes(wixUsers.currentUser.id)) {
$item("#emptyHeart").hide();
$item("#fullHeart").show();
}
}
$item("#emptyHeart").onClick((event) => {
if (wixUsers.currentUser.loggedIn) {
let $itemm = $w.at(event.context);
toggleHeart($itemm, itemData)
} else {
let $item = $w.at(event.context);
$item('#emptyHeart').hide()
$item('#emptyHeart').show('bounce', bounceCenter)
setTimeout(() => {
login()
}, 500)
}
})
$item("#fullHeart").onClick((event) => {
let $itemm = $w.at(event.context);
toggleHeart($itemm, itemData);
});
})
async function toggleHeart(itemm, itemData) {
let emptyHeart = itemm("#emptyHeart");
let fullHeart = itemm("#fullHeart");
let likes = itemm("#likes");
if (fullHeart.hidden) {
fullHeart.show('bounce', bounceCenter);
emptyHeart.hide()
await saveHeart(itemData);
likes.text = (Number(likes.text) + 1).toLocaleString();
} else {
fullHeart.hide();
emptyHeart.show('bounce', bounceCenter);
await deleteHeart(itemData);
likes.text = (Number(likes.text) - 1).toLocaleString();
}
}
async function saveHeart(itemData) {
if (!itemData.likes) {
likes_array.push({
"_id": wixUsers.currentUser.id
})
itemData.likes = await JSON.stringify(likes_array);
wixData.update("Pensiocomments", itemData)
.then((results) => {});
} else {
likes_array = JSON.parse(itemData.likes);
likes_array.push({
"_id": wixUsers.currentUser.id
})
itemData.likes = await JSON.stringify(likes_array);
wixData.update("Pensiocomments", itemData)
.then((results) => {});
}
}
async function deleteHeart(itemData) {
if (itemData.likes) {
let parsed_likes = JSON.parse(itemData.likes);
let deleted_likes = parsed_likes.filter(function (obj) {
return obj._id !== wixUsers.currentUser.id;
});
itemData.likes = await JSON.stringify(deleted_likes);
wixData.update("Pensiocomments", itemData)
.then((results) => {});
}
}
}
function insertComment() {
let currentItem = $w('#dataset1').getCurrentItem()
wixData.insert('Pensiocomments', {
'comment': filterWords.clean($w('#textBox2').value),
'currentUserId': wixUsers.currentUser.id,
'memberReference': wixUsers.currentUser.id,
'pageReference': currentItem._id,
'pageId': currentItem._id,
'rating': $w('#ratingsInput').value
})
.catch((error) => {
$w('#button3').enable()
$w('#infotext').text = "Something isn't right. Error: " + error
$w('#infotext').expand()
setTimeout(() => {
$w('#infotext').text = "Comment isn't available. The comment was deleted or was taken down"
$w('#infotext').collapse()
}, 7500)
})
.then(() => {
$w('#textBox2').value = ''
$w('#button3').enable()
$w('#dataset4').refresh()
.then(() => {
let totalCount = $w("#dataset4").getTotalCount();
$w('#text344').text = totalCount.toString() + " ratings and comments"
wixData.aggregate("Pensiocomments")
.filter(wixData.filter().eq("pageId", $w('#dataset1').getCurrentItem()._id))
.sum("rating", "rating")
.run()
.then((results) => {
if (results.items.length < 1) {
$w('#ratingsDisplay').rating = 0
} else {
let sumAmount = results.items[0].rating
let rating = Number(sumAmount / totalCount).toFixed(1)
$w('#ratingsDisplay').rating = Number(rating)
setTimeout(() => {
$w('#ratingsInput').resetValidityIndication();
}, 100)
$w('#dataset4').refresh()
}
});
deleteComment()
})
})
}
The simple solution in this case was to use the page´s dynamic dataset instead of an ordinary dataset 