From 36c74eacd76d7be6e1064f4d152f720c9df16df8 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 20 Jan 2023 12:08:17 -1000 Subject: [PATCH 1/2] The Localize HOC is not hooked into the currentUserPersonalDetails timezone changes. We just need to add the currentUserPersonalDetails in and then pass the selected timezone to datetimeToCalendarTime() and they will update in realtime. --- src/components/withLocalize.js | 18 +++++++++++++----- src/libs/DateUtils.js | 6 ++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/components/withLocalize.js b/src/components/withLocalize.js index 918c05594825..9880fecbeb46 100755 --- a/src/components/withLocalize.js +++ b/src/components/withLocalize.js @@ -1,6 +1,8 @@ import React, {createContext, forwardRef} from 'react'; import PropTypes from 'prop-types'; import {withOnyx} from 'react-native-onyx'; +import lodashGet from 'lodash/get'; + import getComponentDisplayName from '../libs/getComponentDisplayName'; import ONYXKEYS from '../ONYXKEYS'; import * as Localize from '../libs/Localize'; @@ -9,6 +11,8 @@ import * as LocalePhoneNumber from '../libs/LocalePhoneNumber'; import * as NumberFormatUtils from '../libs/NumberFormatUtils'; import * as LocaleDigitUtils from '../libs/LocaleDigitUtils'; import CONST from '../CONST'; +import compose from '../libs/compose'; +import withCurrentUserPersonalDetails from './withCurrentUserPersonalDetails'; const LocaleContext = createContext(null); @@ -105,6 +109,7 @@ class LocaleContextProvider extends React.Component { this.props.preferredLocale, datetime, includeTimezone, + lodashGet(this.props, 'currentUserPersonalDetails.timezone.selected'), ); } @@ -152,11 +157,14 @@ class LocaleContextProvider extends React.Component { LocaleContextProvider.propTypes = localeProviderPropTypes; LocaleContextProvider.defaultProps = localeProviderDefaultProps; -const Provider = withOnyx({ - preferredLocale: { - key: ONYXKEYS.NVP_PREFERRED_LOCALE, - }, -})(LocaleContextProvider); +const Provider = compose( + withCurrentUserPersonalDetails, + withOnyx({ + preferredLocale: { + key: ONYXKEYS.NVP_PREFERRED_LOCALE, + }, + }), +)(LocaleContextProvider); Provider.displayName = 'withOnyx(LocaleContextProvider)'; diff --git a/src/libs/DateUtils.js b/src/libs/DateUtils.js index 6fc5aa83902a..663d3a5f93fa 100644 --- a/src/libs/DateUtils.js +++ b/src/libs/DateUtils.js @@ -49,6 +49,7 @@ function getLocalMomentFromDatetime(locale, datetime, currentSelectedTimezone = if (!datetime) { return moment.tz(currentSelectedTimezone); } + return moment.utc(datetime).tz(currentSelectedTimezone); } @@ -63,11 +64,12 @@ function getLocalMomentFromDatetime(locale, datetime, currentSelectedTimezone = * @param {String} locale * @param {String} datetime * @param {Boolean} includeTimeZone + * @param {String} [currentSelectedTimezone] * * @returns {String} */ -function datetimeToCalendarTime(locale, datetime, includeTimeZone = false) { - const date = getLocalMomentFromDatetime(locale, datetime); +function datetimeToCalendarTime(locale, datetime, includeTimeZone = false, currentSelectedTimezone) { + const date = getLocalMomentFromDatetime(locale, datetime, currentSelectedTimezone); const tz = includeTimeZone ? ' [UTC]Z' : ''; const todayAt = Localize.translate(locale, 'common.todayAt'); From 5499c28920f27d24603ae920b505d4ca53a5a3a0 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 20 Jan 2023 12:16:07 -1000 Subject: [PATCH 2/2] fix propTypes --- src/components/withLocalize.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/components/withLocalize.js b/src/components/withLocalize.js index 9880fecbeb46..a188e413ddf6 100755 --- a/src/components/withLocalize.js +++ b/src/components/withLocalize.js @@ -46,12 +46,24 @@ const localeProviderPropTypes = { /** The user's preferred locale e.g. 'en', 'es-ES' */ preferredLocale: PropTypes.string, - /* Actual content wrapped by this component */ + /** Actual content wrapped by this component */ children: PropTypes.node.isRequired, + + /** The current user's personalDetails */ + currentUserPersonalDetails: PropTypes.shape({ + + /** Timezone of the current user */ + timezone: PropTypes.shape({ + + /** Value of the selected timezone */ + selected: PropTypes.string, + }), + }), }; const localeProviderDefaultProps = { preferredLocale: CONST.DEFAULT_LOCALE, + currentUserPersonalDetails: {}, }; class LocaleContextProvider extends React.Component {