Skip to content

Commit

Permalink
chore: replace localize import with new library (deriv-com#16140)
Browse files Browse the repository at this point in the history
* chore: replace localize import with new library

* chore: removed unused component
  • Loading branch information
likhith-deriv authored Jul 30, 2024
1 parent bd32ef2 commit 1486143
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 72 deletions.
46 changes: 37 additions & 9 deletions __mocks__/translation.mock.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
const Localize = ({ i18n_default_text, values }) => {
// Replace placeholders in the default text with actual values
const localizedText = i18n_default_text.replace(/\{\{(\w+)\}\}/g, (match, key) => values[key] || match);
import React from 'react';

return localizedText || null;
const replaceValue = (text, values) => {
const valueMatch = text.match(/{{(\w+)}}/);
if (valueMatch) {
const valueKey = valueMatch[1];
return values[valueKey] || text;
}
return text;
};

const Localize = ({ i18n_default_text, components = [], values = {} }) => {
// Split text into parts, extracting placeholders for components
const parts = i18n_default_text.split(/(<\d+>.*?<\/\d+>|{{\w+}})/g);

return (
<>
{parts.map((part, index) => {
// Handle component placeholders
const componentMatch = part.match(/<(\d+)>(.*?)<\/\1>/);
if (componentMatch) {
const componentIndex = parseInt(componentMatch[1]);
const content = replaceValue(componentMatch[2], values);
const Component = components[componentIndex];
return Component ? React.cloneElement(Component, { key: index, children: content }) : content;
}
// Replace placeholders with actual values
return replaceValue(part, values);
})}
</>
);
};

const mockFn = jest.fn((text, args) => {
return text.replace(/{{(.*?)}}/g, (_, match) => args[match.trim()]);
});

// Mock for useTranslations hook
const useTranslations = () => ({
localize: jest.fn((text, args) => {
return text.replace(/{{(.*?)}}/g, (_, match) => args[match.trim()]);
}),
localize: mockFn,
currentLang: 'EN',
});

const localize = jest.fn(text => text);
const localize = mockFn;

const getAllowedLanguages = jest.fn(() => ({ EN: 'English', VI: 'Tiếng Việt' }));

export { Localize, localize, useTranslations, getAllowedLanguages };
export { Localize, localize, useTranslations, getAllowedLanguages };
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Field, FormikValues, useFormikContext } from 'formik';
import { Dropdown, SelectNative } from '@deriv/components';
import { EMPLOYMENT_VALUES, TEmploymentStatus, shouldHideOccupationField } from '@deriv/shared';
import { localize } from '@deriv/translations';
import { useTranslations } from '@deriv-com/translations';
import { useDevice } from '@deriv-com/ui';
import {
getAccountTurnoverList,
Expand Down Expand Up @@ -40,13 +40,14 @@ type TFinancialInformationProps = {
const FinancialDetailsDropdownField = ({
dropdown_list,
field_key,
placeholder = localize('Please select'),
placeholder,
label,
}: TFinancialDetailsDropdownFieldProps) => {
const { values, handleChange, handleBlur, touched, errors, setFieldValue } = useFormikContext<{
[key: string]: string;
}>();
const { isDesktop } = useDevice();
const { localize } = useTranslations();
return (
<Field name={field_key}>
{({ field }: FormikValues) => (
Expand All @@ -67,7 +68,7 @@ const FinancialDetailsDropdownField = ({
/>
) : (
<SelectNative
placeholder={placeholder}
placeholder={placeholder ?? localize('Please select')}
name={field.name}
label={label}
list_items={dropdown_list}
Expand All @@ -90,14 +91,15 @@ const FinancialDetailsDropdownField = ({
const FinancialDetailsOccupationDropdownField = ({
dropdown_list,
field_key,
placeholder = localize('Please select'),
placeholder,
label,
employment_status,
}: TFinancialDetailsDropdownFieldProps) => {
const { values, handleChange, handleBlur, touched, errors, setFieldValue } = useFormikContext<{
[key: string]: string;
}>();
const { isDesktop } = useDevice();
const { localize } = useTranslations();

const getFormattedOccupationValues = () =>
employment_status === EMPLOYMENT_VALUES.EMPLOYED && values?.occupation === EMPLOYMENT_VALUES.UNEMPLOYED
Expand Down Expand Up @@ -128,7 +130,7 @@ const FinancialDetailsOccupationDropdownField = ({
) : (
<SelectNative
{...field}
placeholder={placeholder}
placeholder={placeholder ?? localize('Please select')}
name={field.name}
label={label}
list_items={dropdown_list}
Expand All @@ -152,6 +154,8 @@ const FinancialDetailsOccupationDropdownField = ({
* @returns {JSX.Element}
*/
const FinancialInformation = ({ employment_status }: TFinancialInformationProps) => {
const { localize } = useTranslations();

return (
<React.Fragment>
<FinancialDetailsDropdownField
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { Button, Icon, Modal, Text, MobileDialog } from '@deriv/components';
import { localize } from '@deriv/translations';
import { useTranslations } from '@deriv-com/translations';
import { useDevice } from '@deriv-com/ui';

type RiskToleranceWarningModalProps = {
Expand All @@ -21,6 +21,8 @@ const RiskToleranceWarningModal = ({
has_sub_header = false,
}: RiskToleranceWarningModalProps) => {
const { isDesktop } = useDevice();
const { localize } = useTranslations();

return (
<React.Fragment>
{isDesktop ? (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { MobileDialog, Modal } from '@deriv/components';
import { localize } from '@deriv/translations';
import { useTranslations } from '@deriv-com/translations';
import { useDevice } from '@deriv-com/ui';

type TestWarningModalProps = {
Expand All @@ -11,6 +11,8 @@ type TestWarningModalProps = {

const TestWarningModal = ({ show_risk_modal, body_content, footer_content }: TestWarningModalProps) => {
const { isDesktop } = useDevice();
const { localize } = useTranslations();

return (
<React.Fragment>
{isDesktop ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import clsx from 'clsx';
import { Field } from 'formik';
import { Dropdown, Text, SelectNative } from '@deriv/components';
import { localize } from '@deriv/translations';
import { useTranslations } from '@deriv-com/translations';
import { TTradingAssessmentForm, TQuestion } from 'Types';
import { MAX_QUESTION_TEXT_LENGTH } from '../../Constants/trading-assessment';
import { useDevice } from '@deriv-com/ui';
Expand Down Expand Up @@ -45,6 +45,7 @@ const TradingAssessmentDropdown = ({
}, [values]);

const { isDesktop } = useDevice();
const { localize } = useTranslations();

const checkIfAllFieldsFilled = () => {
if (values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import clsx from 'clsx';
import { observer, useStore } from '@deriv/stores';
import { Formik, Form, FormikErrors, FormikHelpers } from 'formik';
import { Button, Modal, Text } from '@deriv/components';
import { localize, Localize } from '@deriv/translations';
import { useTranslations, Localize } from '@deriv-com/translations';
import TradingAssessmentRadioButton from './trading-assessment-radio-buttons';
import TradingAssessmentDropdown from './trading-assessment-dropdown';
import { getTradingAssessmentQuestions } from '../../Constants/trading-assessment-questions';
Expand Down Expand Up @@ -41,6 +41,7 @@ const TradingAssessmentForm = observer(
is_responsive,
}: TradingAssessmentFormProps) => {
const { traders_hub } = useStore();
const { localize } = useTranslations();
const { is_eu_user } = traders_hub;
const assessment_questions = getTradingAssessmentQuestions();
const stored_items = parseInt(localStorage.getItem('current_question_index') ?? '0');
Expand Down
2 changes: 1 addition & 1 deletion packages/account/src/Configs/financial-details-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
EMPLOYMENT_VALUES,
TEmploymentStatus,
} from '@deriv/shared';
import { localize } from '@deriv/translations';
import { localize } from '@deriv-com/translations';

type TFinancialDetailsConfig = {
real_account_signup_target: string;
Expand Down
21 changes: 0 additions & 21 deletions packages/account/src/Containers/toast-popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,12 @@ import { Toast } from '@deriv/components';
import { observer, useStore } from '@deriv/stores';
import { useDevice } from '@deriv-com/ui';

type TToastPopUp = {
portal_id?: string;
className: string;
} & React.ComponentProps<typeof Toast>;

type TNetworkStatusToastError = {
status: string;
portal_id: string;
message: string;
};

export const ToastPopup = ({
portal_id = 'popup_root',
children,
className,
...props
}: React.PropsWithChildren<TToastPopUp>) => {
const new_portal_id = document.getElementById(portal_id);
if (!new_portal_id) return null;
return ReactDOM.createPortal(
<Toast className={clsx('dc-toast-popup', className)} {...props}>
{children}
</Toast>,
new_portal_id
);
};

/**
* Network status Toast components
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useHistory, withRouter } from 'react-router';
import { FormSubmitErrorMessage, Loading, Button, Dropdown, Modal, Icon, SelectNative, Text } from '@deriv/components';
import { routes, platforms, WS, shouldHideOccupationField } from '@deriv/shared';
import { observer, useStore } from '@deriv/stores';
import { localize, Localize } from '@deriv/translations';
import { useTranslations, Localize } from '@deriv-com/translations';
import LeaveConfirm from 'Components/leave-confirm';
import IconMessageContent from 'Components/icon-message-content';
import DemoMessage from 'Components/demo-message';
Expand Down Expand Up @@ -71,34 +71,37 @@ const ConfirmationContent = ({ className }: { className?: string }) => {
);
};

const ConfirmationModal = ({ is_visible, toggleModal, onSubmit }: TConfirmationModal) => (
<Modal
className='financial-assessment-confirmation'
is_open={is_visible}
small
toggleModal={() => toggleModal(false)}
title={localize('Appropriateness Test, WARNING:')}
>
<Modal.Body>
<ConfirmationContent />
</Modal.Body>
<Modal.Footer>
<Button large onClick={() => toggleModal(false)} secondary>
<Localize i18n_default_text='Decline' />
</Button>
<Button
large
onClick={() => {
onSubmit();
toggleModal(false);
}}
primary
>
<Localize i18n_default_text='Accept' />
</Button>
</Modal.Footer>
</Modal>
);
const ConfirmationModal = ({ is_visible, toggleModal, onSubmit }: TConfirmationModal) => {
const { localize } = useTranslations();
return (
<Modal
className='financial-assessment-confirmation'
is_open={is_visible}
small
toggleModal={() => toggleModal(false)}
title={localize('Appropriateness Test, WARNING:')}
>
<Modal.Body>
<ConfirmationContent />
</Modal.Body>
<Modal.Footer>
<Button large onClick={() => toggleModal(false)} secondary>
<Localize i18n_default_text='Decline' />
</Button>
<Button
large
onClick={() => {
onSubmit();
toggleModal(false);
}}
primary
>
<Localize i18n_default_text='Accept' />
</Button>
</Modal.Footer>
</Modal>
);
};

const ConfirmationPage = ({ toggleModal, onSubmit }: TConfirmationPage) => (
<div className='account__confirmation-page'>
Expand Down Expand Up @@ -128,9 +131,10 @@ const ConfirmationPage = ({ toggleModal, onSubmit }: TConfirmationPage) => (
</div>
</div>
);

const SubmittedPage = ({ platform, routeBackInApp }: TSubmittedPage) => {
const history = useHistory();

const { localize } = useTranslations();
const onClickButton = () => {
if (platforms[platform].is_hard_redirect) {
window.location.href = platforms[platform].url;
Expand Down Expand Up @@ -197,6 +201,7 @@ const FinancialAssessment = observer(() => {
const is_mf = landing_company_shortcode === 'maltainvest';

const history = useHistory();
const { localize } = useTranslations();

const [is_loading, setIsLoading] = React.useState(true);
const [is_confirmation_visible, setIsConfirmationVisible] = React.useState(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { localize } from '@deriv/translations';
import { localize } from '@deriv-com/translations';

export const getIncomeSourceList = () => [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { localize, Localize } from '@deriv/translations';
import { Localize, useTranslations } from '@deriv-com/translations';
import FormBody from 'Components/form-body';
import FormSubHeader from 'Components/form-sub-header';
import { RiskToleranceWarningModal, TestWarningModal } from 'Components/trading-assessment';
Expand Down Expand Up @@ -28,6 +28,7 @@ const populateData = form_data => {
};
const TradingAssessment = observer(() => {
const { isDesktop } = useDevice();
const { localize } = useTranslations();
const { client } = useStore();
const { is_virtual, setFinancialAndTradingAssessment } = client;
const history = useHistory();
Expand Down

0 comments on commit 1486143

Please sign in to comment.