[Network] Language Picker on Login Page call the API unnecessarily & does not behave correctly
If you havenβt already, check out our contributing guidelines for onboarding and email [email protected] to request to join our Slack channel!
ISSUES
- Language Picker calls the API which is unnecessary and It blocks the network queue.
- Its theme does not match other inputs in the app. No border, etc.
- We have to select the two times to make language change take effect.

- We have to select the language two times to switch it.
Action Performed:
- Open the login page & sign in with some account.
- Sign out of the app.
- Switch a language.
- Check Console logs for seeing the blocked network queue.
Expected Result:
- No API calls should be made instead language should be set on Onyx only.
- The picker theme should match with the theme of other Pickers.
- It should work in the first change.
Actual Result:
- It blocks the queue and makes an unnecessary backend request.
- Styling does not match the app theme.
- 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:
Proposal
- Correct the Dropdown theme so that it shows the outline when opened.
- 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. - Or, based on the suggestion from Peter, we can simply check for the
currentUserAccountIDbefore making the API call as this is only present when the user is logined.
Triggered auto assignment to @cdraeger11 (AutoAssignerTriage), see https://stackoverflow.com/c/expensify/questions/4749 for more details.
@cdraeger11 Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!
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.
I'll take a look and post back
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.clearbeing made - clue: 2 At 1:20 a
Onyx.setcall that sets theeslocale again is observed - it originates fromredirectToSignIn
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
Oh. Great. Network requests will definitely be removed. Good to know that it fixes the issue.
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);
}
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);
}
@cdraeger11 Still overdue 6 days?! Let's take care of this!
@cdraeger11 8 days overdue is a lot. Should this be a Weekly issue? If so, feel free to change it!
Seems like there are some clear ideas here. Gonna move this one along.
Triggered auto assignment to @jboniface (External), see https://stackoverflow.com/c/expensify/questions/8582 for more details.
job posted here
Triggered auto assignment to @Beamanator (Exported), see https://stackoverflow.com/c/expensify/questions/7972 for more details.
What do you think @Beamanator?
@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
Sure.
- Correct the Dropdown theme so that it matches other dropdowns in the app.
- Block the API call when Language is selected. based on the suggestion we can just put a check in the setLocale for
currentUserAccountIDwhere If this isundefineddon't make the API call. https://github.com/Expensify/App/blob/8b10c1c1a0267eb66ef8fdc6e0049f9b101836a2/src/libs/actions/App.js#L38 - 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.
Thanks for confirming and re-posing @parasharrajat - The job is yours! Please submit a PR when you have a chance π
π Deployed to staging by @Beamanator in version: 1.0.88-3 π
| platform | result |
|---|---|
| π€ android π€ | cancelled πͺ |
| π₯ desktop π₯ | cancelled πͺ |
| π iOS π | cancelled πͺ |
| πΈ web πΈ | success β |
π Deployed to production by @roryabraham in version: 1.0.90-2 π
| platform | result |
|---|---|
| π€ android π€ | success β |
| π₯ desktop π₯ | success β |
| π iOS π | failure β |
| πΈ web πΈ | success β |