App icon indicating copy to clipboard operation
App copied to clipboard

[Network] Language Picker on Login Page call the API unnecessarily & does not behave correctly

Open parasharrajat opened this issue 5 years ago β€’ 24 comments

If you haven’t already, check out our contributing guidelines for onboarding and email [email protected] to request to join our Slack channel!


ISSUES

  1. Language Picker calls the API which is unnecessary and It blocks the network queue.
  2. Its theme does not match other inputs in the app. No border, etc.
  3. We have to select the two times to make language change take effect.

image

  1. We have to select the language two times to switch it.

Action Performed:

  1. Open the login page & sign in with some account.
  2. Sign out of the app.
  3. Switch a language.
  4. Check Console logs for seeing the blocked network queue.

Expected Result:

  1. No API calls should be made instead language should be set on Onyx only.
  2. The picker theme should match with the theme of other Pickers.
  3. It should work in the first change.

Actual Result:

  1. It blocks the queue and makes an unnecessary backend request.
  2. Styling does not match the app theme.
  3. We have to select the language two times to activate it.

Workaround:

None. UI issue

Platform:

Where is this issue occurring?

  • Web
  • iOS
  • Android
  • Desktop App
  • Mobile Web

Version Number: 1.0.85.0 Logs: https://stackoverflow.com/c/expensify/questions/4856 Notes/Photos/Videos: Any additional supporting documentation

https://user-images.githubusercontent.com/24370807/128774998-c097efea-5ce1-42eb-8473-00994ccc565d.mp4

Expensify/Expensify Issue URL:

View all open jobs on Upwork

view this job on Upwork

Proposal

  1. Correct the Dropdown theme so that it shows the outline when opened.
  2. Block the API call when Language is selected. To do this we can add a prop offline to the LocalPicker which can then pass to https://github.com/Expensify/App/blob/8b10c1c1a0267eb66ef8fdc6e0049f9b101836a2/src/libs/actions/App.js#L38 to block the API call when offline is true.
  3. Or, based on the suggestion from Peter, we can simply check for the currentUserAccountID before making the API call as this is only present when the user is logined.

parasharrajat avatar Aug 09 '21 21:08 parasharrajat

Triggered auto assignment to @cdraeger11 (AutoAssignerTriage), see https://stackoverflow.com/c/expensify/questions/4749 for more details.

MelvinBot avatar Aug 09 '21 21:08 MelvinBot

@cdraeger11 Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!

MelvinBot avatar Aug 13 '21 19:08 MelvinBot

We have to select the two times to make language change take effect

This seems another issue with Onyx. ONYXKEYS.NVP_PREFERRED_LOCALE is being reset to en from es for the first attempt after logout.

I tested the withLocalize HOC that if it's getting correct preferredLocale but its not. LocalePicker only run the setLocale one time with es next time it is skipped due to the locale !== preferredLocale. But we get en from the Onyx as preferredLocale. I don't know much of Onyx so asking help from cc: @kidroca. may be its happening to other keys as well.

parasharrajat avatar Aug 14 '21 22:08 parasharrajat

I'll take a look and post back

kidroca avatar Aug 16 '21 07:08 kidroca

I've found the problem while debugging

It can be traced to the 20th second of this video where I follow the steps to reproduce with the deubgger

https://user-images.githubusercontent.com/12156624/129549608-fd7c5642-b873-4b32-a2ea-f4cad4bcc60f.mp4

  • clue: 1 Between the 20 and 25 seconds you can see a call to Onyx.clear being made
  • clue: 2 At 1:20 a Onyx.set call that sets the es locale again is observed - it originates from redirectToSignIn

Short version: A network request to save the locale should not be made, because there is no logged in user. In such cases (missing authToken) the internal logic redirects back to login, which resets storage and applies some defaults - see below


The problem comes from the network request that happens as it involves addDefaultValuesToParameters

1. Network request to set locale

https://github.com/Expensify/App/blob/8b10c1c1a0267eb66ef8fdc6e0049f9b101836a2/src/libs/actions/App.js#L38-L41

2. addDefaultValuesToParameters calls redirectToSignIn

https://github.com/Expensify/App/blob/8b10c1c1a0267eb66ef8fdc6e0049f9b101836a2/src/libs/API.js#L51-L60 There's no apiToken and so redirectToSignIn is called

3. redirectToSignIn clears storage and resets locale

https://github.com/Expensify/App/blob/8b10c1c1a0267eb66ef8fdc6e0049f9b101836a2/src/libs/actions/SignInRedirect.js#L46-L53 Here currentPreferredLocale is the locale before the update, because connections aren't notified with the new value yet When the connections are notified with the updated value this function already runs and preferredLocale is set And this later causes the bug where the locale does not get updated

kidroca avatar Aug 16 '21 10:08 kidroca

Oh. Great. Network requests will definitely be removed. Good to know that it fixes the issue.

parasharrajat avatar Aug 16 '21 11:08 parasharrajat

Regarding the proposal, you don't have to go through this "Block the API call when Language is selected. To do this we can add a prop offline to the LocalPicker which can then pass to" You don't need to pass a prop to the LocalePicker and then pass it to setLocale as parameter

setLocale already has access to the session here

https://github.com/Expensify/App/blob/8b10c1c1a0267eb66ef8fdc6e0049f9b101836a2/src/libs/actions/App.js#L13-L19

So the same information can be extracted like

let currentUserAccountID;
let isLoggedIn = false;
Onyx.connect({
    key: ONYXKEYS.SESSION,
    callback: (val) => {
        currentUserAccountID = lodashGet(val, 'accountID', '');
        isLoggedIn = Boolean(lodashGet(val, 'authToken'));
    },
});

And then used in setLocale

function setLocale(locale) {
  if (isLoggedIn) {
      API.PreferredLocale_Update({name: 'preferredLocale', value: locale});
  }

  Onyx.merge(ONYXKEYS.NVP_PREFERRED_LOCALE, locale);
}

kidroca avatar Aug 16 '21 11:08 kidroca

Or the request can be skipped simply because there would not be a currentUserAccountID

function setLocale(locale) {
  if (currentUserAccountID) {
      API.PreferredLocale_Update({name: 'preferredLocale', value: locale});
  }

  Onyx.merge(ONYXKEYS.NVP_PREFERRED_LOCALE, locale);
}

kidroca avatar Aug 16 '21 11:08 kidroca

@cdraeger11 Still overdue 6 days?! Let's take care of this!

MelvinBot avatar Aug 17 '21 19:08 MelvinBot

@cdraeger11 8 days overdue is a lot. Should this be a Weekly issue? If so, feel free to change it!

MelvinBot avatar Aug 19 '21 19:08 MelvinBot

Seems like there are some clear ideas here. Gonna move this one along.

marcaaron avatar Aug 21 '21 01:08 marcaaron

Triggered auto assignment to @jboniface (External), see https://stackoverflow.com/c/expensify/questions/8582 for more details.

MelvinBot avatar Aug 21 '21 01:08 MelvinBot

job posted here

jboniface avatar Aug 23 '21 16:08 jboniface

Triggered auto assignment to @Beamanator (Exported), see https://stackoverflow.com/c/expensify/questions/7972 for more details.

MelvinBot avatar Aug 23 '21 16:08 MelvinBot

What do you think @Beamanator?

parasharrajat avatar Aug 24 '21 16:08 parasharrajat

@parasharrajat would you mind re-posting exactly what the latest proposal is? :D Looks like it went through a few reviews so I just want to be clear what the latest plan is

Beamanator avatar Aug 24 '21 16:08 Beamanator

Sure.

  1. Correct the Dropdown theme so that it matches other dropdowns in the app.
  2. Block the API call when Language is selected. based on the suggestion we can just put a check in the setLocale for currentUserAccountID where If this is undefined don't make the API call. https://github.com/Expensify/App/blob/8b10c1c1a0267eb66ef8fdc6e0049f9b101836a2/src/libs/actions/App.js#L38
  3. Fix the language switching so that it works on the first attempt. It looks like blocking API call will fix it too. Thanks to Kidroca for help.

parasharrajat avatar Aug 24 '21 16:08 parasharrajat

Thanks for confirming and re-posing @parasharrajat - The job is yours! Please submit a PR when you have a chance πŸ‘

Beamanator avatar Aug 24 '21 17:08 Beamanator

πŸš€ Deployed to staging by @Beamanator in version: 1.0.88-3 πŸš€

platform result
πŸ€– android πŸ€– cancelled πŸ”ͺ
πŸ–₯ desktop πŸ–₯ cancelled πŸ”ͺ
🍎 iOS 🍎 cancelled πŸ”ͺ
πŸ•Έ web πŸ•Έ success βœ…

botify avatar Aug 30 '21 17:08 botify

πŸš€ Deployed to production by @roryabraham in version: 1.0.90-2 πŸš€

platform result
πŸ€– android πŸ€– success βœ…
πŸ–₯ desktop πŸ–₯ success βœ…
🍎 iOS 🍎 failure ❌
πŸ•Έ web πŸ•Έ success βœ…

botify avatar Sep 01 '21 01:09 botify