-
Notifications
You must be signed in to change notification settings - Fork 144
Do not retain values when switching between rules in the DateFilter component #7423
Conversation
@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 |
Changelog entry fixed (I think), thanks for the heads up @samueljseay |
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.
Did testing following the instructions and it works as expected.
We need approval from the maintenance team though.
@samueljseay : adding you as a reviewer so we are getting approval from maintainers before moving forward. |
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.
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? 🤔
ps: somewhere the setState is being called recursively. Also, one of the checks is failing.
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 ); |
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.
Can we add a unit test for this change, something like:
should reset value/state when the rule is switched
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.
Tests added in b8a1ff9
Woo, thanks for the review 😄
I don't think this will work because in the wcpay local environment the I also tried using You can perform some testing with the following steps: In
In
Thanks, I'll take a look
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 Edit: We're only using changelogger for the app changelog AFAIK so your package changelog entry looks correct. |
Hey @joshuatf 👋, thanks for stepping into the review ! Any chance to review the PR today, so we could wrap it up this week? |
@kalessil Sorry, my schedule has become fairly packed with release issues so I won't be able to review this week. |
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.
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(); | ||
} ); |
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.
Nice work on these tests 🎉
const newActiveFilters = [ ...this.state.activeFilters ]; | ||
newActiveFilters[ index ] = { | ||
...newActiveFilters[ index ], | ||
[ property ]: value, | ||
...( shouldResetValue === true ? { value: null } : {} ), |
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.
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 )
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.
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
e4edfe7
to
f9c2420
Compare
f9c2420
to
a91888b
Compare
…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>
Fixes #7422
Also fixes Automattic/woocommerce-payments#1632
The
DateFilter
component used in theAdvancedFilters
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:
Testing instructions
Storybook
npm run storybook
AdvancedFilters
story from the left-hand side panelWCPay
In
woocommerce-admin
:fix/7422-date-filter-retains-values
branch.npm run build
./bin/make-zip.sh woocommerce-admin.zip
woocommerce-admin.zip
In
woocommerce-payments
woocommerce-admin.zip
into thedocker/wordpress/wp-content/plugins
directory/wp-admin/plugins.php
and activate the WooCommerce Admin plugin./wp-admin/admin.php?page=wc-admin&path=%2Fpayments%2Ftransactions
and attempt the steps to reproduce as detailed in [GlobalStep] User navigates to the Blank screen upon filtering transactions. Automattic/woocommerce-payments#1632To reproduce the bugs
Temporarily apply the changes in
packages/components/src/advanced-filters/stories/index.js
from this branch to themain
branch for testing.In Storybook:
AdvancedFilters
component.