So, the issues are as follows:
-
The reviews data is being captured to the “Reviews” database, but the statistics aren’t coming up in the “reviews stats” database. It’s a little thing, but it’s bugging me.
-
Product ID isn’t being captured or linked to the review, so there’s not way to filter the reviews by product, it’s just one muckety muck.
-
submitting a reveiew and then refreshing the page brings up the newly submitted review for an entire 5 seconds, before the reviews repeater on the product page goes completely blank.
Possible reasons: I am 100% NOT a proficient coder, and just sort of tra’la’la’ed my way through the tutorial, and did eventually fix all of the errors in the code (so that the little red dots went away). But that by no means suggests that I would be able to identify a glaring error in my work, if a little flag doesn’t pop up.
The answer to any questions there may be as to why, how and what with the code is unanimously “I don’t know.”
Current light box code:
import wixWindow from ‘wix-window’;
import wixData from ‘wix-data’;
//----------------GLOBAL VARIABLES------------------//
let productID;
//-----------------LIGHTBOX SETUP--------------//
$w.onReady( function (){
productID = wixWindow.lightbox.getContext().productId;
$w ('#dataset2').onBeforeSave(() => {
if ($w(‘#ratingsInput1’).value === ‘’) {
$w('#rateError').show()
return Promise.reject();
}
$w('#dataset2').setFieldValues({
productId,
rating: $w('#ratingsInput1').value,
recommends: $w('#radioGroup1').value
});
});
$w('#dataset2')
wixWindow.lightbox.close();
});
async function updateStatistics(isRecommended) {
let stats = await wixData.get(‘review-stats’, productId);
if (stats) {
stats.rating += parseInt($w(‘#ratingsInput1’).value, 10);
stats.count += 1;
stats.recommended += (isRecommended === "true") ? 1 :0;
return wixData.update(‘review-stats’, stats)
}
stats = {
_id: productId,
rating: parseInt($w('#ratingsInput1').value, 10)
count: 1,
recommended: (isRecommended === "true") ? 1 : 0
};
return wixData.insert(‘review-stats’, stats)
}
//----------------EVENT HANDLER ------------//
export function radioRating_change(event, $w) {
$w('#rateError').hide();
}
Current Repeater/product page code:
import wixData from ‘wix-data’;
import wixWindow from ‘wix-window’ ;
//-------global variables-----//
let product;
//PAGE SET UP//
$w.onReady( async function (){
product = **await** $w('#productPage1').getProduct();
initReviews();
});
async function initReviews(){
await $w(‘#dataset1’).setFilter(wixData.filter().eq(‘productID’, product._id));
showReviews ();
loadStatistics();
}
//LOAD CURRENT PRODUCT STATS
async function loadStatistics() {
const stats = await wixData.get(‘reviews-stats’,product._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(‘#ratingsDisplay1’);
ratings.rating = avgRating;
ratings.numRatings = stats.count;
$w('#text120').text = '${percentRecommended} % would recommend';
$w('#ratingsDisplay1').show();
} **else** {
$w('#text120').text = 'There are no reviews yet';
}
$w('#text120').show();
}
//---------------REPEATER SET UP------------------//
export function reviewsRepeater_itemReady($w, itemData, index) {
if (itemData.recommends) {
$w(‘#recommendation’).text = ‘I recommend this product.’;
} **else** {
$w('#recommendation').text = "I don't recommend this product.";
}
if (itemData.photo){
$w(‘#reviewImage’).src = itemData.photo;
$w('#reviewImage').expand();
}
$w('#ratingsDisplay2').rating = itemData.rating;
let date = itemData._createdDate;
$w(‘#submissionTime’).text = date.toLocaleString();
}
//------------------DATA SETUP-------------------//
export function showReviews(){
if ($w(‘#dataset1’).getTotalCount() > 0) {
$w(‘#repeater1’).expand();
} **else** {
$w('#repeater1').collapse();
}
}
//------------------EVENT HANDLERS------------------//
export async function addReview_click(event,$w){
const dataForLightbox = {
productID: product._id
};
let result = await wixWindow.openLightbox(‘Review Box’, dataForLightbox);
$w('#Reviews').refresh();
loadStatistics();
$w('#thankYouMessage').show();
}