diff --git a/src/libs/DateUtils.js b/src/libs/DateUtils.js index b2107c821310..1ffe14c5e9ec 100644 --- a/src/libs/DateUtils.js +++ b/src/libs/DateUtils.js @@ -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; }, }); @@ -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); } /** @@ -115,6 +109,22 @@ 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 */ @@ -122,6 +132,8 @@ const DateUtils = { timestampToRelative, timestampToDateTime, startCurrentDateUpdater, + updateTimezone, + throttledUpdateTimezone, }; export default DateUtils; diff --git a/src/pages/home/report/ReportActionCompose.js b/src/pages/home/report/ReportActionCompose.js index 787bfa996b12..ef106f675138 100755 --- a/src/pages/home/report/ReportActionCompose.js +++ b/src/pages/home/report/ReportActionCompose.js @@ -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 = { @@ -428,6 +429,8 @@ class ReportActionCompose extends React.Component { return; } + DateUtils.throttledUpdateTimezone(); + this.props.onSubmit(trimmedComment); this.updateComment(''); this.setTextInputShouldClear(true); diff --git a/src/pages/settings/InitialPage.js b/src/pages/settings/InitialPage.js index 8bfa58596188..f806b55d0696 100755 --- a/src/pages/settings/InitialPage.js +++ b/src/pages/settings/InitialPage.js @@ -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 */ @@ -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',