Skip to content
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

#4517 update timezone on user activity #4799

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/libs/DateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@ import Onyx from 'react-native-onyx';
import ONYXKEYS from '../ONYXKEYS';
import CONST from '../CONST';
import {translate} from './translate';
import * as PersonalDetails from './actions/PersonalDetails';

let timezone;
Onyx.connect({
key: ONYXKEYS.MY_PERSONAL_DETAILS,
callback: (val) => {
timezone = val ? val.timezone : CONST.DEFAULT_TIME_ZONE.selected;

// Make sure that if we have a timezone in object format that we're getting the selected timezone name
// Older timezone formats only include the timezone name, but the newer format also included whether or
// not the timezone was selected automatically
if (_.isObject(timezone)) {
timezone = val.timezone.selected;
}
timezone = val ? val.timezone : CONST.DEFAULT_TIME_ZONE;
},
});

Expand All @@ -37,7 +31,7 @@ Onyx.connect({
*/
function getLocalMomentFromTimestamp(locale, timestamp) {
moment.locale(locale);
return moment.unix(timestamp).tz(timezone);
return moment.unix(timestamp).tz(timezone.selected);
}

/**
Expand Down Expand Up @@ -115,13 +109,31 @@ function startCurrentDateUpdater() {
});
}

/*
* Updates user's timezone, if their timezone is set to automatic and
* is different from current timezone
*/
function updateTimezone() {
const currentTimezone = moment.tz.guess(true);
if (timezone.automatic && timezone.selected !== currentTimezone) {
PersonalDetails.setPersonalDetails({timezone: {...timezone, selected: currentTimezone}});
}
}

/*
* Returns a version of updateTimezone function throttled by 5 minutes
*/
const throttledUpdateTimezone = _.throttle(() => updateTimezone(), 1000 * 60 * 5);

/**
* @namespace DateUtils
*/
const DateUtils = {
timestampToRelative,
timestampToDateTime,
startCurrentDateUpdater,
updateTimezone,
throttledUpdateTimezone,
};

export default DateUtils;
3 changes: 3 additions & 0 deletions src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {participantPropTypes} from '../sidebar/optionPropTypes';
import currentUserPersonalDetailsPropsTypes from '../../settings/Profile/currentUserPersonalDetailsPropsTypes';
import ParticipantLocalTime from './ParticipantLocalTime';
import {withNetwork, withPersonalDetails} from '../../../components/OnyxProvider';
import DateUtils from '../../../libs/DateUtils';
import Tooltip from '../../../components/Tooltip';

const propTypes = {
Expand Down Expand Up @@ -428,6 +429,8 @@ class ReportActionCompose extends React.Component {
return;
}

DateUtils.throttledUpdateTimezone();

this.props.onSubmit(trimmedComment);
this.updateComment('');
this.setTextInputShouldClear(true);
Expand Down
6 changes: 5 additions & 1 deletion src/pages/settings/InitialPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import ROUTES from '../../ROUTES';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import compose from '../../libs/compose';
import CONST from '../../CONST';
import DateUtils from '../../libs/DateUtils';

const propTypes = {
/* Onyx Props */
Expand Down Expand Up @@ -90,7 +91,10 @@ const defaultMenuItems = [
{
translationKey: 'common.profile',
icon: Profile,
action: () => { Navigation.navigate(ROUTES.SETTINGS_PROFILE); },
action: () => {
DateUtils.updateTimezone();
Navigation.navigate(ROUTES.SETTINGS_PROFILE);
},
},
{
translationKey: 'common.preferences',
Expand Down