Skip to content

Commit

Permalink
Merge pull request #4150 from Expensify/marcaaron-fixIpWithLocalize
Browse files Browse the repository at this point in the history
  • Loading branch information
iwiznia authored Jul 26, 2021
2 parents c009187 + 67e0a87 commit 48bf2f5
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 45 deletions.
141 changes: 102 additions & 39 deletions src/components/withLocalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import getComponentDisplayName from '../libs/getComponentDisplayName';
import compose from '../libs/compose';
import ONYXKEYS from '../ONYXKEYS';
import {translate} from '../libs/translate';
import DateUtils from '../libs/DateUtils';
Expand Down Expand Up @@ -30,59 +29,123 @@ const withLocalizePropTypes = {
fromLocalPhone: PropTypes.func.isRequired,
};

function withLocalizeHOC(WrappedComponent) {
const WithLocalize = (props) => {
const translations = {
translate: (phrase, variables) => translate(props.preferredLocale, phrase, variables),
numberFormat: (number, options) => numberFormat(props.preferredLocale, number, options),
timestampToRelative: timestamp => DateUtils.timestampToRelative(props.preferredLocale, timestamp),
timestampToDateTime: (timestamp, includeTimezone) => DateUtils.timestampToDateTime(
props.preferredLocale,
timestamp,
includeTimezone,
),
toLocalPhone: number => toLocalPhone(props.preferredLocale, number),
fromLocalPhone: number => fromLocalPhone(props.preferredLocale, number),
};
return (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={props.forwardedRef}
translate={translations.translate}
numberFormat={translations.numberFormat}
timestampToRelative={translations.timestampToRelative}
timestampToDateTime={translations.timestampToDateTime}
toLocalPhone={translations.toLocalPhone}
fromLocalPhone={translations.fromLocalPhone}
/>
);
};
WithLocalize.displayName = `WithLocalize(${getComponentDisplayName(WrappedComponent)})`;
WithLocalize.propTypes = {
export default (WrappedComponent) => {
const propTypes = {
/** The user's preferred locale e.g. 'en', 'es-ES' */
preferredLocale: PropTypes.string,

/** Passed ref from whatever component is wrapped in the HOC */
forwardedRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({current: PropTypes.instanceOf(React.Component)}),
]),
};
WithLocalize.defaultProps = {

const defaultProps = {
preferredLocale: CONST.DEFAULT_LOCALE,
forwardedRef: undefined,
};
return React.forwardRef((props, ref) => (

class WithLocalize extends React.Component {
constructor(props) {
super(props);

this.translate = this.translate.bind(this);
this.numberFormat = this.numberFormat.bind(this);
this.timestampToRelative = this.timestampToRelative.bind(this);
this.timestampToDateTime = this.timestampToDateTime.bind(this);
this.fromLocalPhone = this.fromLocalPhone.bind(this);
this.toLocalPhone = this.toLocalPhone.bind(this);
}

/**
* @param {String} phrase
* @param {Object} [variables]
* @returns {String}
*/
translate(phrase, variables) {
return translate(this.props.preferredLocale, phrase, variables);
}

/**
* @param {Number} number
* @param {Intl.NumberFormatOptions} options
* @returns {String}
*/
numberFormat(number, options) {
return numberFormat(this.props.preferredLocale, number, options);
}

/**
* @param {Number} timestamp
* @returns {String}
*/
timestampToRelative(timestamp) {
return DateUtils.timestampToRelative(this.props.preferredLocale, timestamp);
}

/**
* @param {Number} timestamp
* @param {Boolean} [includeTimezone]
* @returns {String}
*/
timestampToDateTime(timestamp, includeTimezone) {
return DateUtils.timestampToDateTime(
this.props.preferredLocale,
timestamp,
includeTimezone,
);
}

/**
* @param {Number} number
* @returns {String}
*/
toLocalPhone(number) {
return toLocalPhone(this.props.preferredLocale, number);
}

/**
* @param {Number} number
* @returns {String}
*/
fromLocalPhone(number) {
return fromLocalPhone(this.props.preferredLocale, number);
}

render() {
return (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...this.props}
ref={this.props.forwardedRef}
translate={this.translate}
numberFormat={this.numberFormat}
timestampToRelative={this.timestampToRelative}
timestampToDateTime={this.timestampToDateTime}
toLocalPhone={this.toLocalPhone}
fromLocalPhone={this.fromLocalPhone}
/>
);
}
}

WithLocalize.propTypes = propTypes;
WithLocalize.defaultProps = defaultProps;

const withForwardedRef = React.forwardRef((props, ref) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<WithLocalize {...props} forwardedRef={ref} />
));
}
export default compose(
withOnyx({

withForwardedRef.displayName = `withLocalize(${getComponentDisplayName(WrappedComponent)})`;

return withOnyx({
preferredLocale: {
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
},
}),
withLocalizeHOC,
);
})(withForwardedRef);
};

export {
withLocalizePropTypes,
Expand Down
4 changes: 2 additions & 2 deletions src/libs/LocalePhoneNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import translations from '../languages/translations';
*
* @param {String} locale eg 'en', 'es-ES'
* @param {String} number
* @returns {string}
* @returns {String}
*/
function toLocalPhone(locale, number) {
const numString = lodashTrim(number);
Expand All @@ -31,7 +31,7 @@ function toLocalPhone(locale, number) {
*
* @param {String} locale eg 'en', 'es-ES'
* @param {String} number
* @returns {string}
* @returns {String}
*/
function fromLocalPhone(locale, number) {
const numString = lodashTrim(number);
Expand Down
5 changes: 1 addition & 4 deletions src/pages/home/report/ReportActionContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import ContextMenuItem from '../../../components/ContextMenuItem';
import ReportActionPropTypes from './ReportActionPropTypes';
import Clipboard from '../../../libs/Clipboard';
import compose from '../../../libs/compose';
import {isReportMessageAttachment, canEditReportAction, canDeleteReportAction} from '../../../libs/reportUtils';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import ReportActionComposeFocusManager from '../../../libs/ReportActionComposeFocusManager';
Expand Down Expand Up @@ -197,6 +196,4 @@ class ReportActionContextMenu extends React.Component {
ReportActionContextMenu.propTypes = propTypes;
ReportActionContextMenu.defaultProps = defaultProps;

export default compose(
withLocalize,
)(ReportActionContextMenu);
export default withLocalize(ReportActionContextMenu);
2 changes: 2 additions & 0 deletions src/pages/home/report/ReportActionItemSingle.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ const ReportActionItemSingle = ({

ReportActionItemSingle.propTypes = propTypes;
ReportActionItemSingle.defaultProps = defaultProps;
ReportActionItemSingle.displayName = 'ReportActionItemSingle';

export default compose(
withLocalize,
withOnyx({
Expand Down
1 change: 1 addition & 0 deletions src/pages/home/report/ReportView.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const ReportView = ({reportID, session}) => (

ReportView.propTypes = propTypes;
ReportView.defaultProps = defaultProps;
ReportView.displayName = 'ReportView';

export default withOnyx({
session: {
Expand Down

0 comments on commit 48bf2f5

Please sign in to comment.