-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Added Permission check before setting Preferred Language #5943
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
Changes from all commits
e7bf8f7
9990cc2
783182d
d8d4417
8f9007a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import Onyx from 'react-native-onyx'; | ||
| import SignoutManager from '../SignoutManager'; | ||
| import ONYXKEYS from '../../ONYXKEYS'; | ||
| import Permissions from '../Permissions'; | ||
|
|
||
| let currentActiveClients; | ||
| Onyx.connect({ | ||
|
|
@@ -26,7 +27,8 @@ function clearStorageAndRedirect(errorMessage) { | |
| // Clearing storage discards the authToken. This causes a redirect to the SignIn screen | ||
| Onyx.clear() | ||
| .then(() => { | ||
| if (preferredLocale) { | ||
| // Betas are only present when user is Logined | ||
| if (Permissions.canUseInternationalization([]) && preferredLocale) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to review early. I wanted to have feedback on this piece of the code. In reality, we don't have beta access when the user is logged out but I used this check instead of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah hmm not sure how
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I have selected spanish locale and log out (or get logged out), then I want to see the login screen in spanish, not english. In any case it's fine to only check for the betas in the UI, if a user not on the beta sets the locale manually via developer console or something, then it's ok if the localization feature works for them,
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can check for isDevelopment if we want, but I don't think it's necessary
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's needed. Otherwise, the user will be locked in one language. If we are not showing a LocalePicker on the login page in staging then the language should not change to Spanish at all after logout, if the logged-in user was using spanish. We determine whether the locale picker is shown or not, is via beta permissions check so I have used the same here.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this check is confusing. Calling
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. It can be. I will test this way. |
||
| Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale); | ||
| } | ||
| if (activeClients && activeClients.length > 0) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,9 @@ import Log from '../Log'; | |
| import NetworkConnection from '../NetworkConnection'; | ||
| import NameValuePair from './NameValuePair'; | ||
| import getSkinToneEmojiFromIndex from '../../pages/home/report/EmojiPickerMenu/getSkinToneEmojiFromIndex'; | ||
| import Permissions from '../Permissions'; | ||
| import {setLocale} from './App'; | ||
|
|
||
|
|
||
| let sessionAuthToken = ''; | ||
| let sessionEmail = ''; | ||
|
|
@@ -58,9 +61,35 @@ function changePassword(oldPassword, password) { | |
| } | ||
|
|
||
| function getBetas() { | ||
| API.User_GetBetas().then((response) => { | ||
| return API.User_GetBetas().then((response) => { | ||
| if (response.jsonCode === 200) { | ||
| Onyx.set(ONYXKEYS.BETAS, response.betas); | ||
| return response.betas; | ||
| } | ||
| return []; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Fetches betas and User's preferred Locale, then set the app locale. | ||
| * @param {String} currentPreferredLocale | ||
| */ | ||
| function fetchBetasAndSetPreferredLocale(currentPreferredLocale) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm having trouble figuring out why we are combining these. It's not obvious from looking at the code so I don't think it's the right solution to the problem. If I'm understanding correctly, there is some race condition where we want to "set preferred locale" but only if we are on the beta and the betas have not loaded yet...? This seems very easy to fix at the API layer by just returning the preferred locale if the user is on the beta and do the check in the server. So basically have something like API.User_GetPreferredLocale()
.then(({preferredLocale}) => {
Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale);
});
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another POV is that we should never prevent getting or setting this NVP depending on the beta. |
||
| Promise.all([ | ||
| getBetas(), | ||
| API.Get({ | ||
| returnValueList: 'nameValuePairs', | ||
| nvpNames: ONYXKEYS.NVP_PREFERRED_LOCALE, | ||
| }), | ||
| ]).then((betas, response) => { | ||
| const preferredLocale = lodashGet(response, ['nameValuePairs', 'preferredLocale'], CONST.DEFAULT_LOCALE); | ||
| if (currentPreferredLocale !== CONST.DEFAULT_LOCALE | ||
| && preferredLocale !== currentPreferredLocale | ||
| && Permissions.canUseInternationalization(betas) | ||
| ) { | ||
| setLocale(currentPreferredLocale); | ||
| } else { | ||
| Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the difference between |
||
| } | ||
| }); | ||
| } | ||
|
|
@@ -312,4 +341,5 @@ export { | |
| setPreferredSkinTone, | ||
| setShouldUseSecureStaging, | ||
| clearUserErrorMessage, | ||
| fetchBetasAndSetPreferredLocale, | ||
| }; | ||
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.
I don't get why we are setting the preferredLocale here, the user is logging in, their locale can't change in this flow.
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.
Could you please clarify this for me? I didn't get it.
I think, if the user does not have access to betas then we don't want the language to be changed from En as the locale is a beta feature.
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.
I don't get why we need to set a locale here nor where the value from the locale will come. The only place where the user can set the locale is from the preferences page, no?
The way I see it, regardless of the beta, the locale should be what's in preferred locale or english if nothing is set in preferred locale.
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.
Here, the locale is picked from Onyx if the user has set a locale from the login page picker.

I am following the existing code. Just added a check for beta so that we can break the loop if the user does not have localization beta access.
Yup, it does but with one exception that it also picks the locale preference from the login page.
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.
If we kill the login page language selection , would that mean we can remove this?
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.
Then we will have to remove the extra code.