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

Fix/1078 shipping values flash during onboarding setup #1157

Merged

Conversation

jorgemd24
Copy link
Contributor

Changes proposed in this Pull Request:

Closes #1078

What was causing the issue?

The issue was caused because:

  1. When the Shipping Rate/Shipping Times field is updated, it triggers the API to save the values in the server.
  2. Once the values are saved and the request is finished, the store is updated.
  3. Each input (Shipping Rates & Time Rates) has a useEffect with the following format:
const [ value, setValue ] = useState( savedValue );

useEffect( () => {
	setValue( savedValue );
	}, [ savedValue ] );

//Sample of savedValue = { countries:["UK"..] , price: 1 }

It should only update the state if the savedValue is updated. I did some debugging and I see that React detects that savedValue is different every time therefore is creating extras re-renderings in both components. This causes that the input gets re-render always with the savedValue instead of the value from the state, consequently showing old data.

Proposed solution

This has been resolved updating the useEffect function with the following format:

useEffect( () => {
	setValue( savedValue );
}, [ savedValue.price ] );

useEffect( () => {
	setValue( savedValue );
}, [ savedValue.time ] );

In this way we make sure that it should only update the state with the savedValues if the price or time has been updated.

I think we should check other components that can have the same issue and could impact the performance.

With quicker connections, the same issue was happening but it was not visible for the user.

Screenshots:

Screen Capture on 2021-12-15 at 17-38-20

Detailed test instructions:

Visual Testing

  1. Go to step 2 of onboarding setup page.
  2. Select "All countries" for audience location.
  3. Move to step 3
  4. Open browser DevTool and throttle network with "Slow 3G"

Unit Tests:

  1. npm run test:js -- countriesTimeInput
  2. npm run test:js -- countriesPriceInput

@jorgemd24 jorgemd24 requested a review from a team December 15, 2021 17:34
Copy link
Member

@ecgan ecgan left a comment

Choose a reason for hiding this comment

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

Just a quick non-thorough review to add two 🚧 blocker comments below.

Comment on lines 17 to 19
useEffect( () => {
setValue( savedValue );
}, [ savedValue ] );
}, [ savedValue.time ] );
Copy link
Member

Choose a reason for hiding this comment

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

🚧 Same thing, React ESLint will complain about this: "React Hook useEffect has a missing dependency: 'savedValue'. Either include it or remove the dependency array.eslintreact-hooks/exhaustive-deps".

Copy link
Contributor

@puntope puntope Dec 16, 2021

Choose a reason for hiding this comment

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

Not judging, just curious!

Why the GH Actions linter is not failing? In theory the GH actions and the linter task should help us to handle this if it's a blocker.

Copy link
Member

Choose a reason for hiding this comment

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

There's the --quiet option in this workflow to skip the JS linter warnings. Otherwise, it would show 14 lint warnings by GH annotations in every PR. We could remove the --quiet if it's more helpful than the current.

Copy link
Contributor

@puntope puntope Dec 17, 2021

Choose a reason for hiding this comment

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

Is not there a way to promote "React Hook useEffect has a missing dependency... as error? Since it's a blocker issue. Or on the other side, add annotation somewhere in order to ignore the other warnings and remove --quiet

Copy link
Member

Choose a reason for hiding this comment

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

Is not there a way to promote "React Hook useEffect has a missing dependency... as error?

It can be done by changing 'warn' to 'error' here:

'react-hooks/exhaustive-deps': [
'warn',

@@ -0,0 +1,85 @@
/**
Copy link
Contributor

@puntope puntope Dec 16, 2021

Choose a reason for hiding this comment

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

Thanks for adding tests.

Is there a reason to have countriesPriceInput.test.js and countriesTimeInput.test.js in separated files?

All the mocks and external deps are duplicated between both files. I'd consider to use only one file tests/countriesInput.test.js and test there both.

Besides the dependencies , the tests themselves are kind of duplicated since both are using same logic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea was to keep different files to make easier the addition of other specific tests for each component. If you like I could join both tests in one file and if we need to add more tests to these components in the future, we could:

  • Continuing adding the tests in the same file
  • Or create a new file depending on the logic and component that we are going to test.

Copy link
Contributor

@puntope puntope Dec 17, 2021

Choose a reason for hiding this comment

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

Didn't check all the code yet but I can see how add-rate-modal/useGetRemainingCountryCodes and add-time-modal/useGetRemainingCountryCodes are duplicated code (only one line changes between them, and name is same actually), same goes with all the files in the modals (add & edit) and maybe more... (TBD)

This is totally out scope of this issue but feels for me that a refactor is needed here.

Since I don't wanna block the PR and this is more likely a generalised problem in the component I will open a separated issue regarding the refactor (Including this tests).

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that shippings-rates & shipping-times are components that seem to have the same logic, consequently, duplicating code and logic. Let's open the issue where we can discuss if it is needed to refactor those two components. Thanks!

@ecgan
Copy link
Member

ecgan commented Dec 20, 2021

@jorgemd24 , I tested your solution in this PR here, the solution somewhat helps the issue when users change the values slowly. If they change the values back and forth quickly over slow connections, then we would still see flashing of old values.

📹 See my demo video below with my voice annotation:

shipping-rates-values-flashing.mov

I think your solution is a good enough as it does somewhat help the issue (reduce the chances of users seeing flashing of old values). To fix the issue completely, I think we would need to work on it in #288 which would require quite a bit of time (not really a "good first issue" as indicated in #1078). I hope I can work on it soon one day.

Copy link
Member

@ecgan ecgan left a comment

Choose a reason for hiding this comment

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

Tested, the solution somewhat helps the issue #1078 but not completely, see my demo video in #1157 (comment). All tests passed. 👍

Given that it somewhat helps the issue for now, I think we are good to go with this PR. For the long term, I think we should look into working on #288.

@jorgemd24
Copy link
Contributor Author

Tested, the solution somewhat helps the issue #1078 but not completely, see my demo video in #1157 (comment). All tests passed. 👍

Given that it somewhat helps the issue for now, I think we are good to go with this PR. For the long term, I think we should look into working on #288.

Thanks, @ecgan, I agree with you that this is a temporal solution. I am sure that with the implementations from #288 we are going to tackle the root of the problem.

@jorgemd24 jorgemd24 closed this Dec 21, 2021
@jorgemd24 jorgemd24 reopened this Dec 21, 2021
@jorgemd24 jorgemd24 merged commit ba64773 into develop Dec 21, 2021
@jorgemd24 jorgemd24 deleted the fix/1078-shipping-values-flash-during-onboarding-setup branch December 21, 2021 09:55
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.

Shipping values flash during the onboarding setup
4 participants