Skip to content

Commit

Permalink
move functions
Browse files Browse the repository at this point in the history
  • Loading branch information
luacmartins committed Jul 19, 2021
1 parent ddda46f commit 2df99f8
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 35 deletions.
5 changes: 3 additions & 2 deletions src/libs/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Navigation/AppNavigator/AuthScreens.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Navigation/NavigationRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Pusher/EventType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
14 changes: 13 additions & 1 deletion 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 @@ -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,
};
27 changes: 0 additions & 27 deletions src/libs/actions/PersonalDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
Expand All @@ -330,6 +305,4 @@ export {
deleteAvatar,
fetchCurrencyPreferences,
getCurrencyList,
subscribeToPersonalDetails,
updatePreferredLocale,
};
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,
};
4 changes: 2 additions & 2 deletions src/pages/settings/PreferencesPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -116,7 +116,7 @@ const PreferencesPage = ({
<Picker
onChange={(locale) => {
if (locale !== preferredLocale) {
updatePreferredLocale(locale);
setLocale(locale);
}
}}
items={Object.values(localesToLanguages)}
Expand Down

0 comments on commit 2df99f8

Please sign in to comment.