From d10632a7f0f71c2ebd30c79894890e6cb4c515da Mon Sep 17 00:00:00 2001 From: leemyong pakvn <3759923+leemyongpakvn@users.noreply.github.com> Date: Fri, 13 Oct 2023 09:03:32 +0700 Subject: [PATCH 1/3] replace jQueryAJAX by FetchAPI --- controllers/front/ReportComment.php | 4 +- controllers/front/UpdateCommentUsefulness.php | 6 +- views/js/list-comments.js | 58 +++++++++++++------ 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/controllers/front/ReportComment.php b/controllers/front/ReportComment.php index 9be0af98..7183bb76 100644 --- a/controllers/front/ReportComment.php +++ b/controllers/front/ReportComment.php @@ -47,7 +47,9 @@ public function display() return false; } - $id_product_comment = (int) Tools::getValue('id_product_comment'); + $content = trim(file_get_contents('php://input')); + $decoded = json_decode($content, true); + $id_product_comment = (int) $decoded['id_product_comment']; /** @var EntityManagerInterface $entityManager */ $entityManager = $this->container->get('doctrine.orm.entity_manager'); diff --git a/controllers/front/UpdateCommentUsefulness.php b/controllers/front/UpdateCommentUsefulness.php index 774435b9..60fdf02d 100644 --- a/controllers/front/UpdateCommentUsefulness.php +++ b/controllers/front/UpdateCommentUsefulness.php @@ -70,8 +70,10 @@ public function display() return false; } - $id_product_comment = (int) Tools::getValue('id_product_comment'); - $usefulness = (bool) Tools::getValue('usefulness'); + $content = trim(file_get_contents('php://input')); + $decoded = json_decode($content, true); + $id_product_comment = (int) $decoded['id_product_comment']; + $usefulness = (bool) $decoded['usefulness']; /** @var EntityManagerInterface $entityManager */ $entityManager = $this->container->get('doctrine.orm.entity_manager'); diff --git a/views/js/list-comments.js b/views/js/list-comments.js index 89bc6c40..dece197d 100644 --- a/views/js/list-comments.js +++ b/views/js/list-comments.js @@ -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/json", + }, + body: JSON.stringify({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); @@ -193,9 +202,9 @@ jQuery(document).ready(function () { } else { showUpdatePostCommentErrorModal(productCommentUpdatePostErrorMessage); } - }).fail(function() { - showUpdatePostCommentErrorModal(productCommentUpdatePostErrorMessage); - }); + } catch (error) { + showUpdatePostCommentErrorModal(error); + } } function confirmCommentAbuse(commentId) { @@ -204,22 +213,35 @@ 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/json", + }, + body: JSON.stringify({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) $(pagesListId).hide(); From c72a8c52bc9e809b0354d8695f3c72dbce241899 Mon Sep 17 00:00:00 2001 From: leemyong pakvn <3759923+leemyongpakvn@users.noreply.github.com> Date: Fri, 13 Oct 2023 10:12:43 +0700 Subject: [PATCH 2/3] lint fix --- views/js/list-comments.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/views/js/list-comments.js b/views/js/list-comments.js index dece197d..ec591b59 100644 --- a/views/js/list-comments.js +++ b/views/js/list-comments.js @@ -202,7 +202,7 @@ jQuery(document).ready(function () { } else { showUpdatePostCommentErrorModal(productCommentUpdatePostErrorMessage); } - } catch (error) { + } catch (error) { showUpdatePostCommentErrorModal(error); } } @@ -241,7 +241,7 @@ jQuery(document).ready(function () { showReportCommentErrorModal(error); } } - + if (totalPages <= 1) $(pagesListId).hide(); From 1a6a64ac08a4aa91824d7366c91f4c64076f1a80 Mon Sep 17 00:00:00 2001 From: leemyong pakvn <3759923+leemyongpakvn@users.noreply.github.com> Date: Thu, 26 Oct 2023 14:29:29 +0700 Subject: [PATCH 3/3] use application/x-www-form-urlencoded to avoid changes in PHP code --- controllers/front/ReportComment.php | 4 +--- controllers/front/UpdateCommentUsefulness.php | 6 ++---- views/js/list-comments.js | 10 +++++----- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/controllers/front/ReportComment.php b/controllers/front/ReportComment.php index 7183bb76..9be0af98 100644 --- a/controllers/front/ReportComment.php +++ b/controllers/front/ReportComment.php @@ -47,9 +47,7 @@ public function display() return false; } - $content = trim(file_get_contents('php://input')); - $decoded = json_decode($content, true); - $id_product_comment = (int) $decoded['id_product_comment']; + $id_product_comment = (int) Tools::getValue('id_product_comment'); /** @var EntityManagerInterface $entityManager */ $entityManager = $this->container->get('doctrine.orm.entity_manager'); diff --git a/controllers/front/UpdateCommentUsefulness.php b/controllers/front/UpdateCommentUsefulness.php index 60fdf02d..774435b9 100644 --- a/controllers/front/UpdateCommentUsefulness.php +++ b/controllers/front/UpdateCommentUsefulness.php @@ -70,10 +70,8 @@ public function display() return false; } - $content = trim(file_get_contents('php://input')); - $decoded = json_decode($content, true); - $id_product_comment = (int) $decoded['id_product_comment']; - $usefulness = (bool) $decoded['usefulness']; + $id_product_comment = (int) Tools::getValue('id_product_comment'); + $usefulness = (bool) Tools::getValue('usefulness'); /** @var EntityManagerInterface $entityManager */ $entityManager = $this->container->get('doctrine.orm.entity_manager'); diff --git a/views/js/list-comments.js b/views/js/list-comments.js index ec591b59..06de9d5c 100644 --- a/views/js/list-comments.js +++ b/views/js/list-comments.js @@ -185,9 +185,9 @@ jQuery(document).ready(function () { const response = await fetch(updateCommentUsefulnessUrl, { method: "POST", headers: { - "Content-Type": "application/json", + "Content-Type": "application/x-www-form-urlencoded", }, - body: JSON.stringify({id_product_comment: commentId, usefulness: usefulness}), + body: "id_product_comment=" + commentId + "&usefulness=" + usefulness, }); if (response.status === 200) { @@ -222,9 +222,9 @@ jQuery(document).ready(function () { const response = await fetch(reportCommentUrl, { method: "POST", headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({id_product_comment: commentId}), + "Content-Type": "application/x-www-form-urlencoded", + }, + body: "id_product_comment=" + commentId, }); if (response.status === 200) {