Skip to content

Commit

Permalink
Update javascript to use json instead of html
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreRambaud committed Nov 13, 2020
1 parent 15a0e45 commit 9e01903
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 33 deletions.
18 changes: 3 additions & 15 deletions views/js/list-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ jQuery(document).ready(function () {
}

function paginateComments(page) {
$.get(commentsListUrl, {page: page}, function(result) {
const jsonResponse = JSON.parse(result);

$.get(commentsListUrl, {page: page}, function(jsonResponse) {
if (jsonResponse.comments && jsonResponse.comments.length > 0) {
populateComments(jsonResponse.comments);
if (jsonResponse.comments_nb > jsonResponse.comments_per_page) {
Expand Down Expand Up @@ -123,12 +121,7 @@ jQuery(document).ready(function () {
}

function updateCommentUsefulness($comment, commentId, usefulness) {
$.post(updateCommentUsefulnessUrl, {id_product_comment: commentId, usefulness: usefulness}, function(jsonResponse){
var jsonData = false;
try {
jsonData = JSON.parse(jsonResponse);
} catch (e) {
}
$.post(updateCommentUsefulnessUrl, {id_product_comment: commentId, usefulness: usefulness}, function(jsonData){
if (jsonData) {
if (jsonData.success) {
$('.useful-review-value', $comment).html(jsonData.usefulness);
Expand All @@ -151,12 +144,7 @@ jQuery(document).ready(function () {
if (!confirm) {
return;
}
$.post(reportCommentUrl, {id_product_comment: commentId}, function(jsonResponse){
var jsonData = false;
try {
jsonData = JSON.parse(jsonResponse);
} catch (e) {
}
$.post(reportCommentUrl, {id_product_comment: commentId}, function(jsonData){
if (jsonData) {
if (jsonData.success) {
reportCommentPostedModal.modal('show');
Expand Down
7 changes: 1 addition & 6 deletions views/js/post-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,7 @@ jQuery(document).ready(function () {
if (!validateFormData(formData)) {
return;
}
$.post($(this).attr('action'), $(this).serialize(), function(jsonResponse) {
var jsonData = false;
try {
jsonData = JSON.parse(jsonResponse);
} catch (e) {
}
$.post($(this).attr('action'), $(this).serialize(), function(jsonData) {
if (jsonData) {
if (jsonData.success) {
clearPostCommentForm();
Expand Down
16 changes: 4 additions & 12 deletions views/js/productListingComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var productListingComments = (function () {
seenIds[id] = true;
});


var IDsArray = Object.keys(seenIds);
var prevDataIDs = data.productIDs.splice(0);
data.productIDs = prevDataIDs.concat(IDsArray);
Expand All @@ -97,24 +97,17 @@ var productListingComments = (function () {
}

function loadProductsData() {
if (data.productIDs.length === 0)
if (data.productIDs.length === 0)
return;

data.commentsLoadingInProgress = true;

var dataIDsCopy = data.productIDs.slice(0);
selectedProductIDs = dataIDsCopy.splice(0, data.ajaxIDsLimit);


$.get(data.ajaxUrl, { id_products: selectedProductIDs }, function (jsonResponse) {
var jsonData = false;
try {
jsonData = JSON.parse(jsonResponse);
} catch (e) {
}

$.get(data.ajaxUrl, { id_products: selectedProductIDs }, function (jsonData) {
if (jsonData) {

$.each(jsonData.products, function(i, elem) {
var productData = elem;
var $productsReviewsContainer = $('.product-list-reviews[data-id="' + productData.id_product + '"]');
Expand All @@ -126,7 +119,7 @@ var productListingComments = (function () {
$self.find(DOMStrings.productListReviewsStarsContainer).rating({ grade: productData.average_grade, starWidth: 16 });
$self.find(DOMStrings.productListReviewsNumberOfComments).text('(' + productData.comments_nb + ')');
$self.closest(DOMStrings.productContainer).addClass(DOMClasses.hasReviews);
$self.css('visibility', 'visible');
$self.css('visibility', 'visible');
}

$self.closest(DOMStrings.productContainer).addClass(DOMClasses.reviewsLoaded);
Expand Down Expand Up @@ -156,4 +149,3 @@ var productListingComments = (function () {
}
}
})();

0 comments on commit 9e01903

Please sign in to comment.