I have been using this tutorial for product reviews: https://support.wix.com/en/article/corvid-tutorial-adding-ratings-and-reviews-to-a-wix-stores-site but as i am not running a shop I have tried to use it for reviewing ecotourism projects around the world that I have on a database and then access through a dynamic page. I have edited the ID’s from ‘product’ to ‘title’ as that is the fieldkey for the project. I am having no luck! Is it just impossible with the tutorial above to use outside of the wix stores app? I feel like I am linking the wrong dataset or get function from the first async fuction, but I am not sure. Is there something in my code I can change?
This is the code on the dynamic page:
import wixData from 'wix-data';
import wixWindow from 'wix-window';
let title;
//-------------Page Setup-------------//
$w.onReady(async function () {
title = await $w('#conservationDataset').getCurrentItem();
initReviews();
});
//-------------Loads the current product's reviews-------------//
async function initReviews () {
await $w('#ReviewsDatasetRead').setFilter(wixData.filter().eq('projectName', title._id));
showReviews();
loadStatistics();
}
//-------------Load the current product's statistics-------------//
async function loadStatistics() {
const stats = await wixData.get('Review-stats', title._id);
if (stats) {
let avgRating = (Math.round(stats.rating * 10 / stats.count) / 10);
let percentRecommended = Math.round(stats.recommended / stats.count * 100);
let ratings = $w('#generalRatings');
ratings.rating = avgRating;
ratings.numRatings = stats.count;
$w('#recoPercent').text = `${percentRecommended} % would recommend`;
$w('#generalRatings').show();
} else {
$w('#recoPercent').text = 'There are no reviews yet';
}
$w('#recoPercent').show();
}
//-------------Repeater Setup -------------//
export function reviewrepeater_itemReady($item, itemData, index) {
if (itemData.recommends) {
$w('#recommendation').text = 'I recommend this project.';
} else {
$w('#recommendation').text = "I don't recommend this project.";
}
// if (itemData.photo) {
// $w('#reviewImage').src = itemData.photo;
// $w('#reviewImage').expand();
//}
$w('#oneRating').rating = itemData.rating;
let date = itemData._createdDate;
$w('#submissionTime').text = date.toLocaleString();
}
//-------------Data Setup -------------//
export function showReviews() {
if ($w('#ReviewsDatasetRead').getTotalCount() > 0) {
$w('#reviewsStrip').expand();
} else {
$w('#reviewsStrip').collapse();
}
}
//------------- review button Handlers -------------//
export async function addReview_click(event, $w) {
const dataForLightbox = {
projectName: title._id
};
let result = await wixWindow.openLightbox("Review box", title);
$w('#ReviewsDatasetRead').refresh();
loadStatistics();
$w('#thankYouMessage').show();
}
//------------- load more review button Handlers -------------//
export function loadMoreReviews_click(event, $w) {
$w('#ReviewsDatasetRead').loadMore();
}
This is the code on my review box pop up:
import wixWindow from 'wix-window';
import wixData from 'wix-data';
let projectName;
$w.onReady(function () {
projectName = wixWindow.lightbox.getContext().projectName;
$w('#submitReviews').onBeforeSave(() => {
if ($w('#radioButtons').value === '') {
$w('#rateError').show();
return Promise.reject();
}
$w('#submitReviews').setFieldValues({
projectName,
rating: $w('#radioButtons').value,
recommends: $w('#radioGroup1').value
});
});
$w('#submitReviews').onAfterSave(async () => {
await updateStatistics($w('#radioGroup1').value);
wixWindow.lightbox.close();
});
});
async function updateStatistics(isRecommended) {
let stats = await wixData.get('Review-stats', projectName);
if (stats) {
stats.rating += parseInt($w('#radioButtons').value, 10);
stats.count += 1;
stats.recommended += (isRecommended === "true") ? 1 : 0;
return wixData.update('Review-stats', stats)
}
stats = {
_id: projectName,
rating: parseInt($w('#radioButtons').value, 10),
count: 1,
recommended: (isRecommended === "true") ? 1 : 0
};
return wixData.insert('Review-stats', stats)
}
export function radioButtons_change(event, $w) {
$w('#rateError').hide();
}