Skip to content

Commit

Permalink
Merge pull request PrestaShop#189 from leemyongpakvn/replace_jqueryAJ…
Browse files Browse the repository at this point in the history
…AX_by_FetchAPI

Replace jQueryAJAX by FetchAPI
  • Loading branch information
nicosomb authored Nov 14, 2023
2 parents b6c6cda + 1a6a64a commit fae55dc
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions views/js/list-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,18 @@ jQuery(document).ready(function () {
commentsList.append($comment);
}

function updateCommentUsefulness($comment, commentId, usefulness) {
$.post(updateCommentUsefulnessUrl, {id_product_comment: commentId, usefulness: usefulness}, function(jsonData){
if (jsonData) {
async function updateCommentUsefulness($comment, commentId, usefulness) {
try {
const response = await fetch(updateCommentUsefulnessUrl, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: "id_product_comment=" + commentId + "&usefulness=" + usefulness,
});

if (response.status === 200) {
const jsonData = await response.json();
if (jsonData.success) {
$('.useful-review-value', $comment).html(jsonData.usefulness);
$('.not-useful-review-value', $comment).html(jsonData.total_usefulness - jsonData.usefulness);
Expand All @@ -193,9 +202,9 @@ jQuery(document).ready(function () {
} else {
showUpdatePostCommentErrorModal(productCommentUpdatePostErrorMessage);
}
}).fail(function() {
showUpdatePostCommentErrorModal(productCommentUpdatePostErrorMessage);
});
} catch (error) {
showUpdatePostCommentErrorModal(error);
}
}

function confirmCommentAbuse(commentId) {
Expand All @@ -204,20 +213,33 @@ jQuery(document).ready(function () {
if (!confirm) {
return;
}
$.post(reportCommentUrl, {id_product_comment: commentId}, function(jsonData){
if (jsonData) {
if (jsonData.success) {
reportCommentPostedModal.modal('show');
} else {
showReportCommentErrorModal(jsonData.error);
}
confirmCommentAbuseFetch(commentId);
})
}

async function confirmCommentAbuseFetch(commentId) {
try {
const response = await fetch(reportCommentUrl, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: "id_product_comment=" + commentId,
});

if (response.status === 200) {
const jsonData = await response.json();
if (jsonData.success) {
reportCommentPostedModal.modal('show');
} else {
showReportCommentErrorModal(productCommentAbuseReportErrorMessage);
showReportCommentErrorModal(jsonData.error);
}
}).fail(function() {
} else {
showReportCommentErrorModal(productCommentAbuseReportErrorMessage);
});
})
}
} catch (error) {
showReportCommentErrorModal(error);
}
}

if (totalPages <= 1)
Expand Down

0 comments on commit fae55dc

Please sign in to comment.