From 5dc947df8a332d72a3dc38241ab5f9e6211c181d Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 14 Jul 2021 16:43:06 -0600 Subject: [PATCH 1/8] change preferred locale onyx key name --- README.md | 2 +- src/CONST.js | 1 + src/ONYXKEYS.js | 2 +- src/components/withLocalize.js | 2 +- src/libs/OptionsListUtils.js | 2 +- src/libs/actions/App.js | 2 +- src/libs/translate.js | 2 +- src/pages/settings/PreferencesPage.js | 5 ++--- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index da5d751538a8..aa836ae2bc7f 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/CONST.js b/src/CONST.js index 511ba361a309..3733c0fe3dde 100755 --- a/src/CONST.js +++ b/src/CONST.js @@ -213,6 +213,7 @@ const CONST = { ACH_DATA_THROTTLED: 'expensify_ACHData_throttled', FAILED_BANK_ACCOUNT_VALIDATIONS_PREFIX: 'private_failedBankValidations_', BANK_ACCOUNT_GET_THROTTLED: 'private_throttledHistory_BankAccount_Get', + PREFERRED_LOCALE: 'preferredLocale', }, DEFAULT_TIME_ZONE: {automatic: true, selected: 'America/Los_Angeles'}, DEFAULT_ACCOUNT_DATA: {error: '', success: '', loading: false}, diff --git a/src/ONYXKEYS.js b/src/ONYXKEYS.js index 5535e4b7c838..ded87710b8e1 100755 --- a/src/ONYXKEYS.js +++ b/src/ONYXKEYS.js @@ -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', diff --git a/src/components/withLocalize.js b/src/components/withLocalize.js index 28b9409796d3..78eca06f6077 100755 --- a/src/components/withLocalize.js +++ b/src/components/withLocalize.js @@ -78,7 +78,7 @@ function withLocalizeHOC(WrappedComponent) { export default compose( withOnyx({ preferredLocale: { - key: ONYXKEYS.PREFERRED_LOCALE, + key: ONYXKEYS.NVP_PREFERRED_LOCALE, }, }), withLocalizeHOC, diff --git a/src/libs/OptionsListUtils.js b/src/libs/OptionsListUtils.js index 56a2eaa54e9d..6069e0ff77e0 100644 --- a/src/libs/OptionsListUtils.js +++ b/src/libs/OptionsListUtils.js @@ -37,7 +37,7 @@ Onyx.connect({ let preferredLocale; Onyx.connect({ - key: ONYXKEYS.PREFERRED_LOCALE, + key: ONYXKEYS.NVP_PREFERRED_LOCALE, callback: val => preferredLocale = val || CONST.DEFAULT_LOCALE, }); diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 397690b7daee..6b696c7610a9 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -12,7 +12,7 @@ function setCurrentURL(url) { * @param {String} locale */ function setLocale(locale) { - Onyx.set(ONYXKEYS.PREFERRED_LOCALE, locale); + Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, locale); } export { diff --git a/src/libs/translate.js b/src/libs/translate.js index 277f407fb1a8..4716a25841e9 100644 --- a/src/libs/translate.js +++ b/src/libs/translate.js @@ -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; diff --git a/src/pages/settings/PreferencesPage.js b/src/pages/settings/PreferencesPage.js index 3fd50a75a334..008745dc1469 100755 --- a/src/pages/settings/PreferencesPage.js +++ b/src/pages/settings/PreferencesPage.js @@ -12,7 +12,6 @@ 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'; @@ -116,7 +115,7 @@ const PreferencesPage = ({ { if (locale !== preferredLocale) { - setLocale(locale); + NameValuePair.set(CONST.NVP.PREFERRED_LOCALE, locale, ONYXKEYS.NVP_PREFERRED_LOCALE) } }} items={Object.values(localesToLanguages)} @@ -143,7 +142,7 @@ export default compose( key: ONYXKEYS.USER, }, preferredLocale: { - key: ONYXKEYS.PREFERRED_LOCALE, + key: ONYXKEYS.NVP_PREFERRED_LOCALE, }, }), )(PreferencesPage); From e7a7f3f0264c8ccb934ac1a6c215c677a2ab1b7e Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 14 Jul 2021 16:57:43 -0600 Subject: [PATCH 2/8] remove setLocale --- src/libs/actions/App.js | 8 -------- src/pages/settings/PreferencesPage.js | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 6b696c7610a9..811911206fc7 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -8,14 +8,6 @@ function setCurrentURL(url) { Onyx.set(ONYXKEYS.CURRENT_URL, url); } -/** - * @param {String} locale - */ -function setLocale(locale) { - Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, locale); -} - export { setCurrentURL, - setLocale, }; diff --git a/src/pages/settings/PreferencesPage.js b/src/pages/settings/PreferencesPage.js index 008745dc1469..0fc4104988ce 100755 --- a/src/pages/settings/PreferencesPage.js +++ b/src/pages/settings/PreferencesPage.js @@ -115,7 +115,7 @@ const PreferencesPage = ({ { if (locale !== preferredLocale) { - NameValuePair.set(CONST.NVP.PREFERRED_LOCALE, locale, ONYXKEYS.NVP_PREFERRED_LOCALE) + NameValuePair.set(CONST.NVP.PREFERRED_LOCALE, locale, ONYXKEYS.NVP_PREFERRED_LOCALE); } }} items={Object.values(localesToLanguages)} From df1a166dd7593a46ecad308412a827532d252ef2 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 14 Jul 2021 17:12:48 -0600 Subject: [PATCH 3/8] fetch preferred locale --- src/libs/Navigation/AppNavigator/AuthScreens.js | 1 + src/libs/Navigation/NavigationRoot.js | 2 +- src/libs/actions/App.js | 4 +--- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index ff370c7ef42e..7f2d7b3d3648 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -149,6 +149,7 @@ class AuthScreens extends React.Component { // 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(); diff --git a/src/libs/Navigation/NavigationRoot.js b/src/libs/Navigation/NavigationRoot.js index ec8745ec6cf1..b3799579c01c 100644 --- a/src/libs/Navigation/NavigationRoot.js +++ b/src/libs/Navigation/NavigationRoot.js @@ -4,7 +4,7 @@ import {getPathFromState, NavigationContainer} from '@react-navigation/native'; import {navigationRef} from './Navigation'; import linkingConfig from './linkingConfig'; import AppNavigator from './AppNavigator'; -import {setCurrentURL} from '../actions/App'; +import setCurrentURL from '../actions/App'; import FullScreenLoadingIndicator from '../../components/FullscreenLoadingIndicator'; const propTypes = { diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 811911206fc7..afcba3a38a5b 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -8,6 +8,4 @@ function setCurrentURL(url) { Onyx.set(ONYXKEYS.CURRENT_URL, url); } -export { - setCurrentURL, -}; +export default setCurrentURL; From 8dd8b57083ca461c4247007c29c8853b5305b803 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 14 Jul 2021 17:57:39 -0600 Subject: [PATCH 4/8] fix style --- src/pages/settings/PreferencesPage.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/pages/settings/PreferencesPage.js b/src/pages/settings/PreferencesPage.js index 0fc4104988ce..747aa7854cf5 100755 --- a/src/pages/settings/PreferencesPage.js +++ b/src/pages/settings/PreferencesPage.js @@ -115,7 +115,11 @@ const PreferencesPage = ({ { if (locale !== preferredLocale) { - NameValuePair.set(CONST.NVP.PREFERRED_LOCALE, locale, ONYXKEYS.NVP_PREFERRED_LOCALE); + NameValuePair.set( + CONST.NVP.PREFERRED_LOCALE, + locale, + ONYXKEYS.NVP_PREFERRED_LOCALE, + ); } }} items={Object.values(localesToLanguages)} From 041d8aa1f0140aa749a3019e1b2f080a0da3c485 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Thu, 15 Jul 2021 18:18:01 -0600 Subject: [PATCH 5/8] add pusher --- .../Navigation/AppNavigator/AuthScreens.js | 5 ++++- src/libs/Pusher/EventType.js | 1 + src/libs/actions/PersonalDetails.js | 22 +++++++++++++++++++ src/pages/settings/PreferencesPage.js | 2 ++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index 7f2d7b3d3648..13f99881ab0f 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -145,7 +145,10 @@ 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(); + PersonalDetails.subscribeToPersonalDetails(); + }); // Fetch some data we need on initialization NameValuePair.get(CONST.NVP.PRIORITY_MODE, ONYXKEYS.NVP_PRIORITY_MODE, 'default'); diff --git a/src/libs/Pusher/EventType.js b/src/libs/Pusher/EventType.js index 0810261af524..991665bbb9f8 100644 --- a/src/libs/Pusher/EventType.js +++ b/src/libs/Pusher/EventType.js @@ -6,4 +6,5 @@ export default { REPORT_COMMENT: 'reportComment', REPORT_COMMENT_EDIT: 'reportCommentEdit', REPORT_TOGGLE_PINNED: 'reportTogglePinned', + PERSONAL_DETAILS_PREFERRED_LOCALE: 'personalDetailsPreferredLocale', }; diff --git a/src/libs/actions/PersonalDetails.js b/src/libs/actions/PersonalDetails.js index c57b81e95c04..983c72eb9f73 100644 --- a/src/libs/actions/PersonalDetails.js +++ b/src/libs/actions/PersonalDetails.js @@ -10,6 +10,8 @@ import * as API from '../API'; import NameValuePair from './NameValuePair'; import {isDefaultRoom} from '../reportUtils'; import {getReportIcons, getDefaultAvatar} from '../OptionsListUtils'; +import * as Pusher from '../Pusher/pusher'; +import Log from '../Log'; let currentUserEmail = ''; Onyx.connect({ @@ -290,6 +292,24 @@ function deleteAvatar(login) { mergeLocalPersonalDetails({avatar: getDefaultAvatar(login)}); } +function subscribeToPersonalDetails() { + const pusherChannelName = 'private-user-accountID-2'; + Pusher.subscribe(pusherChannelName, Pusher.TYPE.PERSONAL_DETAILS_PREFERRED_LOCALE, () => { + console.log('over here'); + }) + .catch((error) => { + Log.info( + '[Personal Details] Failed to subscribe to Pusher channel', + true, + {error, pusherChannelName, eventName: Pusher.TYPE.PERSONAL_DETAILS_PREFERRED_LOCALE}, + ); + }); +} + +function broadcastPreferredLocaleChange(preferredLocale) { + Pusher.sendEvent('private-user-accountID-2', Pusher.TYPE.PERSONAL_DETAILS_PREFERRED_LOCALE, {preferredLocale}); +} + // When the app reconnects from being offline, fetch all of the personal details NetworkConnection.onReconnect(fetchPersonalDetails); @@ -304,4 +324,6 @@ export { deleteAvatar, fetchCurrencyPreferences, getCurrencyList, + subscribeToPersonalDetails, + broadcastPreferredLocaleChange, }; diff --git a/src/pages/settings/PreferencesPage.js b/src/pages/settings/PreferencesPage.js index 747aa7854cf5..4770ffcac508 100755 --- a/src/pages/settings/PreferencesPage.js +++ b/src/pages/settings/PreferencesPage.js @@ -17,6 +17,7 @@ import Switch from '../../components/Switch'; import Picker from '../../components/Picker'; import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize'; import compose from '../../libs/compose'; +import {broadcastPreferredLocaleChange} from '../../libs/actions/PersonalDetails'; const propTypes = { /** The chat priority mode */ @@ -120,6 +121,7 @@ const PreferencesPage = ({ locale, ONYXKEYS.NVP_PREFERRED_LOCALE, ); + broadcastPreferredLocaleChange(locale); } }} items={Object.values(localesToLanguages)} From ddda46fbb14ec653281366e0d2a8af40364a4c83 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 16 Jul 2021 15:48:28 -0600 Subject: [PATCH 6/8] add pusher --- src/libs/API.js | 13 +++++++++++++ src/libs/actions/PersonalDetails.js | 16 +++++++++++----- src/pages/settings/PreferencesPage.js | 9 ++------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/libs/API.js b/src/libs/API.js index 26aaf1439eb1..633e1cbac814 100644 --- a/src/libs/API.js +++ b/src/libs/API.js @@ -538,6 +538,18 @@ function PersonalDetails_Update(parameters) { return Network.post(commandName, parameters); } +/** + * @param {Object} parameters + * @param {Object} parameters.preferredLocale + * @returns {Promise} + */ +function PreferredLocale_Update(parameters) { + const commandName = 'PreferredLocale_Update'; + requireParameters(['preferredLocale'], + parameters, commandName); + return Network.post(commandName, parameters); +} + /** * @param {Object} parameters * @param {String} parameters.socket_id @@ -1099,4 +1111,5 @@ export { GetCurrencyList, Policy_Create, Policy_Employees_Remove, + PreferredLocale_Update, }; diff --git a/src/libs/actions/PersonalDetails.js b/src/libs/actions/PersonalDetails.js index 983c72eb9f73..1d0e9a0d8aee 100644 --- a/src/libs/actions/PersonalDetails.js +++ b/src/libs/actions/PersonalDetails.js @@ -208,6 +208,16 @@ function mergeLocalPersonalDetails(details) { Onyx.merge(ONYXKEYS.PERSONAL_DETAILS, {[currentUserEmail]: mergedDetails}); } +/** + * Sets the personal details object for the current user + * + * @param {String} preferredLocale + */ +function updatePreferredLocale(preferredLocale) { + API.PreferredLocale_Update({preferredLocale}); + Onyx.merge(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale); +} + /** * Sets the personal details object for the current user * @@ -306,10 +316,6 @@ function subscribeToPersonalDetails() { }); } -function broadcastPreferredLocaleChange(preferredLocale) { - Pusher.sendEvent('private-user-accountID-2', Pusher.TYPE.PERSONAL_DETAILS_PREFERRED_LOCALE, {preferredLocale}); -} - // When the app reconnects from being offline, fetch all of the personal details NetworkConnection.onReconnect(fetchPersonalDetails); @@ -325,5 +331,5 @@ export { fetchCurrencyPreferences, getCurrencyList, subscribeToPersonalDetails, - broadcastPreferredLocaleChange, + updatePreferredLocale, }; diff --git a/src/pages/settings/PreferencesPage.js b/src/pages/settings/PreferencesPage.js index 4770ffcac508..49af9cd49c53 100755 --- a/src/pages/settings/PreferencesPage.js +++ b/src/pages/settings/PreferencesPage.js @@ -17,7 +17,7 @@ import Switch from '../../components/Switch'; import Picker from '../../components/Picker'; import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize'; import compose from '../../libs/compose'; -import {broadcastPreferredLocaleChange} from '../../libs/actions/PersonalDetails'; +import {updatePreferredLocale} from '../../libs/actions/PersonalDetails'; const propTypes = { /** The chat priority mode */ @@ -116,12 +116,7 @@ const PreferencesPage = ({ { if (locale !== preferredLocale) { - NameValuePair.set( - CONST.NVP.PREFERRED_LOCALE, - locale, - ONYXKEYS.NVP_PREFERRED_LOCALE, - ); - broadcastPreferredLocaleChange(locale); + updatePreferredLocale(locale); } }} items={Object.values(localesToLanguages)} From 2df99f85a964c19536d410c427d3320d226d5be5 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 19 Jul 2021 15:51:51 -0600 Subject: [PATCH 7/8] move functions --- src/libs/API.js | 5 +-- .../Navigation/AppNavigator/AuthScreens.js | 2 +- src/libs/Navigation/NavigationRoot.js | 2 +- src/libs/Pusher/EventType.js | 2 +- src/libs/actions/App.js | 14 +++++++- src/libs/actions/PersonalDetails.js | 27 --------------- src/libs/actions/User.js | 33 +++++++++++++++++++ src/pages/settings/PreferencesPage.js | 4 +-- 8 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/libs/API.js b/src/libs/API.js index 633e1cbac814..1401a28be6d9 100644 --- a/src/libs/API.js +++ b/src/libs/API.js @@ -540,12 +540,13 @@ function PersonalDetails_Update(parameters) { /** * @param {Object} parameters - * @param {Object} parameters.preferredLocale + * @param {Object} parameters.name + * @param {Object} parameters.value * @returns {Promise} */ function PreferredLocale_Update(parameters) { const commandName = 'PreferredLocale_Update'; - requireParameters(['preferredLocale'], + requireParameters(['name', 'value'], parameters, commandName); return Network.post(commandName, parameters); } diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index 13f99881ab0f..29dc0e475224 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -147,7 +147,7 @@ class AuthScreens extends React.Component { authEndpoint: `${CONFIG.EXPENSIFY.URL_API_ROOT}api?command=Push_Authenticate`, }).then(() => { subscribeToUserEvents(); - PersonalDetails.subscribeToPersonalDetails(); + User.subscribeToUserEvents(); }); // Fetch some data we need on initialization diff --git a/src/libs/Navigation/NavigationRoot.js b/src/libs/Navigation/NavigationRoot.js index b3799579c01c..ec8745ec6cf1 100644 --- a/src/libs/Navigation/NavigationRoot.js +++ b/src/libs/Navigation/NavigationRoot.js @@ -4,7 +4,7 @@ import {getPathFromState, NavigationContainer} from '@react-navigation/native'; import {navigationRef} from './Navigation'; import linkingConfig from './linkingConfig'; import AppNavigator from './AppNavigator'; -import setCurrentURL from '../actions/App'; +import {setCurrentURL} from '../actions/App'; import FullScreenLoadingIndicator from '../../components/FullscreenLoadingIndicator'; const propTypes = { diff --git a/src/libs/Pusher/EventType.js b/src/libs/Pusher/EventType.js index 991665bbb9f8..713f69b5c128 100644 --- a/src/libs/Pusher/EventType.js +++ b/src/libs/Pusher/EventType.js @@ -6,5 +6,5 @@ export default { REPORT_COMMENT: 'reportComment', REPORT_COMMENT_EDIT: 'reportCommentEdit', REPORT_TOGGLE_PINNED: 'reportTogglePinned', - PERSONAL_DETAILS_PREFERRED_LOCALE: 'personalDetailsPreferredLocale', + PREFERRED_LOCALE: 'preferredLocale', }; diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index afcba3a38a5b..9635948348cd 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -1,5 +1,6 @@ import Onyx from 'react-native-onyx'; import ONYXKEYS from '../../ONYXKEYS'; +import * as API from '../API'; /** * @param {String} url @@ -8,4 +9,15 @@ function setCurrentURL(url) { Onyx.set(ONYXKEYS.CURRENT_URL, url); } -export default setCurrentURL; +/** +* @param {String} locale +*/ +function setLocale(locale) { + API.PreferredLocale_Update({name: 'preferredLocale', value: locale}); + Onyx.merge(ONYXKEYS.NVP_PREFERRED_LOCALE, locale); +} + +export { + setCurrentURL, + setLocale, +}; diff --git a/src/libs/actions/PersonalDetails.js b/src/libs/actions/PersonalDetails.js index 1d0e9a0d8aee..3cb2dcb9565a 100644 --- a/src/libs/actions/PersonalDetails.js +++ b/src/libs/actions/PersonalDetails.js @@ -10,8 +10,6 @@ import * as API from '../API'; import NameValuePair from './NameValuePair'; import {isDefaultRoom} from '../reportUtils'; import {getReportIcons, getDefaultAvatar} from '../OptionsListUtils'; -import * as Pusher from '../Pusher/pusher'; -import Log from '../Log'; let currentUserEmail = ''; Onyx.connect({ @@ -208,16 +206,6 @@ function mergeLocalPersonalDetails(details) { Onyx.merge(ONYXKEYS.PERSONAL_DETAILS, {[currentUserEmail]: mergedDetails}); } -/** - * Sets the personal details object for the current user - * - * @param {String} preferredLocale - */ -function updatePreferredLocale(preferredLocale) { - API.PreferredLocale_Update({preferredLocale}); - Onyx.merge(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale); -} - /** * Sets the personal details object for the current user * @@ -302,19 +290,6 @@ function deleteAvatar(login) { mergeLocalPersonalDetails({avatar: getDefaultAvatar(login)}); } -function subscribeToPersonalDetails() { - const pusherChannelName = 'private-user-accountID-2'; - Pusher.subscribe(pusherChannelName, Pusher.TYPE.PERSONAL_DETAILS_PREFERRED_LOCALE, () => { - console.log('over here'); - }) - .catch((error) => { - Log.info( - '[Personal Details] Failed to subscribe to Pusher channel', - true, - {error, pusherChannelName, eventName: Pusher.TYPE.PERSONAL_DETAILS_PREFERRED_LOCALE}, - ); - }); -} // When the app reconnects from being offline, fetch all of the personal details NetworkConnection.onReconnect(fetchPersonalDetails); @@ -330,6 +305,4 @@ export { deleteAvatar, fetchCurrencyPreferences, getCurrencyList, - subscribeToPersonalDetails, - updatePreferredLocale, }; diff --git a/src/libs/actions/User.js b/src/libs/actions/User.js index 4bf663aca62f..6e7c895b6552 100644 --- a/src/libs/actions/User.js +++ b/src/libs/actions/User.js @@ -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', ''); }, }); @@ -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, @@ -246,4 +278,5 @@ export { validateLogin, isBlockedFromConcierge, getDomainInfo, + subscribeToUserEvents, }; diff --git a/src/pages/settings/PreferencesPage.js b/src/pages/settings/PreferencesPage.js index 49af9cd49c53..009150001840 100755 --- a/src/pages/settings/PreferencesPage.js +++ b/src/pages/settings/PreferencesPage.js @@ -17,7 +17,7 @@ import Switch from '../../components/Switch'; import Picker from '../../components/Picker'; import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize'; import compose from '../../libs/compose'; -import {updatePreferredLocale} from '../../libs/actions/PersonalDetails'; +import {setLocale} from '../../libs/actions/App'; const propTypes = { /** The chat priority mode */ @@ -116,7 +116,7 @@ const PreferencesPage = ({ { if (locale !== preferredLocale) { - updatePreferredLocale(locale); + setLocale(locale); } }} items={Object.values(localesToLanguages)} From 84cdbadaa33f58f7d8fe01e1e5525ac2a9bb4672 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 19 Jul 2021 15:57:30 -0600 Subject: [PATCH 8/8] remove empty line --- src/libs/actions/PersonalDetails.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/PersonalDetails.js b/src/libs/actions/PersonalDetails.js index 3cb2dcb9565a..c57b81e95c04 100644 --- a/src/libs/actions/PersonalDetails.js +++ b/src/libs/actions/PersonalDetails.js @@ -290,7 +290,6 @@ function deleteAvatar(login) { mergeLocalPersonalDetails({avatar: getDefaultAvatar(login)}); } - // When the app reconnects from being offline, fetch all of the personal details NetworkConnection.onReconnect(fetchPersonalDetails);