Skip to content

Commit

Permalink
george / rm65041 / migrate p2p-cashier component to TS (deriv-com#6177)
Browse files Browse the repository at this point in the history
* refactor(p2p-cashier): migrate p2p-cashier component to TS

* commit

* perf(types): add types for notification store

* perf(types): add types for ui store

* perf(types): convert p2p-cashier to TS

* refactor(types): change RootStore type to TRootStore

* test(p2p-cashier): test fix (change wrapper order fro p2p-cashier component)

* refactor(types): refactor current_focus type

* test(p2p-cashier): remove eslint disables
  • Loading branch information
heorhi-deriv authored Sep 6, 2022
1 parent d03e7ad commit 503e77e
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Text } from '@deriv/components';
import { isMobile } from '@deriv/shared';
import { Localize } from '@deriv/translations';
import { connect } from 'Stores/connect';
import { RootStore } from 'Types';
import { TRootStore } from 'Types';
import './virtual.scss';

type TVirtualProps = RouteComponentProps & {
Expand Down Expand Up @@ -51,7 +51,7 @@ const Virtual = ({ is_dark_mode_on, toggleAccountsDialog }: TVirtualProps) => {
);
};

export default connect(({ ui }: RootStore) => ({
export default connect(({ ui }: TRootStore) => ({
is_dark_mode_on: ui.is_dark_mode_on,
toggleAccountsDialog: ui.toggleAccountsDialog,
}))(withRouter(Virtual));
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Icon, Text } from '@deriv/components';
import { localize, Localize } from '@deriv/translations';
import { formatDate } from '@deriv/shared';
import { connect } from 'Stores/connect';
import { RootStore } from 'Types';
import { TRootStore } from 'Types';

type TAccount = NonNullable<Authorize['account_list']>[0];

Expand Down Expand Up @@ -292,7 +292,7 @@ const CashierLocked = ({
);
};

export default connect(({ client, modules }: RootStore) => ({
export default connect(({ client, modules }: TRootStore) => ({
account_status: client.account_status,
accounts: client.accounts,
current_currency_type: client.current_currency_type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DesktopWrapper, Input, Icon, MobileWrapper, Text, useInterval } from '@
import { getCurrencyDisplayCode } from '@deriv/shared';
import { localize, Localize } from '@deriv/translations';
import { connect } from 'Stores/connect';
import { RootStore, TReactChangeEvent, TReactChildren } from 'Types';
import { TRootStore, TReactChangeEvent, TReactChildren } from 'Types';
import './crypto-fiat-converter.scss';

type TTimerProps = {
Expand Down Expand Up @@ -170,7 +170,7 @@ const CryptoFiatConverter = ({
);
};

export default connect(({ modules }: RootStore) => ({
export default connect(({ modules }: TRootStore) => ({
converter_from_amount: modules.cashier.crypto_fiat_converter.converter_from_amount,
converter_from_error: modules.cashier.crypto_fiat_converter.converter_from_error,
converter_to_error: modules.cashier.crypto_fiat_converter.converter_to_error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Button, Modal } from '@deriv/components';
import { localize, Localize } from '@deriv/translations';
import { connect } from 'Stores/connect';
import { RootStore } from 'Types';
import { TRootStore } from 'Types';

type TCryptoTransactionsCancelModalProps = {
cancelCryptoTransaction: (selected_crypto_transaction_id: string) => void;
Expand Down Expand Up @@ -45,7 +45,7 @@ const CryptoTransactionsCancelModal = ({
);
};

export default connect(({ modules }: RootStore) => ({
export default connect(({ modules }: TRootStore) => ({
cancelCryptoTransaction: modules.cashier.transaction_history.cancelCryptoTransaction,
hideCryptoTransactionsCancelModal: modules.cashier.transaction_history.hideCryptoTransactionsCancelModal,
is_cancel_modal_visible: modules.cashier.transaction_history.is_crypto_transactions_cancel_modal_visible,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DataList, Icon, Loading, MobileWrapper, Table, Text } from '@deriv/comp
import { isDesktop, isMobile, routes } from '@deriv/shared';
import { localize, Localize } from '@deriv/translations';
import { connect } from 'Stores/connect';
import { RootStore, TCryptoTransactionDetails } from 'Types';
import { TRootStore, TCryptoTransactionDetails } from 'Types';
import CryptoTransactionsCancelModal from './crypto-transactions-cancel-modal';
import CryptoTransactionsStatusModal from './crypto-transactions-status-modal';
import CryptoTransactionsRenderer from './crypto-transactions-renderer';
Expand Down Expand Up @@ -102,7 +102,7 @@ const CryptoTransactionsHistory = ({
);
};

export default connect(({ client, modules }: RootStore) => ({
export default connect(({ client, modules }: TRootStore) => ({
crypto_transactions: modules.cashier.transaction_history.crypto_transactions,
currency: client.currency,
is_loading: modules.cashier.transaction_history.is_loading,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { epochToMoment, formatMoney, isMobile } from '@deriv/shared';
import { localize, Localize } from '@deriv/translations';
import { getStatus } from 'Constants/transaction-status';
import { connect } from 'Stores/connect';
import { RootStore, TCryptoTransactionDetails } from 'Types';
import { TRootStore, TCryptoTransactionDetails } from 'Types';

type TCryptoTransactionsRendererProps = {
row: TCryptoTransactionDetails;
Expand Down Expand Up @@ -304,7 +304,7 @@ const CryptoTransactionsRenderer = ({
);
};

export default connect(({ client, modules }: RootStore) => ({
export default connect(({ client, modules }: TRootStore) => ({
currency: client.currency,
cancelCryptoTransaction: modules.cashier.transaction_history.cancelCryptoTransaction,
showCryptoTransactionsCancelModal: modules.cashier.transaction_history.showCryptoTransactionsCancelModal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Button, Modal } from '@deriv/components';
import { localize } from '@deriv/translations';
import { connect } from 'Stores/connect';
import { TReactMouseEvent, RootStore } from 'Types';
import { TReactMouseEvent, TRootStore } from 'Types';

type TCryptoTransactionsStatusModalProps = {
hideCryptoTransactionsStatusModal: (e: TReactMouseEvent) => void;
Expand Down Expand Up @@ -34,7 +34,7 @@ const CryptoTransactionsStatusModal = ({
);
};

export default connect(({ modules }: RootStore) => ({
export default connect(({ modules }: TRootStore) => ({
hideCryptoTransactionsStatusModal: modules.cashier.transaction_history.hideCryptoTransactionsStatusModal,
is_status_modal_visible: modules.cashier.transaction_history.is_crypto_transactions_status_modal_visible,
selected_crypto_status: modules.cashier.transaction_history.selected_crypto_status,
Expand Down
4 changes: 2 additions & 2 deletions packages/cashier/src/components/error-dialog/error-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Dialog } from '@deriv/components';
import { localize, Localize } from '@deriv/translations';
import { routes } from '@deriv/shared';
import { connect } from 'Stores/connect';
import { RootStore, TError, TReactElement } from 'Types';
import { TRootStore, TError, TReactElement } from 'Types';

type TErrorDialogProps = {
disableApp: () => void;
Expand Down Expand Up @@ -148,7 +148,7 @@ const ErrorDialog = ({ disableApp, enableApp, error = {} }: TErrorDialogProps) =
);
};

export default connect(({ ui }: RootStore) => ({
export default connect(({ ui }: TRootStore) => ({
disableApp: ui.disableApp,
enableApp: ui.enableApp,
}))(ErrorDialog);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Icon, Button, Text } from '@deriv/components';
import { Localize, localize } from '@deriv/translations';
import { connect } from 'Stores/connect';
import { RootStore } from 'Types';
import { TRootStore } from 'Types';
import './funds-protection.scss';

type TFundsProtectionProps = {
Expand Down Expand Up @@ -40,6 +40,6 @@ const FundsProtection = ({ submitFundsProtection }: TFundsProtectionProps) => {
);
};

export default connect(({ modules }: RootStore) => ({
export default connect(({ modules }: TRootStore) => ({
submitFundsProtection: modules.cashier.deposit.submitFundsProtection,
}))(FundsProtection);
4 changes: 2 additions & 2 deletions packages/cashier/src/components/no-balance/no-balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Button, Icon, Text } from '@deriv/components';
import { routes, getCurrencyDisplayCode } from '@deriv/shared';
import { localize, Localize } from '@deriv/translations';
import { connect } from 'Stores/connect';
import { RootStore } from 'Types';
import { TRootStore } from 'Types';

type TNoBalanceProps = RouteComponentProps & {
currency: string;
Expand Down Expand Up @@ -48,7 +48,7 @@ const NoBalance = ({ currency, history, is_deposit_locked, setTabIndex }: TNoBal
};

export default withRouter(
connect(({ client, modules }: RootStore) => ({
connect(({ client, modules }: TRootStore) => ({
currency: client.currency,
is_deposit_locked: modules.cashier.deposit.is_deposit_locked,
setTabIndex: modules.cashier.general_store.setCashierTabIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ButtonLink, Text, Icon } from '@deriv/components';
import { Localize } from '@deriv/translations';
import { epochToMoment } from '@deriv/shared';
import { connect } from 'Stores/connect';
import { RootStore } from 'Types';
import { TRootStore } from 'Types';
import { getStatus } from 'Constants/transaction-status';
import './recent-transaction.scss';

Expand Down Expand Up @@ -132,7 +132,7 @@ const RecentTransaction = ({
);
};

export default connect(({ modules, client }: RootStore) => ({
export default connect(({ modules, client }: TRootStore) => ({
crypto_transactions: modules.cashier.transaction_history.crypto_transactions,
currency: client.currency,
onMount: modules.cashier.transaction_history.onMount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jest.mock('@deriv/components', () => ({
Loading: () => <div>Loading</div>,
}));

jest.mock('@deriv/p2p', () => () => <div>P2P</div>);
jest.mock('@deriv/p2p', () => jest.fn(() => 'P2P'));

describe('<P2PCashier />', () => {
const history = createBrowserHistory();
Expand Down
3 changes: 0 additions & 3 deletions packages/cashier/src/pages/p2p-cashier/index.js

This file was deleted.

3 changes: 3 additions & 0 deletions packages/cashier/src/pages/p2p-cashier/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import P2PCashier from './p2p-cashier';

export default P2PCashier;
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
import React from 'react';
import PropTypes from 'prop-types';
import { RouteComponentProps } from 'react-router';
import { withRouter } from 'react-router-dom';
import { getLanguage } from '@deriv/translations';
import { routes, WS } from '@deriv/shared';
import { Loading } from '@deriv/components';
import P2P from '@deriv/p2p';
import { connect } from 'Stores/connect';
import { Loading } from '@deriv/components';
import { routes, WS } from '@deriv/shared';
import { getLanguage } from '@deriv/translations';
import { get, init, timePromise } from 'Utils/server_time';
import { connect } from 'Stores/connect';
import { TClientStore, TCommonStore, TNotificationStore, TRootStore, TUiStore } from 'Types';

type TLocalCurrencyConfig = {
currency: string;
decimal_places: number;
};

type TP2PCashierProps = RouteComponentProps & {
addNotificationMessage: TNotificationStore['addNotificationMessage'];
balance: TClientStore['balance'];
currency: TClientStore['currency'];
current_focus: TUiStore['current_focus'];
filterNotificationMessages: TNotificationStore['filterNotificationMessages'];
is_dark_mode_on: TUiStore['is_dark_mode_on'];
is_logging_in: TClientStore['is_logging_in'];
is_mobile: TUiStore['is_mobile'];
is_virtual: TClientStore['is_virtual'];
local_currency_config: TLocalCurrencyConfig;
loginid: TClientStore['loginid'];
Notifications: TUiStore['notification_messages_ui'];
platform: TCommonStore['platform'];
refreshNotifications: TNotificationStore['refreshNotifications'];
removeNotificationByKey: TNotificationStore['removeNotificationByKey'];
removeNotificationMessage: TNotificationStore['removeNotificationMessage'];
residence: TClientStore['residence'];
setCurrentFocus: TUiStore['setCurrentFocus'];
// TODO: replace setNotificationCount and setOnRemount types when cashier.general_store will be typed
setNotificationCount: (value: number) => void;
setOnRemount: (func: () => void) => void;
};

/* P2P will use the same websocket connection as Deriv/Binary, we need to pass it as a prop */
const P2PCashier = ({
addNotificationMessage,
balance,
currency,
current_focus,
filterNotificationMessages,
Expand All @@ -28,31 +59,19 @@ const P2PCashier = ({
removeNotificationByKey,
removeNotificationMessage,
residence,
setNotificationCount,
setCurrentFocus,
balance,
setNotificationCount,
setOnRemount,
}) => {
const [order_id, setOrderId] = React.useState(null);
}: TP2PCashierProps) => {
const [order_id, setOrderId] = React.useState<string | null>(null);
const server_time = {
get,
init,
timePromise,
};

React.useEffect(() => {
const url_params = new URLSearchParams(location.search);
const passed_order_id = url_params.get('order');

if (passed_order_id) {
setQueryOrder(passed_order_id);
}

return () => setQueryOrder(null);
}, [location.search, setQueryOrder]);

const setQueryOrder = React.useCallback(
input_order_id => {
(input_order_id: string | null) => {
const current_query_params = new URLSearchParams(location.search);

if (current_query_params.has('order')) {
Expand Down Expand Up @@ -84,6 +103,17 @@ const P2PCashier = ({
[history, location.hash, location.search, order_id]
);

React.useEffect(() => {
const url_params = new URLSearchParams(location.search);
const passed_order_id = url_params.get('order');

if (passed_order_id) {
setQueryOrder(passed_order_id);
}

return () => setQueryOrder(null);
}, [location.search, setQueryOrder]);

if (is_logging_in) {
return <Loading is_fullscreen />;
}
Expand Down Expand Up @@ -118,30 +148,7 @@ const P2PCashier = ({
);
};

P2PCashier.propTypes = {
addNotificationMessage: PropTypes.func,
balance: PropTypes.string,
currency: PropTypes.string,
current_focus: PropTypes.string,
filterNotificationMessages: PropTypes.func,
history: PropTypes.object,
is_dark_mode_on: PropTypes.bool,
is_logging_in: PropTypes.bool,
is_mobile: PropTypes.bool,
is_virtual: PropTypes.bool,
local_currency_config: PropTypes.object,
location: PropTypes.object,
loginid: PropTypes.string,
platform: PropTypes.any,
refreshNotifications: PropTypes.func,
removeNotificationByKey: PropTypes.func,
removeNotificationMessage: PropTypes.func,
residence: PropTypes.string,
setNotificationCount: PropTypes.func,
setCurrentFocus: PropTypes.func,
};

export default connect(({ client, common, modules, notifications, ui }) => ({
export default connect(({ client, common, modules, notifications, ui }: TRootStore) => ({
addNotificationMessage: notifications.addNotificationMessage,
balance: client.balance,
currency: client.currency,
Expand Down
1 change: 1 addition & 0 deletions packages/cashier/src/types/stores/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './client-store.types';
export * from './common-store.types';
export * from './notification-store.types';
export * from './root-store.types';
export * from './ui-store.types';
Loading

0 comments on commit 503e77e

Please sign in to comment.