Skip to content

Commit

Permalink
FarhanNurzi/feat: WALL-1574/Add useAvailableAccounts hook to @deriv/a…
Browse files Browse the repository at this point in the history
…pi (deriv-com#9786)

* feat: added use-authorize hook taken from sergei pr

Co-authored-by: Sergei Baranovski <120570511+sergei-deriv@users.noreply.github.com>

* chore: sorted imports for use-authorize

Co-authored-by: Sergei Baranovski <120570511+sergei-deriv@users.noreply.github.com>

* chore: moved default empty string in use-authorize

* chore: incorporated code reviews

* chore: added useLandingCompany into api package

* chore: added useLandingCompany into api package

* chore: added useAvailableAccounts and useAccountTypes hooks into api package

* chore: added useSettings to @deriv/api

* chore: get country_code from user settings instead of authorize

* chore: combine get_settings with set_settings

* fix: change request type for landing_company

* chore: combine get_settings with set_settings

* refactor: change function name

* chore: add landing_company field to each account types

* chore: add missing dependencies for useLandingCompany return data

* chore: return all mutation data

* chore: export hook

* refactor: types and mutation function name

* refactor: use-landing-company-hook

* fix: remove dependency

* fix: remove dependency

* refactor: use-available-accounts hook

* fix: review comments, remove gaming accounts

* refactor: separate accounts usememo

---------

Co-authored-by: adrienne-rio <adrienne@deriv.com>
Co-authored-by: Sergei Baranovski <120570511+sergei-deriv@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 29, 2023
1 parent 379bc41 commit 39ce848
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/api/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { default as useActiveAccount } from './useActiveAccount';
export { default as useActiveTradingAccount } from './useActiveTradingAccount';
export { default as useActiveWalletAccount } from './useActiveWalletAccount';
export { default as useAuthorize } from './useAuthorize';
export { default as useAvailableAccounts } from './useAvailableAccounts';
export { default as useBalance } from './useBalance';
export { default as useCurrencyConfig } from './useCurrencyConfig';
export { default as useGetAccountStatus } from './useGetAccountStatus';
Expand Down
31 changes: 31 additions & 0 deletions packages/api/src/hooks/useAccountTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useMemo } from 'react';
import useFetch from '../useFetch';

/**
* A custom hook to get available account types for a specific landing company
*
* @param landing_company {string} - The landing company shortcode
*/
const useAccountTypes = (landing_company?: string) => {
const { data, ...rest } = useFetch('get_account_types', {
payload: { company: landing_company },
});

const modified_data = useMemo(() => {
if (!data?.get_account_types) return;

return {
/** List of available account types */
...data.get_account_types,
/** Landing company for the account types */
landing_company,
};
}, [data?.get_account_types, landing_company]);

return {
data: modified_data,
...rest,
};
};

export default useAccountTypes;
47 changes: 47 additions & 0 deletions packages/api/src/hooks/useAvailableAccounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useMemo } from 'react';
import useAccountTypes from './useAccountTypes';
import useLandingCompany from './useLandingCompany';

/** A custom hook to get available accounts for every landing companies */
const useAvailableAccounts = () => {
const { data: landing_company_data } = useLandingCompany();
const { data: available_financial_accounts } = useAccountTypes(landing_company_data?.financial_company_shortcode);
const { data: available_virtual_accounts } = useAccountTypes(landing_company_data?.virtual_company_shortcode);

// memoize the available financial accounts
const financial_accounts = useMemo(() => {
if (!available_financial_accounts) return;

return {
...available_financial_accounts,
};
}, [available_financial_accounts]);

// memoize the available virtual accounts
const virtual_accounts = useMemo(() => {
if (!available_virtual_accounts) return;

return {
...available_virtual_accounts,
};
}, [available_virtual_accounts]);

// memoize the combined available accounts
const available_accounts = useMemo(() => {
if (!available_financial_accounts && !available_virtual_accounts) return;

return {
/** List of available financial accounts */
financial_accounts,
/** List of available virtual accounts */
virtual_accounts,
};
}, [available_financial_accounts, available_virtual_accounts]);

return {
/** List of available accounts */
data: available_accounts,
};
};

export default useAvailableAccounts;
4 changes: 1 addition & 3 deletions packages/api/src/hooks/useLandingCompany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ const useLandingCompany = () => {

const modified_data = useMemo(() => {
if (!data?.landing_company) return;
const { financial_company, gaming_company, virtual_company } = data.landing_company;
const { financial_company, virtual_company } = data.landing_company;
return {
...data.landing_company,
/** Short code of financial landing company */
financial_company_shortcode: financial_company?.shortcode,
/** Short code of gaming landing company */
gaming_company_shortcode: gaming_company?.shortcode,
/** Short code of virtual landing company */
virtual_company_shortcode: virtual_company,
};
Expand Down
83 changes: 83 additions & 0 deletions packages/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,89 @@ type TPrivateSocketEndpoints = {
[k: string]: unknown;
};
};
get_account_types: {
request: {
/**
* Must be `1`
*/
get_account_types: 1;
/**
* [Optional] Set to landing company to get relevant account types. If not set, this defaults to current account landing company
*/
company?: string;
/**
* [Optional] Used to pass data through the websocket, which may be retrieved via the `echo_req` output field. Maximum size is 3500 bytes.
*/
passthrough?: {
[k: string]: unknown;
};
/**
* [Optional] Used to map request to response.
*/
req_id?: number;
};
response: {
get_account_types?: {
/**
* Trading account types that are available to create or link to
*/
trading: {
/**
* Details for trading account types
*
* This interface was referenced by `undefined`'s JSON-Schema definition
* via the `patternProperty` "^(binary|dxtrade|mt5|standard|derivez)$".
*/
[k: string]: {
/**
* Wallet currencies allowed for this trading account
*/
allowed_wallet_currencies: string[];
/**
* Can this trading account linked to another currency after opening
*/
linkable_to_different_currency: 0 | 1;
/**
* Wallet types that this trading account can be linked to.
*/
linkable_wallet_types: string[];
};
};
/**
* Wallet accounts types that are available to create or link to
*/
wallet: {
/**
* Details for wallets account types
*
* This interface was referenced by `undefined`'s JSON-Schema definition
* via the `patternProperty` "^(affiliate|crypto|doughflow|p2p|paymentagent|paymentagent_client|virtual)$".
*/
[k: string]: {
/**
* Allowed currencies for creating accounts of this type; used or disallowed currencies are not listed.
*/
currencies: string[];
};
};
};
/**
* Echo of the request made.
*/
echo_req: {
[k: string]: unknown;
};
/**
* Action name of the request made.
*/
msg_type: 'get_account_types';
/**
* Optional field sent in request to map to response, present only when request contains `req_id`.
*/
req_id?: number;
[k: string]: unknown;
};
};
};

type TSocketEndpoints = {
Expand Down

0 comments on commit 39ce848

Please sign in to comment.