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

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

Closed
parasharrajat opened this issue Aug 9, 2021 · 24 comments
Assignees
Labels
Engineering External Added to denote the issue can be worked on by a contributor Weekly KSv2

Comments

@parasharrajat
Copy link
Member

parasharrajat commented Aug 9, 2021

If you haven’t already, check out our contributing guidelines for onboarding and email contributors@expensify.com 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

login-picker.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
    function setLocale(locale) {
    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 parasharrajat added AutoAssignerTriage Auto assign issues for triage to an available triage team member Daily KSv2 labels Aug 9, 2021
@MelvinBot
Copy link

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

@MelvinBot MelvinBot added Overdue and removed AutoAssignerTriage Auto assign issues for triage to an available triage team member labels Aug 9, 2021
@parasharrajat parasharrajat changed the title Language Picker unneccesary call the API on the Login page pausing the network queue & does not match the app theme. Language Picker on Login Page call the API unnecessarily & does not behave correctly Aug 13, 2021
@MelvinBot
Copy link

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

@parasharrajat
Copy link
Member Author

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.

@kidroca
Copy link
Contributor

kidroca commented Aug 16, 2021

I'll take a look and post back

@kidroca
Copy link
Contributor

kidroca commented Aug 16, 2021

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

Eexpensify_Locale_Picker_On_Login_Screen_Debugging.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

function setLocale(locale) {
API.PreferredLocale_Update({name: 'preferredLocale', value: locale});
Onyx.merge(ONYXKEYS.NVP_PREFERRED_LOCALE, locale);
}

2. addDefaultValuesToParameters calls redirectToSignIn

App/src/libs/API.js

Lines 51 to 60 in 8b10c1c

function addDefaultValuesToParameters(command, parameters) {
const finalParameters = {...parameters};
if (isAuthTokenRequired(command) && !parameters.authToken) {
// If we end up here with no authToken it means we are trying to make an API request before we are signed in.
// In this case, we should cancel the current request by pausing the queue and clearing the remaining requests.
if (!authToken) {
redirectToSignIn();
console.debug('A request was made without an authToken', {command, parameters});

There's no apiToken and so redirectToSignIn is called

3. redirectToSignIn clears storage and resets locale

const preferredLocale = currentPreferredLocale;
// Clearing storage discards the authToken. This causes a redirect to the SignIn screen
Onyx.clear()
.then(() => {
if (preferredLocale) {
Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale);
}

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

@parasharrajat
Copy link
Member Author

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

@kidroca
Copy link
Contributor

kidroca commented Aug 16, 2021

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

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

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
Copy link
Contributor

kidroca commented Aug 16, 2021

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);
}

@MelvinBot
Copy link

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

@MelvinBot
Copy link

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

@marcaaron marcaaron changed the title Language Picker on Login Page call the API unnecessarily & does not behave correctly [Network] Language Picker on Login Page call the API unnecessarily & does not behave correctly Aug 21, 2021
@MelvinBot MelvinBot removed the Overdue label Aug 21, 2021
@marcaaron
Copy link
Contributor

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

@marcaaron marcaaron added the External Added to denote the issue can be worked on by a contributor label Aug 21, 2021
@MelvinBot
Copy link

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

@jboniface
Copy link

job posted here

@MelvinBot MelvinBot removed the Overdue label Aug 23, 2021
@MelvinBot MelvinBot added the Help Wanted Apply this label when an issue is open to proposals by contributors label Aug 23, 2021
@MelvinBot
Copy link

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

@parasharrajat
Copy link
Member Author

What do you think @Beamanator?

@Beamanator
Copy link
Contributor

@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

@parasharrajat
Copy link
Member Author

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.
    function setLocale(locale) {
  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.

@Beamanator
Copy link
Contributor

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

@MelvinBot MelvinBot removed the Help Wanted Apply this label when an issue is open to proposals by contributors label Aug 24, 2021
@botify
Copy link

botify commented Aug 30, 2021

🚀 Deployed to staging by @Beamanator in version: 1.0.88-3 🚀

platform result
🤖 android 🤖 cancelled 🔪
🖥 desktop 🖥 cancelled 🔪
🍎 iOS 🍎 cancelled 🔪
🕸 web 🕸 success ✅

@botify
Copy link

botify commented Sep 1, 2021

🚀 Deployed to production by @roryabraham in version: 1.0.90-2 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 failure ❌
🕸 web 🕸 success ✅

@jboniface
Copy link

it looks like this should have been closed, right? or is there something still pending on this issue?

@MelvinBot MelvinBot removed the Overdue label Sep 9, 2021
@parasharrajat
Copy link
Member Author

parasharrajat commented Sep 9, 2021

@jboniface
Payment 🤑 .


Lol, what! I was never hired for this job. 🦀

@jboniface
Copy link

oh whoops, accept it and I'll pay you. I was expecting the new process where we show the "hold for payment" message in the title but this must have deployed before that

@jboniface
Copy link

jboniface commented Sep 9, 2021

ok i paid it (note: i put it up for 500 because you both reported and solved it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Engineering External Added to denote the issue can be worked on by a contributor Weekly KSv2
Projects
None yet
Development

No branches or pull requests

8 participants