Skip to content

Commit

Permalink
Merge pull request #4055 from Expensify/cmartins-preferred-locale
Browse files Browse the repository at this point in the history
Save preferred locale in NVP
  • Loading branch information
thienlnam authored Jul 21, 2021
2 parents 0c532ed + 84cdbad commit 7e395c5
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ localize the following types of data when presented to the user (even accessibil
- Phones: see [LocalPhoneNumber](https://github.com/Expensify/Expensify.cash/blob/bdfbafe18ee2d60f766c697744f23fad64b62cad/src/libs/LocalePhoneNumber.js#L51-L52)

In most cases, you will be needing to localize data used in a component, if that's the case, there's a HOC [withLocalize](https://github.com/Expensify/Expensify.cash/blob/37465dbd07da1feab8347835d82ed3d2302cde4c/src/components/withLocalize.js).
It will abstract most of the logic you need (mostly subscribe to the [PREFERRED_LOCALE](https://github.com/Expensify/Expensify.cash/blob/6cf1a56df670a11bf61aa67eeb64c1f87161dea1/src/ONYXKEYS.js#L88) Onyx key)
It will abstract most of the logic you need (mostly subscribe to the [NVP_PREFERRED_LOCALE](https://github.com/Expensify/Expensify.cash/blob/6cf1a56df670a11bf61aa67eeb64c1f87161dea1/src/ONYXKEYS.js#L88) Onyx key)
and is the preferred way of localizing things inside components.

Some pointers:
Expand Down
1 change: 1 addition & 0 deletions src/CONST.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/ONYXKEYS.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default {
},

// Indicates which locale should be used
PREFERRED_LOCALE: 'preferredLocale',
NVP_PREFERRED_LOCALE: 'preferredLocale',

// List of transactionIDs in process of rejection
TRANSACTIONS_BEING_REJECTED: 'transactionsBeingRejected',
Expand Down
2 changes: 1 addition & 1 deletion src/components/withLocalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function withLocalizeHOC(WrappedComponent) {
export default compose(
withOnyx({
preferredLocale: {
key: ONYXKEYS.PREFERRED_LOCALE,
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
},
}),
withLocalizeHOC,
Expand Down
14 changes: 14 additions & 0 deletions src/libs/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,19 @@ function PersonalDetails_Update(parameters) {
return Network.post(commandName, parameters);
}

/**
* @param {Object} parameters
* @param {Object} parameters.name
* @param {Object} parameters.value
* @returns {Promise}
*/
function PreferredLocale_Update(parameters) {
const commandName = 'PreferredLocale_Update';
requireParameters(['name', 'value'],
parameters, commandName);
return Network.post(commandName, parameters);
}

/**
* @param {Object} parameters
* @param {String} parameters.socket_id
Expand Down Expand Up @@ -1099,4 +1112,5 @@ export {
GetCurrencyList,
Policy_Create,
Policy_Employees_Remove,
PreferredLocale_Update,
};
6 changes: 5 additions & 1 deletion src/libs/Navigation/AppNavigator/AuthScreens.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,14 @@ class AuthScreens extends React.Component {
appKey: CONFIG.PUSHER.APP_KEY,
cluster: CONFIG.PUSHER.CLUSTER,
authEndpoint: `${CONFIG.EXPENSIFY.URL_API_ROOT}api?command=Push_Authenticate`,
}).then(subscribeToUserEvents);
}).then(() => {
subscribeToUserEvents();
User.subscribeToUserEvents();
});

// Fetch some data we need on initialization
NameValuePair.get(CONST.NVP.PRIORITY_MODE, ONYXKEYS.NVP_PRIORITY_MODE, 'default');
NameValuePair.get(CONST.NVP.PREFERRED_LOCALE, ONYXKEYS.NVP_PREFERRED_LOCALE, 'en');
PersonalDetails.fetchPersonalDetails();
User.getUserDetails();
User.getBetas();
Expand Down
2 changes: 1 addition & 1 deletion src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Onyx.connect({

let preferredLocale;
Onyx.connect({
key: ONYXKEYS.PREFERRED_LOCALE,
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
callback: val => preferredLocale = val || CONST.DEFAULT_LOCALE,
});

Expand Down
1 change: 1 addition & 0 deletions src/libs/Pusher/EventType.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default {
REPORT_COMMENT: 'reportComment',
REPORT_COMMENT_EDIT: 'reportCommentEdit',
REPORT_TOGGLE_PINNED: 'reportTogglePinned',
PREFERRED_LOCALE: 'preferredLocale',
};
8 changes: 5 additions & 3 deletions src/libs/actions/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Onyx from 'react-native-onyx';
import ONYXKEYS from '../../ONYXKEYS';
import * as API from '../API';

/**
* @param {String} url
Expand All @@ -9,10 +10,11 @@ function setCurrentURL(url) {
}

/**
* @param {String} locale
*/
* @param {String} locale
*/
function setLocale(locale) {
Onyx.set(ONYXKEYS.PREFERRED_LOCALE, locale);
API.PreferredLocale_Update({name: 'preferredLocale', value: locale});
Onyx.merge(ONYXKEYS.NVP_PREFERRED_LOCALE, locale);
}

export {
Expand Down
33 changes: 33 additions & 0 deletions src/libs/actions/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ import * as API from '../API';
import CONST from '../../CONST';
import Navigation from '../Navigation/Navigation';
import ROUTES from '../../ROUTES';
import * as Pusher from '../Pusher/pusher';
import Log from '../Log';
import NetworkConnection from '../NetworkConnection';

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

Expand Down Expand Up @@ -236,6 +241,33 @@ function getDomainInfo() {
});
}

/**
* Initialize our pusher subscription to listen for user changes
*/
function subscribeToUserEvents() {
// If we don't have the user's accountID yet we can't subscribe so return early
if (!currentUserAccountID) {
return;
}

const pusherChannelName = `private-user-accountID-${currentUserAccountID}`;

// Live-update an user's preferred locale
Pusher.subscribe(pusherChannelName, Pusher.TYPE.PREFERRED_LOCALE, (pushJSON) => {
Onyx.merge(ONYXKEYS.NVP_PREFERRED_LOCALE, pushJSON.preferredLocale);
}, false,
() => {
NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel');
})
.catch((error) => {
Log.info(
'[User] Failed to subscribe to Pusher channel',
true,
{error, pusherChannelName, eventName: Pusher.TYPE.PREFERRED_LOCALE},
);
});
}

export {
changePassword,
getBetas,
Expand All @@ -246,4 +278,5 @@ export {
validateLogin,
isBlockedFromConcierge,
getDomainInfo,
subscribeToUserEvents,
};
2 changes: 1 addition & 1 deletion src/libs/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ONYXKEYS from '../ONYXKEYS';

let preferredLocale = CONST.DEFAULT_LOCALE;
Onyx.connect({
key: ONYXKEYS.PREFERRED_LOCALE,
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
callback: (val) => {
if (val) {
preferredLocale = val;
Expand Down
4 changes: 2 additions & 2 deletions src/pages/settings/PreferencesPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import Text from '../../components/Text';
import NameValuePair from '../../libs/actions/NameValuePair';
import CONST from '../../CONST';
import {setExpensifyNewsStatus} from '../../libs/actions/User';
import {setLocale} from '../../libs/actions/App';
import ScreenWrapper from '../../components/ScreenWrapper';
import Switch from '../../components/Switch';
import Picker from '../../components/Picker';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import compose from '../../libs/compose';
import {setLocale} from '../../libs/actions/App';

const propTypes = {
/** The chat priority mode */
Expand Down Expand Up @@ -143,7 +143,7 @@ export default compose(
key: ONYXKEYS.USER,
},
preferredLocale: {
key: ONYXKEYS.PREFERRED_LOCALE,
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
},
}),
)(PreferencesPage);

0 comments on commit 7e395c5

Please sign in to comment.