Skip to content
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

Enhancement/8687 pax date range #8701

Merged
merged 21 commits into from
May 15, 2024
Merged

Conversation

zutigrm
Copy link
Collaborator

@zutigrm zutigrm commented May 13, 2024

Summary

Addresses issue:

Relevant technical choices

PR Author Checklist

  • My code is tested and passes existing unit tests.
  • My code has an appropriate set of unit tests which all pass.
  • My code is backward-compatible with WordPress 5.2 and PHP 7.4.
  • My code follows the WordPress coding standards.
  • My code has proper inline documentation.
  • I have added a QA Brief on the issue linked above.
  • I have signed the Contributor License Agreement (see https://cla.developers.google.com/).

Do not alter or remove anything below. The following sections will be managed by moderators only.

Code Reviewer Checklist

  • Run the code.
  • Ensure the acceptance criteria are satisfied.
  • Reassess the implementation with the IB.
  • Ensure no unrelated changes are included.
  • Ensure CI checks pass.
  • Check Storybook where applicable.
  • Ensure there is a QA Brief.

Merge Reviewer Checklist

  • Ensure the PR has the correct target branch.
  • Double-check that the PR is okay to be merged.
  • Ensure the corresponding issue has a ZenHub release assigned.
  • Add a changelog message to the issue.

@zutigrm zutigrm marked this pull request as ready for review May 15, 2024 08:15
Copy link

github-actions bot commented May 15, 2024

Build files for 296e040 have been deleted.

Copy link
Collaborator

@benbowler benbowler left a comment

Choose a reason for hiding this comment

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

Hey @zutigrm a couple of comments below.

const date = formatPaxDate( '2019-10-31' );

expect( date.year ).toBe( 2019 );
expect( date.month ).toBe( 10 ); // 1-imdex based month
Copy link
Collaborator

Choose a reason for hiding this comment

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

Possible typo in the comment here.

Comment on lines 165 to 183
/* eslint-disable react-hooks/exhaustive-deps */
// Rule disabled for compiled `paxDateRange` properties, instead of using the main object
// to ensure effect runs every time dates are changed.
useEffect( () => {
if ( displayMode === 'reporting' && paxAppRef?.current ) {
const { startDate, endDate } = paxDateRange;

paxAppRef.current.getServices().adsDateRangeService.update( {
startDate: formatPaxDate( startDate ),
endDate: formatPaxDate( endDate ),
} );
}
}, [
paxAppRef?.current,
registry,
displayMode,
`${ paxDateRange?.startDate }|${ paxDateRange?.endDate }`,
] );
/* eslint-enable react-hooks/exhaustive-deps */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Have you tried destructuring paxDateRange above like so:

   const { current, startDate, endDate } = useSelect( ... ) || {}

Could be cleaner and remove the need disabling the rule, although this may not fix the issue with updating correctly with the date updates.

Copy link
Collaborator Author

@zutigrm zutigrm May 15, 2024

Choose a reason for hiding this comment

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

The idea is to source data outside the useEffect as dates are needed as dependency, and also avoid subscribing to the state from within hook. So we need to keep it there. Eslint rule is due to the not using variable directly but as parsed string.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Won't creating the string as a new object cause this to re-run on every render? Either way, removing the hook warning about exhaustive dependencies means other errors could be made here, and ignoring these hook rules can lead to subtle bugs. I'd rather another way around this. 🤔

@zutigrm
Copy link
Collaborator Author

zutigrm commented May 15, 2024

Thanks @benbowler PR updated

Copy link

github-actions bot commented May 15, 2024

Size Change: +2.6 kB (+0.18%)

Total Size: 1.44 MB

Filename Size Change
./dist/assets/css/googlesitekit-admin-css-********************.min.css 53.7 kB -40 B (-0.07%)
./dist/assets/js/34-********************.js 3.12 kB +1 B (+0.03%)
./dist/assets/js/googlesitekit-activation-********************.js 23.9 kB +39 B (+0.16%)
./dist/assets/js/googlesitekit-ad-blocking-recovery-********************.js 57.4 kB +41 B (+0.07%)
./dist/assets/js/googlesitekit-adminbar-********************.js 34.2 kB -4 B (-0.01%)
./dist/assets/js/googlesitekit-api-********************.js 10.2 kB +46 B (+0.46%)
./dist/assets/js/googlesitekit-components-gm2-********************.js 5.88 kB +3 B (+0.05%)
./dist/assets/js/googlesitekit-components-gm3-********************.js 10 kB +2 B (+0.02%)
./dist/assets/js/googlesitekit-datastore-forms-********************.js 9.13 kB +36 B (+0.4%)
./dist/assets/js/googlesitekit-datastore-site-********************.js 19.2 kB +58 B (+0.3%)
./dist/assets/js/googlesitekit-datastore-ui-********************.js 10.1 kB +37 B (+0.37%)
./dist/assets/js/googlesitekit-datastore-user-********************.js 24.8 kB -3 B (-0.01%)
./dist/assets/js/googlesitekit-entity-dashboard-********************.js 71.2 kB +25 B (+0.04%)
./dist/assets/js/googlesitekit-main-dashboard-********************.js 96.7 kB +1 kB (+1.05%)
./dist/assets/js/googlesitekit-modules-********************.js 21.7 kB +49 B (+0.23%)
./dist/assets/js/googlesitekit-modules-ads-********************.js 26.2 kB +868 B (+3.43%)
./dist/assets/js/googlesitekit-modules-adsense-********************.js 110 kB +39 B (+0.04%)
./dist/assets/js/googlesitekit-modules-analytics-4-********************.js 110 kB +46 B (+0.04%)
./dist/assets/js/googlesitekit-modules-pagespeed-insights-********************.js 22.4 kB +55 B (+0.25%)
./dist/assets/js/googlesitekit-modules-search-console-********************.js 58.1 kB +24 B (+0.04%)
./dist/assets/js/googlesitekit-modules-tagmanager-********************.js 30.1 kB +30 B (+0.1%)
./dist/assets/js/googlesitekit-settings-********************.js 59.7 kB +35 B (+0.06%)
./dist/assets/js/googlesitekit-splash-********************.js 71.8 kB +68 B (+0.09%)
./dist/assets/js/googlesitekit-user-input-********************.js 46.9 kB +52 B (+0.11%)
./dist/assets/js/googlesitekit-vendor-********************.js 317 kB -20 B (-0.01%)
./dist/assets/js/googlesitekit-widgets-********************.js 33.4 kB +44 B (+0.13%)
./dist/assets/js/googlesitekit-wp-dashboard-********************.js 59.8 kB +57 B (+0.1%)
./dist/assets/js/runtime-********************.js 1.3 kB +3 B (+0.23%)
ℹ️ View Unchanged
Filename Size
./dist/assets/css/googlesitekit-adminbar-css-********************.min.css 11.2 kB
./dist/assets/css/googlesitekit-authorize-application-css-********************.min.css 731 B
./dist/assets/css/googlesitekit-wp-dashboard-css-********************.min.css 7.48 kB
./dist/assets/js/29-********************.js 2.8 kB
./dist/assets/js/30-********************.js 2.28 kB
./dist/assets/js/31-********************.js 3.72 kB
./dist/assets/js/32-********************.js 929 B
./dist/assets/js/33-********************.js 888 B
./dist/assets/js/analytics-advanced-tracking-********************.js 778 B
./dist/assets/js/contact-form-7-********************.js 606 B
./dist/assets/js/googlesitekit-data-********************.js 2.18 kB
./dist/assets/js/googlesitekit-datastore-location-********************.js 2.09 kB
./dist/assets/js/googlesitekit-i18n-********************.js 3.92 kB
./dist/assets/js/googlesitekit-polyfills-********************.js 379 B
./dist/assets/js/mailchimp-********************.js 593 B
./dist/assets/js/optin-monster-********************.js 635 B
./dist/assets/js/popup-maker-********************.js 595 B
./dist/assets/js/woocommerce-********************.js 613 B
./dist/assets/js/wpforms-********************.js 595 B

compressed-size-action

Copy link
Collaborator

@benbowler benbowler left a comment

Choose a reason for hiding this comment

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

Thanks, approved.

Copy link
Collaborator

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

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

I don't think it's a good idea for us to be ignoring the rules of hooks, so I'm going to look at this and see if we can adjust the useEffect arguments to not require ignoring that rule.

Comment on lines 165 to 183
/* eslint-disable react-hooks/exhaustive-deps */
// Rule disabled for compiled `paxDateRange` properties, instead of using the main object
// to ensure effect runs every time dates are changed.
useEffect( () => {
if ( displayMode === 'reporting' && paxAppRef?.current ) {
const { startDate, endDate } = paxDateRange;

paxAppRef.current.getServices().adsDateRangeService.update( {
startDate: formatPaxDate( startDate ),
endDate: formatPaxDate( endDate ),
} );
}
}, [
paxAppRef?.current,
registry,
displayMode,
`${ paxDateRange?.startDate }|${ paxDateRange?.endDate }`,
] );
/* eslint-enable react-hooks/exhaustive-deps */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Won't creating the string as a new object cause this to re-run on every render? Either way, removing the hook warning about exhaustive dependencies means other errors could be made here, and ignoring these hook rules can lead to subtle bugs. I'd rather another way around this. 🤔

Comment on lines +120 to +121
// eslint-disable-next-line require-await
get: async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this actually need to be an async function? If PAX won't error, I don't see why we can't omit the async here 🤔

If we're going to leave an ESLint ignore in here though, which I'm fine with, we should add a comment explaining it.

assets/js/modules/ads/pax/services.test.js Show resolved Hide resolved
const partnerDateRange =
await services.partnerDateRangeService.get();

/* eslint-disable sitekit/acronym-case */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this rule needed here? 🤔

assets/js/modules/ads/pax/utils.js Outdated Show resolved Hide resolved
assets/js/modules/ads/pax/utils.js Outdated Show resolved Hide resolved
assets/js/modules/ads/pax/utils.test.js Outdated Show resolved Hide resolved
} );
}
}, [
paxAppRef?.current,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Using a ref as a dependency of a useEffect is an anti-pattern, because mutating the ref doesn't cause the effect run again.

Maybe we should make this paxAppRef a state value instead? It will trigger the component to render again, but I don't think that's actually an issue 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've changed this to run the date update code after the app is initialised.

It was possible to re-render when the dates changes if they were destructured, as @benbowler mentioned.

Copy link
Collaborator Author

@zutigrm zutigrm May 16, 2024

Choose a reason for hiding this comment

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

because mutating the ref doesn't cause the effect run again

I thought so, but actually it did re-trigger the hook once ref.current was set to the pax Instance, which is not re-run otherwise. As it changes value from undefined to an app instance, it is causing it to run again

@tofumatt tofumatt changed the base branch from develop to main May 15, 2024 18:29
Copy link
Collaborator

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

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

I've made some adjustments to the useEffect here so there aren't violations of the rules of hooks, as well as removing the use of a ref as a useEffect dependency (which doesn't work).

Works well now aside from the PAX app sometimes not updating its UI until there is a click/scroll event on the client, but I suspect that's a PAX app bug and not ours.

@tofumatt tofumatt merged commit ac4763c into main May 15, 2024
21 checks passed
@tofumatt tofumatt deleted the enhancement/8687-pax-date-range branch May 15, 2024 18:43
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.

None yet

3 participants