-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace jQueryAJAX by FetchAPI #189
Replace jQueryAJAX by FetchAPI #189
Conversation
controllers/front/ReportComment.php
Outdated
@@ -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')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, if you insert this code, does that men Tools::getValue()
is not capable of handling one specific usecase? Then what is this usecase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First, when replace jQuery Ajax by Fetch API in list-comment.js, we need replace Tools::getValue
by file_get_contents('php://input')
in ReportComment.php accordingly, because Fetch Post is sent as application/json
not application/x-www-form-urlencoded
nor multipart/form-data
Second, according to from-Legacy-to-Future-architecture's Homogenize the FO & BO architecture, Tools
is an ancient legacy-based component, so in this case Tools removing just double the benefit of this PR. I really replace two legacy library usage by two native functions 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the explanations.
Mmmm 🤔 so that means that every module developer who wants to use Fetch will have to do the same. So we can expect a lot of $content = trim(file_get_contents('php://input'));
that appears... until Symfony can be used on the front (because I'm quite sure Symfony Request object can handle this very well)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion this is way overkill, what's the advantage of using fetch with a JSON body content?
Replacing $.post
by fetch is a good call, but you can use fetch
to perform a regular POST request with form vars instead of JSON body, it will simplify both the JS and PHP code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just follow this manual https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch . And I think we are in a Listing context not a Form context here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not the point, request with form vars are performed on many APIs regardless of what they are requesting, or the context of the request The FormData
here is actually the JS native way to send POST parameters, nothing more, and POST variables are more convenient to use in PHP on the BO side
Requests based on JSON bodies are relevant for REST APIs for example, or in JS ecosystem because most framework already handle them natively. We are also using a JSON bdy in our new API under development, but because we are in a Symfony framework and also relying on APIPlatform which handles the parsing under the hood
But for native PHP in a legacy controller it's not really among the usual practices Most of the time simple API endpoints in PHP rely on $_POST
variables, or at least all our current legacy controllers do
We have no easy tool to get a JSON body request on legacy controller, that's why you had to "hard code" the fetching and parsing based on php://input
But I'm pretty sure the next developer who will see this won't even understand what's it used for 🤷♂️
So in my opinion, but I'm not the only one to decide, the PR should only focus on refactoring the JS side, as your PR title suggests, without impacting the server side controller So instead of sending a JSON body it should send POST variables and my suggestion simply shows how it's done in vanilla JS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright. If you are so worried, we can switch this PR to draft, and continue to discuss more in the related discussion. Wdyt @matks ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @jolelievre is thinking about module developer experience I quite approve his opinion 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Let's postpone this risky PR. I will find another way.
@AureRita This PR is approved technically by devs. So QA just need to check following How to test ? as an end-user. |
@matks , @matthieu-rolland & @jolelievre |
Thanks @leemyongpakvn |
I put back the "Waiting for QA" label 😉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @leemyongpakvn ,
I encountered an exception while testing your PR, only reproducible if I don't clear cache from PS and browser.
Steps to reproduce :
- In BO, Enable upvotes / downvotes on reviews
- In FO, log into your customer account
- Choose a product > Add a comment
- Report abuse
- In BO > Approve the abusive comment
- In FO, report abuse again
- In BO > Delete the abusive comment
- Exception is displayed
PrestaShop\Module\ProductComment\Repository\ProductCommentRepository::delete(): Argument #1 ($entity) must be of type PrestaShop\Module\ProductComment\Entity\ProductComment, null given, called in /Users/fHea/Desktop/ps800/modules/productcomments/productcomments.php on line 210
@PrestaShop/prestashop-core-developers what do you think ? Approved or not ?
@florine2623 There are 2 different problems here:
|
Hello @florine2623 the bug you have detected is not related to this Pull Request. I was able to reproduce it using PrestaShop I have submitted a bug report about it PrestaShop/PrestaShop#34512 This PR works as expected according to my tests 👍 and the problem you have encountered is not related to this PR. Do you think we can merge the PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @leemyongpakvn ,
Yes indeed, I have reproduced the issue without your PR as well ^^
It is QA ✅ !
Thanks!
(Don't forget to use a clean dev branch, Browser clear cache, BO clear cache before testing ;)