Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Do not retain values when switching between rules in the DateFilter component #7423

Merged
merged 7 commits into from
Aug 6, 2021

Conversation

aprea
Copy link
Contributor

@aprea aprea commented Jul 27, 2021

Fixes #7422

Also fixes Automattic/woocommerce-payments#1632

The DateFilter component used in the AdvancedFilters component incorrectly retains values when switching between "Before/After" and "Between" rules.

This creates a few problems (see the steps to reproduce below for the bugs / undesired behavior).

This PR changes this behavior to ensure:

  • Values are retained when switching between "Before" & "After" rules.
  • Values are reset when switching to/from the "Between" rule.

Testing instructions

Storybook

  • Run npm run storybook
  • Visit http://localhost:6007/ in your browser
  • Navigate to the AdvancedFilters story from the left-hand side panel
  • Click the "Open canvas in new tab" button in the top right
  • Add a "Before" date filter.
  • Modify the date value to the 1st of the current month.
  • Switch the rule to "Between"
  • Observe the previously set date value was reset and the between date fields are empty.
  • Change the value of the first date field to the 1st of the current month and the value of the second date field to the last day of the current month.
  • Click the filter button.
  • Observe the query params in the address bar are correctly updated to reflect this change.
  • Switch the rule back to "Before"
  • Observe the previously set date values were reset and the new "Before" date field is empty.
  • Modify the date value to the 1st of the current month.
  • Click the filter button.
  • Observe the query params in the address bar are correctly updated to reflect this change.

WCPay

In woocommerce-admin:

  • Ensure you're on the fix/7422-date-filter-retains-values branch.
  • Run npm run build
  • Run ./bin/make-zip.sh woocommerce-admin.zip
  • Copy woocommerce-admin.zip

In woocommerce-payments

To reproduce the bugs

Temporarily apply the changes in packages/components/src/advanced-filters/stories/index.js from this branch to the main branch for testing.

In Storybook:

  • Add a "Before" date filter to the AdvancedFilters component.
  • Modify the date value to the 1st of the current month.
  • Switch the rule to "Between"
  • Bug: Observe the previously set date value now appears in the second date field.
  • Change the value of the first date field to the 1st of the current month and the value of the second date field to the last day of the current month.
  • Click the filter button.
  • Observe the query params in the address bar are correctly updated to reflect this change.
  • Switch the rule back to "Before"
  • Bug: Observe the previously set date value in the second "Between" field now appears as the before date field value.
  • Click the filter button.
  • Bug: Observe the query params in the address bar now appear as follows (URL decoded)
date_before[0]=2021-07-01&date_before[1]=2021-07-27

@samueljseay
Copy link
Contributor

samueljseay commented Jul 30, 2021

@aprea Thanks for this!

A quick look at this and it looks good to me, but I'll need to test our existing usages of it just to make sure it doesn't unintentionally break anything.

I know it's a pain but could you add a changelog entry into the packages changelog? We don't have a consolidated way to handle package and non-package changelogs right now, so you can just add the entry under Unreleased in packages/components/CHANGELOG.md. And you can remove the entry you added via changelogger (sorry I know the instructions are confusing right now, we could improve this).

@samueljseay samueljseay added needs: author feedback The issue/PR needs a response from any of the parties involved in the issue. and removed status: needs review labels Jul 30, 2021
@aprea
Copy link
Contributor Author

aprea commented Jul 30, 2021

Changelog entry fixed (I think), thanks for the heads up @samueljseay

@aprea aprea added status: needs review and removed needs changelog entry needs: author feedback The issue/PR needs a response from any of the parties involved in the issue. labels Jul 30, 2021
Copy link

@kalessil kalessil left a comment

Choose a reason for hiding this comment

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

Did testing following the instructions and it works as expected.
We need approval from the maintenance team though.

@kalessil kalessil requested a review from samueljseay July 30, 2021 09:13
@kalessil
Copy link

kalessil commented Jul 30, 2021

@samueljseay : adding you as a reviewer so we are getting approval from maintainers before moving forward.

Copy link

@naman03malhotra naman03malhotra left a comment

Choose a reason for hiding this comment

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

The changes look good to me, and work as expected 🥳, lift a comment wrt to unit tests.

However, I can't reproduce this issue: Automattic/woocommerce-payments#1632 with storybook (on main branch), can you please test this with wcpay code once and see if this is fixed.

I've tested by copying the build files from node_modules of wcpay_admin to wcpay node_modules, and I can still see this issue. Maybe I am missing something? 🤔

image

ps: somewhere the setState is being called recursively. Also, one of the checks is failing.

Comment on lines +138 to +162
const { onFilterChange } = this.props;
const { rule } = this.state;

let newDateState = null;
let shouldResetValue = false;

if ( [ rule, newRule ].includes( 'between' ) ) {
newDateState = {
before: null,
beforeText: '',
beforeError: null,
after: null,
afterText: '',
afterError: null,
};

shouldResetValue = true;
}

this.setState( {
rule: newRule,
...newDateState,
} );

onFilterChange( 'rule', newRule, shouldResetValue );

Choose a reason for hiding this comment

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

Can we add a unit test for this change, something like:
should reset value/state when the rule is switched

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests added in b8a1ff9

@aprea
Copy link
Contributor Author

aprea commented Aug 2, 2021

The changes look good to me, and work as expected 🥳, lift a comment wrt to unit tests.

Woo, thanks for the review 😄

However, I can't reproduce this issue: Automattic/woocommerce-payments#1632 with storybook (on main branch), can you please test this with wcpay code once and see if this is fixed.

I've tested by copying the build files from node_modules of wcpay_admin to wcpay node_modules, and I can still see this issue. Maybe I am missing something? 🤔

I don't think this will work because in the wcpay local environment the woocommerce-admin dist files are being enqueued from the woocommerce plugin rather than the woocommerce-payments plugin.

I also tried using npm link but had no luck.

You can perform some testing with the following steps:

In woocommerce-admin:

  • Ensure you're on the fix/7422-date-filter-retains-values branch.
  • Run npm run build
  • Run ./bin/make-zip.sh woocommerce-admin.zip
  • Copy woocommerce-admin.zip

In woocommerce-payments

ps: somewhere the setState is being called recursively.

Thanks, I'll take a look

Also, one of the checks is failing.

This is because of the changelog, I'm a little confused about this process, I'll wait for one of the maintainers of this repo to tell me what I've done wrong here 😄

@joshuatf
Copy link
Contributor

joshuatf commented Aug 3, 2021

This is because of the changelog, I'm a little confused about this process, I'll wait for one of the maintainers of this repo to tell me what I've done wrong here 😄

I'll try and give this a formal review tomorrow, but no need to worry about the failing test as this has not been updated to our new flow. We've recently started using changelogger to add entries, so you can run npm run changelogger -- add to add your changelog entry (just note that this will not fix that failing test 😄 ).

Edit: We're only using changelogger for the app changelog AFAIK so your package changelog entry looks correct.

@kalessil
Copy link

kalessil commented Aug 5, 2021

Hey @joshuatf 👋, thanks for stepping into the review ! Any chance to review the PR today, so we could wrap it up this week?

@joshuatf
Copy link
Contributor

joshuatf commented Aug 5, 2021

@kalessil Sorry, my schedule has become fairly packed with release issues so I won't be able to review this week.

Copy link
Contributor

@louwie17 louwie17 left a comment

Choose a reason for hiding this comment

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

This tested well, and code overall looks good, I did leave one suggestion. This would require a potential bigger refactor, so I am fine if this happens in a separate PR and we can merge this as is (unless you feel inclined to make the changes as part of this PR).

To make sure the changelog action passes, you can add No changelog necessary as it is in the component package. The Github action looks for the no changelog in the description and passes if that is present (might have to re-run the task after you updated the description).

Thanks for updating and fixing this @aprea 🎉

// The previous value should be reset when switching to/from the "Between" rule.
expect( dateFields[ 0 ] ).not.toHaveValue();
expect( dateFields[ 1 ] ).not.toHaveValue();
} );
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice work on these tests 🎉

const newActiveFilters = [ ...this.state.activeFilters ];
newActiveFilters[ index ] = {
...newActiveFilters[ index ],
[ property ]: value,
...( shouldResetValue === true ? { value: null } : {} ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm I am not a huge fan of adding this extra parameter, the onFilterChange functions seems to only be used for updating the value, so adding a shouldResetValue for the one case where we don't update the value seems confusing.
I would be good with going to different routes for this:

  • Add a separate onRuleChange function that handles the reset value, although this is very specific (so maybe not a great option)
  • Update the onFilterChange function to pass in a updated filterValue object instead:
onFilterChange( index, updatedFilterValue ) {
  const newActiveFilters = [ ...this.state.activeFilters ];
  newActiveFilters[ index ] = {
			...newActiveFilters[ index ],
            ...updatedFilterValue
  }
 this.setState( { activeFilters: newActiveFilters } );
}

This way it is more explicit in the date-filter what you are doing ( updating the role and value )

Copy link
Contributor Author

@aprea aprea Aug 6, 2021

Choose a reason for hiding this comment

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

Hey @louwie17, thanks for the review!

Hmm I am not a huge fan of adding this extra parameter

I agree. I went with this approach as it was low touch and didn't require too much refactoring.

Update the onFilterChange function to pass in a updated filterValue object ... This way it is more explicit in the date-filter what you are doing ( updating the role and value )

This is definitely a better approach but would require refactoring the existing filter components (e.g. AttributeFilter, NumberFilter, SearchFilter, etc.)

Ultimately, this probably isn't too difficult but given this is my first PR to this repo I didn't want to get too adventurist.

This would require a potential bigger refactor, so I am fine if this happens in a separate PR and we can merge this as is (unless you feel inclined to make the changes as part of this PR).

Cool thanks. In that case, I might go ahead with the current approach and circle back with the aim of improving this in the future.

GitHub issue to refactor this with the proposed approach #7470

@louwie17 louwie17 added needs: author feedback The issue/PR needs a response from any of the parties involved in the issue. status: ready to merge and removed status: needs review labels Aug 5, 2021
@aprea aprea force-pushed the fix/7422-date-filter-retains-values branch from e4edfe7 to f9c2420 Compare August 6, 2021 05:21
@aprea aprea force-pushed the fix/7422-date-filter-retains-values branch from f9c2420 to a91888b Compare August 6, 2021 05:24
@aprea aprea merged commit 0c83dcf into main Aug 6, 2021
@aprea aprea deleted the fix/7422-date-filter-retains-values branch August 6, 2021 05:39
ObliviousHarmony pushed a commit to woocommerce/woocommerce that referenced this pull request Mar 18, 2022
…omponent (woocommerce/woocommerce-admin#7423)

* Do not retain values when switching between rules in the DateFilter component

* Add changelog entry

* Remove unnecessary use of the ternary operator

* Fix the changelog entry

* Add tests

* Add changelog entry

Co-authored-by: Chris <chris.aprea@automattic.comchris.aprea@automattic.comchris.aprea@automattic.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs: author feedback The issue/PR needs a response from any of the parties involved in the issue. tool: monorepo infrastructure @woocommerce/components
Projects
None yet
6 participants