-
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
Merged
nicosomb
merged 3 commits into
PrestaShop:dev
from
leemyongpakvn:replace_jqueryAJAX_by_FetchAPI
Nov 14, 2023
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
byfile_get_contents('php://input')
in ReportComment.php accordingly, because Fetch Post is sent asapplication/json
notapplication/x-www-form-urlencoded
normultipart/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 usefetch
to perform a regular POST request with form vars instead of JSON body, it will simplify both the JS and PHP codeThere 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 sideRequests 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 doWe 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.