Add plugin check ajax check request#91
Conversation
|
@joemcgill I have addressed your feedback on this PR and left one comment. You can review it now, thanks! |
jjgrainger
left a comment
There was a problem hiding this comment.
@vishalkakadiya looks good, just left some minor changes to the action/method name based on discussions in #84
|
Hi @felixarntz I've made some minor changes to the method/action names. Please can you review and merge if approved. Thanks! |
felixarntz
left a comment
There was a problem hiding this comment.
@jjgrainger @vishalkakadiya Left some feedback here. Overall this looks solid, but there are a few things to clean up.
| } | ||
| ) | ||
| .catch( | ||
| ( error ) => { console.log( error ); } |
There was a problem hiding this comment.
Nit-pick: Better to use console.error.
Also, per my other comment, we should check if a response was still returned from the server. If yes, we can extract the first error message from it and display it (response.data[ 0 ].message). This needs to be optional though, as an error could also occur just because e.g. there is no network connection, in which case there won't be a response.
|
Thanks @felixarntz I've addressed the changes you've requested. This is ready for another review. |
felixarntz
left a comment
There was a problem hiding this comment.
@jjgrainger A few small follow up things, most importantly about the error handling. I'm just unsure whether the approach you have in JS works, so I want to make sure you've tested this.
felixarntz
left a comment
There was a problem hiding this comment.
@jjgrainger The error logging is still not working as expected. You now added another check to ensure the expected success data is present, but at the same time got rid of the (also needed) check that logs the error message if there is one.
To clarify, we need to cover 4 scenarios here:
- Request successful and
data.data.messageis present. - Request seemingly successful and
data.data.messageis not present (least likely to ever happen in reality). - Request error and
data.data[ 0 ].messageis present (which means our AJAX endpoint returned an error). - Request error and
data.data[ 0 ].messageis not present (which means something out of our control failed, e.g. the user is offline).
| if ( ! data.success && ( ! data.data || ! data.data.message ) ) { | ||
| throw new Error( 'Response contains no data' ); | ||
| } | ||
|
|
||
| console.log( data.data.message ); |
There was a problem hiding this comment.
Doesn't this need to use data.data[ 0 ].message? You had that here before which I thought was right, but now you got rid of the [ 0 ].
I tested this and it is indeed not working. You need to also still include a condition that covers the error case. If the 403 error from the AJAX action is returned, the data.data[ 0 ].message in the error response should be logged via console.error. That is not the case right now.
There was a problem hiding this comment.
Thanks @felixarntz
This final console log here is the final success message and all previous errors are handled. Going over the scenarios here:
- Request successful and
data.data.messageis present - This is this final console log. - Request seemingly successful and
data.data.messageis not present (least likely to ever happen in reality). - This is the error thrown above 'Response contains no data', however I can see how this doesn't catch ifsuccessis true but the message is not set. I can update this. - Request error and
data.data[ 0 ].messageis present (which means our AJAX endpoint returned an error) - This is now captured earlier in the response when checkingresponse.ok. This will be set tofalsefor any error status including the 403 we return. - Request error and
data.data[ 0 ].messageis not present (which means something out of our control failed, e.g. the user is offline). - I don't think we can handle the logic with code here, if something like a network failure happens, the response is not handled and a generic console error is logged.
For point 2, I can make the updates here to capture when success is true but data/message is undefined.
For point 3, I can include an additional check as the point in the logic to use the message we return instead of the generic status message?
Let me know if you have any questions.
There was a problem hiding this comment.
Thanks @felixarntz I've updated the code based on your feedback with comments to highlight the different scenarios/errors that are captured here.
mukeshpanchal27
left a comment
There was a problem hiding this comment.
@jjgrainger The checked Ajax request run if we don't select any plugin in dropdown can we restrict it so it will only run when the option has value?
Thanks @mukeshpanchal27 I agree with the idea here, but I think this is something to address later as we have additional functionality to build for the WP Admin and may want to tackle validation later as part of Milestone 1 Review updates. |
There was a problem hiding this comment.
@jjgrainger One last problem with the error logging still remains. I still don't see our error message logged. Did you test this? Did your browser maybe behave differently than mine?
| if ( ! response.ok ) { | ||
| throw new Error( `${response.status}: ${response.statusText}` ); | ||
| } |
There was a problem hiding this comment.
This here is what throws a wrench in things. You already error out due to the bad response code, which makes it impossible to even reach the code below where we expose the more specific JSON error that is present.
I think by removing this our error logging works correctly. Requests that come back with a bad response are automatically logged in the console anyway, so no need to separately log the status code in our logic.
| ) | ||
| .then( | ||
| ( data ) => { | ||
| if ( ! data.success ) { |
There was a problem hiding this comment.
Before checking this, we should check whether data even is an object, particularly when removing the above response.ok check. If the response is empty, then this line here would cause a JS error.
| if ( ! data.success ) { | |
| if ( ! data ) { | |
| throw new Error( 'Response is empty or does not contain valid JSON' ); | |
| } | |
| if ( ! data.success ) { |
| </select> | ||
|
|
||
| <input type="submit" value="<?php esc_attr_e( 'Check it!', 'plugin-check' ); ?>" /> | ||
| <input type="submit" value="<?php esc_attr_e( 'Check it!', 'plugin-check' ); ?>" id="plugin-check__submit" class="button" /> |
There was a problem hiding this comment.
Maybe make it a primary button, since it is the primary action on the page. We'll revise design later, so this is a nit-pick at this point, but a good practice.
| <input type="submit" value="<?php esc_attr_e( 'Check it!', 'plugin-check' ); ?>" id="plugin-check__submit" class="button" /> | |
| <input type="submit" value="<?php esc_attr_e( 'Check it!', 'plugin-check' ); ?>" id="plugin-check__submit" class="button button-primary" /> |
| 'plugin-check-admin', | ||
| 'const PLUGIN_CHECK = ' . json_encode( | ||
| array( | ||
| 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
There was a problem hiding this comment.
Nitpick, but I think sending this value is unnecessary since ajaxurl is already set as a global in the admin.
|
Thanks @felixarntz I've addressed the feedback here and this is ready for review |
Add plugin check ajax check request when user clicks on
Check it!button.Closes #34