Skip to content

Commit

Permalink
Farzin/87456/Extract is_onramp_tab_visible method from cashier `OnR…
Browse files Browse the repository at this point in the history
…ampStore` to reusable hook (#7475)

* feat(core): ✨ add `is_crypto` method to `ClientStore`

* feat(hooks): ✨ add `useOnrampVisible` hook

* refactor(cashier): ♻️ replace `is_onramp_tab_visible` with `useOnrampVisible`

* refactor(core): ♻️ replace `is_onramp_tab_visible` with `useOnrampVisible`

* docs(cashier): 🗑️ deprecated legacy `is_onramp_tab_visible` method

---------

Co-authored-by: Farzin Mirzaie <farzin@deriv.com>
  • Loading branch information
farzin-deriv and Farzin Mirzaie committed Feb 7, 2023
1 parent 055373f commit 3d1e7da
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 29 deletions.
23 changes: 2 additions & 21 deletions packages/cashier/src/containers/cashier/__tests__/cashier.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ describe('<Cashier />', () => {
transaction_history: {
is_crypto_transactions_visible: false,
},
onramp: {
is_onramp_tab_visible: false,
},
payment_agent_transfer: {
is_payment_agent_transfer_visible: false,
},
Expand Down Expand Up @@ -118,6 +115,8 @@ describe('<Cashier />', () => {
is_account_setting_loaded: true,
is_logged_in: true,
is_logging_in: true,
is_virtual: false,
is_crypto: true,
},
modules: {
cashier: {
Expand All @@ -140,9 +139,6 @@ describe('<Cashier />', () => {
transaction_history: {
is_crypto_transactions_visible: true,
},
onramp: {
is_onramp_tab_visible: true,
},
payment_agent_transfer: {
is_payment_agent_transfer_visible: true,
},
Expand Down Expand Up @@ -206,9 +202,6 @@ describe('<Cashier />', () => {
transaction_history: {
is_crypto_transactions_visible: true,
},
onramp: {
is_onramp_tab_visible: true,
},
payment_agent_transfer: {
is_payment_agent_transfer_visible: true,
},
Expand Down Expand Up @@ -267,9 +260,6 @@ describe('<Cashier />', () => {
// transaction_history: {
// is_crypto_transactions_visible: false,
// },
// onramp: {
// is_onramp_tab_visible: true,
// },
// payment_agent_transfer: {
// is_payment_agent_transfer_visible: true,
// },
Expand Down Expand Up @@ -328,9 +318,6 @@ describe('<Cashier />', () => {
transaction_history: {
is_crypto_transactions_visible: true,
},
onramp: {
is_onramp_tab_visible: true,
},
payment_agent_transfer: {
is_payment_agent_transfer_visible: true,
},
Expand Down Expand Up @@ -388,9 +375,6 @@ describe('<Cashier />', () => {
transaction_history: {
is_crypto_transactions_visible: true,
},
onramp: {
is_onramp_tab_visible: true,
},
payment_agent_transfer: {
is_payment_agent_transfer_visible: true,
},
Expand Down Expand Up @@ -447,9 +431,6 @@ describe('<Cashier />', () => {
transaction_history: {
is_crypto_transactions_visible: true,
},
onramp: {
is_onramp_tab_visible: true,
},
payment_agent_transfer: {
is_payment_agent_transfer_visible: true,
},
Expand Down
11 changes: 6 additions & 5 deletions packages/cashier/src/containers/cashier/cashier.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import {
VerticalTab,
Loading,
} from '@deriv/components';
import { useOnrampVisible } from '@deriv/hooks';
import { getSelectedRoute, getStaticUrl, isMobile, routes, WS } from '@deriv/shared';
import { observer, useStore } from '@deriv/stores';
import { localize } from '@deriv/translations';
import AccountPromptDialog from '../../components/account-prompt-dialog';
import ErrorDialog from '../../components/error-dialog';
import { TRootStore, TRoute } from '../../types';
import './cashier.scss';
import { observer, useStore } from '@deriv/stores';
import { useCashierStore } from '../../stores/useCashierStores';
import './cashier.scss';

type TCashierProps = RouteComponentProps & {
routes: TRoute[];
Expand Down Expand Up @@ -49,7 +50,6 @@ const Cashier = observer(({ history, location, routes: routes_config }: TCashier
general_store,
account_transfer,
transaction_history,
onramp,
payment_agent_transfer,
payment_agent,
account_prompt_dialog,
Expand All @@ -67,13 +67,14 @@ const Cashier = observer(({ history, location, routes: routes_config }: TCashier
} = general_store;
const { is_account_transfer_visible } = account_transfer;
const { is_crypto_transactions_visible } = transaction_history;
const { is_onramp_tab_visible } = onramp;
const { is_payment_agent_transfer_visible } = payment_agent_transfer;
const { is_payment_agent_visible } = payment_agent;
const { resetLastLocation } = account_prompt_dialog;
const { routeBackInApp, is_from_derivgo } = common;
const { is_cashier_visible: is_visible, toggleCashier } = ui;
const { is_account_setting_loaded, is_logged_in, is_logging_in, is_pre_appstore } = client;
const is_onramp_visible = useOnrampVisible();

React.useEffect(() => {
toggleCashier();
// we still need to populate the tabs shown on cashier
Expand Down Expand Up @@ -103,7 +104,7 @@ const Cashier = observer(({ history, location, routes: routes_config }: TCashier
(route.path !== routes.cashier_pa || is_payment_agent_visible) &&
(route.path !== routes.cashier_pa_transfer || is_payment_agent_transfer_visible) &&
(route.path !== routes.cashier_p2p || is_p2p_enabled) &&
(route.path !== routes.cashier_onramp || is_onramp_tab_visible) &&
(route.path !== routes.cashier_onramp || is_onramp_visible) &&
(route.path !== routes.cashier_acc_transfer || is_account_transfer_visible)
) {
options.push({
Expand Down
1 change: 1 addition & 0 deletions packages/cashier/src/stores/on-ramp-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default class OnRampStore extends BaseStore {
});
}

/** @deprecated Use `useOnrampVisible` from `@deriv/hooks` package instead. */
get is_onramp_tab_visible() {
const { client } = this.root_store;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import classNames from 'classnames';
import React from 'react';
import { Div100vhContainer, Icon, MobileDrawer, ToggleSwitch, Text, Button } from '@deriv/components';
import { useOnrampVisible } from '@deriv/hooks';
import { routes, PlatformContext, getStaticUrl, whatsapp_url, ContentFlag } from '@deriv/shared';
import { observer, useStore } from '@deriv/stores';
import { localize, getAllowedLanguages, getLanguage } from '@deriv/translations';
Expand Down Expand Up @@ -124,13 +125,13 @@ const ToggleMenuDrawer = observer(({ platform_config }) => {
setIsPreAppStore,
} = client;
const { cashier } = modules;
const { onramp, general_store, payment_agent_transfer, payment_agent, account_transfer } = cashier;
const { is_onramp_tab_visible } = onramp;
const { general_store, payment_agent_transfer, payment_agent, account_transfer } = cashier;
const { is_p2p_enabled } = general_store;
const { is_payment_agent_transfer_visible } = payment_agent_transfer;
const { is_payment_agent_visible } = payment_agent;
const { is_account_transfer_visible } = account_transfer;
const { content_flag, should_show_exit_traders_modal, switchToCRAccount } = traders_hub;
const is_onramp_visible = useOnrampVisible();

const liveChat = useLiveChat();
const [is_open, setIsOpen] = React.useState(false);
Expand Down Expand Up @@ -254,7 +255,7 @@ const ToggleMenuDrawer = observer(({ platform_config }) => {
(route.path !== routes.cashier_pa || is_payment_agent_visible) &&
(route.path !== routes.cashier_pa_transfer || is_payment_agent_transfer_visible) &&
(route.path !== routes.cashier_p2p || is_p2p_enabled) &&
(route.path !== routes.cashier_onramp || is_onramp_tab_visible) &&
(route.path !== routes.cashier_onramp || is_onramp_visible) &&
(route.path !== routes.cashier_acc_transfer || is_account_transfer_visible)
) {
return (
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/Stores/client-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
setCurrencies,
toMoment,
urlForLanguage,
isCryptocurrency,
} from '@deriv/shared';
import { WS, requestLogout } from 'Services';
import { action, computed, makeObservable, observable, reaction, runInAction, toJS, when } from 'mobx';
Expand Down Expand Up @@ -240,6 +241,7 @@ export default class ClientStore extends BaseStore {
all_loginids: computed,
account_title: computed,
currency: computed,
is_crypto: computed,
default_currency: computed,
should_allow_authentication: computed,
is_risky_client: computed,
Expand Down Expand Up @@ -672,6 +674,10 @@ export default class ClientStore extends BaseStore {
return this.default_currency;
}

get is_crypto() {
return isCryptocurrency(this.currency);
}

get default_currency() {
if (Object.keys(this.currencies_list).length > 0) {
const keys = Object.keys(this.currencies_list);
Expand Down
1 change: 1 addition & 0 deletions packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { default as useNeedFinancialAssessment } from './useNeedFinancialAssessm
export { default as useRealSTPAccount } from './useRealSTPAccount';
export { default as useNeedTNC } from './useNeedTNC';
export { default as useDepositLocked } from './useDepositLocked';
export { default as useOnrampVisible } from './useOnrampVisible';
11 changes: 11 additions & 0 deletions packages/hooks/src/useOnrampVisible.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useStore } from '@deriv/stores';

const useOnrampVisible = () => {
const { client } = useStore();
const { is_virtual, is_crypto } = client;
const is_onramp_visible = !is_virtual && is_crypto;

return is_onramp_visible;
};

export default useOnrampVisible;
1 change: 1 addition & 0 deletions packages/stores/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type TClientStore = {
logout: () => Promise<LogOutResponse>;
should_allow_authentication: boolean;
is_landing_company_loaded: boolean;
is_crypto: boolean;
};

type TCommonStoreError = {
Expand Down

0 comments on commit 3d1e7da

Please sign in to comment.