Skip to content

Add plugin check ajax check request#91

Merged
felixarntz merged 22 commits into
trunkfrom
feature/add-ajax-check-trigger
Mar 13, 2023
Merged

Add plugin check ajax check request#91
felixarntz merged 22 commits into
trunkfrom
feature/add-ajax-check-trigger

Conversation

@vishalkakadiya
Copy link
Copy Markdown

Add plugin check ajax check request when user clicks on Check it! button.

Closes #34

Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment thread includes/Admin/Admin_Page.php Outdated
Comment thread templates/admin-page.php Outdated
Comment thread templates/admin-page.php Outdated
@vishalkakadiya
Copy link
Copy Markdown
Author

vishalkakadiya commented Feb 28, 2023

@joemcgill I have addressed your feedback on this PR and left one comment. You can review it now, thanks!

Copy link
Copy Markdown
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates 👍🏻

Copy link
Copy Markdown
Contributor

@jjgrainger jjgrainger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vishalkakadiya looks good, just left some minor changes to the action/method name based on discussions in #84

Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment thread assets/js/plugin-check-admin.js Outdated
Comment thread tests/Admin/Admin_AJAX_Tests.php Outdated
@jjgrainger
Copy link
Copy Markdown
Contributor

Hi @felixarntz I've made some minor changes to the method/action names. Please can you review and merge if approved. Thanks!

Copy link
Copy Markdown
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jjgrainger @vishalkakadiya Left some feedback here. Overall this looks solid, but there are a few things to clean up.

Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment thread includes/Admin/Admin_Page.php Outdated
Comment thread includes/Admin/Admin_Page.php Outdated
Comment thread templates/admin-page.php Outdated
Comment thread templates/admin-page.php Outdated
Comment thread assets/js/plugin-check-admin.js Outdated
Comment thread assets/js/plugin-check-admin.js Outdated
}
)
.catch(
( error ) => { console.log( error ); }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment thread tests/Admin/Admin_Page_Tests.php
@jjgrainger
Copy link
Copy Markdown
Contributor

Thanks @felixarntz I've addressed the changes you've requested. This is ready for another review.

@jjgrainger jjgrainger requested a review from felixarntz March 6, 2023 14:00
Copy link
Copy Markdown
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Comment thread templates/admin-page.php Outdated
Comment thread templates/admin-page.php Outdated
Comment thread templates/admin-page.php Outdated
Comment thread includes/Admin/Admin_AJAX.php Outdated
Comment thread assets/js/plugin-check-admin.js Outdated
Comment thread assets/js/plugin-check-admin.js
@jjgrainger jjgrainger requested a review from felixarntz March 7, 2023 15:04
Copy link
Copy Markdown
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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:

  1. Request successful and data.data.message is present.
  2. Request seemingly successful and data.data.message is not present (least likely to ever happen in reality).
  3. Request error and data.data[ 0 ].message is present (which means our AJAX endpoint returned an error).
  4. Request error and data.data[ 0 ].message is not present (which means something out of our control failed, e.g. the user is offline).

Comment thread assets/js/plugin-check-admin.js Outdated
Comment on lines +40 to +44
if ( ! data.success && ( ! data.data || ! data.data.message ) ) {
throw new Error( 'Response contains no data' );
}

console.log( data.data.message );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @felixarntz

This final console log here is the final success message and all previous errors are handled. Going over the scenarios here:

  1. Request successful and data.data.message is present - This is this final console log.
  2. Request seemingly successful and data.data.message is 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 if success is true but the message is not set. I can update this.
  3. Request error and data.data[ 0 ].message is present (which means our AJAX endpoint returned an error) - This is now captured earlier in the response when checking response.ok. This will be set to false for any error status including the 403 we return.
  4. Request error and data.data[ 0 ].message is 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @felixarntz I've updated the code based on your feedback with comments to highlight the different scenarios/errors that are captured here.

@jjgrainger jjgrainger requested a review from felixarntz March 8, 2023 17:28
Copy link
Copy Markdown
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

Comment thread templates/admin-page.php Outdated
Comment thread includes/Admin/Admin_AJAX.php Outdated
@jjgrainger
Copy link
Copy Markdown
Contributor

@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.

Copy link
Copy Markdown
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

Comment thread assets/js/plugin-check-admin.js Outdated
Comment on lines +31 to +33
if ( ! response.ok ) {
throw new Error( `${response.status}: ${response.statusText}` );
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if ( ! data.success ) {
if ( ! data ) {
throw new Error( 'Response is empty or does not contain valid JSON' );
}
if ( ! data.success ) {

Comment thread templates/admin-page.php Outdated
</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" />
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
<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" />

Comment thread includes/Admin/Admin_Page.php Outdated
'plugin-check-admin',
'const PLUGIN_CHECK = ' . json_encode(
array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but I think sending this value is unnecessary since ajaxurl is already set as a global in the admin.

@jjgrainger jjgrainger requested a review from felixarntz March 13, 2023 17:12
@jjgrainger
Copy link
Copy Markdown
Contributor

Thanks @felixarntz I've addressed the feedback here and this is ready for review

Copy link
Copy Markdown
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jjgrainger, LGTM!

@felixarntz felixarntz merged commit 08df399 into trunk Mar 13, 2023
@felixarntz felixarntz deleted the feature/add-ajax-check-trigger branch March 13, 2023 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create AJAX function and triggers and handling of progress indicator for the user

5 participants