`; } }) .catch(error => console.error('Error fetching data:', error));}// Get the product ID from the hidden inputvar productId = document.getElementById('product_id').value;// Call the function with the correct product IDfetchDataAndRenderReviews(productId); // customers reviews // Assuming productId is defined somewhere in your codevar productId = document.getElementById('product_id').value;var selectedStarRating = null;var selectedSorting = null;var currentPage = 1;var isLoading = false; // Track if reviews are currently being loaded// Assign a variable to the "Show More" buttonvar loadMoreButton = document.querySelector('.loadMoreReviews');// Add a check for the existence of the button before adding the event listenerif (loadMoreButton) { loadMoreButton.addEventListener('click', function () { // Check if reviews are currently being loaded if (!isLoading) { // Call a function to load more reviews or perform additional actions loadMoreReviews(); } });}function loadMoreReviews() { // Increment the current page number currentPage++; // Fetch and render reviews for the next page fetchReviews(productId, currentPage);} // Select the "Show Less" buttonvar showLessButton = document.querySelector('.showLessReviews');document.querySelector('.showLessReviews').addEventListener('click', function () { // Decrement the current page number currentPage--; // Fetch and render reviews for the previous page fetchReviews(productId, currentPage);}); var loaderContainer = document.querySelector('.rj-loader-container');function showLoader() { loaderContainer.style.display = 'flex';}function hideLoader() { loaderContainer.style.display = 'none';} function openImagePopup(productID, imageSource) { var popupContainer = document.getElementById('imagePopup'); var rjpopupImage = document.getElementById('rjpopupImage'); rjpopupImage.src = imageSource; popupContainer.style.display = 'block'; // You can use the productID as needed } function closeImagePopup() { var popupContainer = document.getElementById('imagePopup'); popupContainer.style.display = 'none'; } function fetchReviews(product_id, page) { showLoader(); isLoading = true; // Set loading state to true var domain = document.getElementById('RJShopDomain').value;var sortDirection = selectedSorting === 'newest' ? 'desc' : 'asc'; fetch(`https://app.reviewsjunctionapps.com/api/json-reviews?domain=${domain}&product_id=${product_id}&direction=media&&page=${page}`) .then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } return response.json(); }) .then(data => { var reviews = data.reviews.data; if (reviews.length > 0) { // Filter and sort reviews filterAndSortReviews(reviews); // Render reviews renderReviews(reviews); } if (data.reviews.next_page_url === null) { // Disable the "Show More" button when there are no more reviews loadMoreButton.disabled = true; loadMoreButton.style.cursor = 'not-allowed'; loadMoreButton.querySelector('svg').style.fill = 'gray'; } else { // Enable the "Show More" button loadMoreButton.style.cursor = 'pointer'; loadMoreButton.querySelector('svg').style.fill = 'black'; loadMoreButton.disabled = false; } // Check if there are previous pages if (data.reviews.prev_page_url === null || currentPage === 1) { // Disable the "Show Less" button when on the first page showLessButton.disabled = true; showLessButton.style.cursor = 'not-allowed'; showLessButton.querySelector('svg').style.fill = 'gray'; } else { // Enable the "Show Less" button showLessButton.disabled = false; showLessButton.style.cursor = 'pointer'; showLessButton.querySelector('svg').style.fill = 'black'; } var prevNextButtons = document.querySelector('.prevnextbuttons'); prevNextButtons.style.display = data.reviews.total > 10 ? 'flex' : 'none';if(data.reviews.total == 0){ var noreviews = document.querySelector('.no-reviews'); noreviews.style.display='block'; noreviews.style.textAlign='center';} return Promise.resolve(); // Resolve the promise to keep the chain going }) .catch(error => console.error('Error fetching data:', error)) .finally(() => { hideLoader(); isLoading = false; // Reset loading state });}function filterAndSortReviews(reviews) { // Filter reviews based on the selected star rating if (selectedStarRating) { reviews = reviews.filter(review => review.stars == selectedStarRating); } if (selectedSorting ==='media') { reviews = reviews.sort(review => review.media_files.length > 0); } // Sort reviews based on the selected sorting option if (selectedSorting === 'highest') { reviews.sort((a, b) => b.stars - a.stars); } else if (selectedSorting === 'lowest') { reviews.sort((a, b) => a.stars - b.stars); }else if (selectedSorting === 'newest') { reviews.sort((a, b) => new Date(b.created_at) - new Date(a.created_at)); } else if (selectedSorting === 'oldest') { reviews.sort((a, b) => new Date(a.created_at) - new Date(b.created_at)); }}function renderReviews(reviews) { var reviewsContainer = document.querySelector('.reviews-list-inner'); reviewsContainer.innerHTML = ''; // Clear existing reviews reviews.forEach(review => { var reviewDiv = document.createElement('div'); reviewDiv.className = 'reviews-list-item'; reviewDiv.setAttribute('data-index', reviews.indexOf(review)); // console.log('reviw data', review) // Render name and rating reviewDiv.innerHTML += `
Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.
We notice you're using an ad blocker
Without advertising income, we can't keep making this site awesome for you.