Skip to content

Commit

Permalink
Merge branch 'master' of github.com:binary-com/deriv-app into P2PS-67…
Browse files Browse the repository at this point in the history
…5-share-my-posted-ads
  • Loading branch information
ameerul-deriv committed Aug 28, 2023
2 parents 3090458 + 4e00227 commit 5282366
Show file tree
Hide file tree
Showing 27 changed files with 1,124 additions and 743 deletions.
1,196 changes: 616 additions & 580 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/account/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"dependencies": {
"@binary-com/binary-document-uploader": "^2.4.8",
"@deriv/api-types": "^1.0.116",
"@deriv/api-types": "^1.0.118",
"@deriv/components": "^1.0.0",
"@deriv/hooks": "^1.0.0",
"@deriv/shared": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@tanstack/react-query-devtools": "^4.28.0"
},
"devDependencies": {
"@deriv/api-types": "^1.0.116",
"@deriv/api-types": "^1.0.118",
"@testing-library/react": "^12.0.0",
"@testing-library/react-hooks": "^7.0.2",
"@testing-library/user-event": "^13.5.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ export { default as useActiveWalletAccounts } from './useActiveWalletAccounts';
export { default as useAuthorize } from './useAuthorize';
export { default as useBalance } from './useBalance';
export { default as useCurrencyConfig } from './useCurrencyConfig';
export { default as useMT5LoginList } from './useMT5LoginList';
export { default as useSettings } from './useSettings';
export { default as useTradingPlatformAccounts } from './useTradingPlatformAccounts';
export { default as useWalletAccountsList } from './useWalletAccountsList';
4 changes: 4 additions & 0 deletions packages/api/src/hooks/useAccountsList.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useMemo } from 'react';
import useAuthorize from './useAuthorize';
import useBalance from './useBalance';
import useCurrencyConfig from './useCurrencyConfig';

/** A custom hook that returns the list of accounts of the logged in user. */
const useAccountsList = () => {
const { data: authorize_data, ...rest } = useAuthorize();
const { data: balance_data } = useBalance();
const { getConfig } = useCurrencyConfig();

// Add additional information to the authorize response.
const modified_accounts = useMemo(() => {
Expand All @@ -28,6 +30,8 @@ const useAccountsList = () => {
is_wallet: account.account_category === 'wallet',
/** The account ID of specified account. */
loginid: `${account.loginid}`,
/** Account's currency config information */
currency_config: account.currency ? getConfig(account.currency) : undefined,
} as const;
});
}, [authorize_data.account_list, authorize_data.loginid]);
Expand Down
File renamed without changes.
37 changes: 37 additions & 0 deletions packages/api/src/hooks/useMT5LoginList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useMemo } from 'react';
import useActiveWalletAccounts from './useActiveWalletAccounts';
import useFetch from '../useFetch';

/** A custom hook that gets the list created MT5 accounts of the user. */
const useMT5LoginList = () => {
const { data: wallet } = useActiveWalletAccounts();

const { data: mt5_accounts, ...mt5_accounts_rest } = useFetch('mt5_login_list');

/**
* @description The list of created MT5 accounts
*/
const modified_mt5_accounts = useMemo(() => {
/** Adding the neccesary properties to the response */
const getAccountInfo = (login?: string) => {
return {
platform: wallet?.linked_to?.find(linked => linked.loginid === login)?.platform,
display_login: login?.replace(/^(MT[DR]?)/, ''),
};
};

return mt5_accounts?.mt5_login_list?.map(account => ({
...account,
...getAccountInfo(account.login),
loginid: account.login,
}));
}, [mt5_accounts?.mt5_login_list, wallet?.linked_to]);

return {
/** The list of created MT5 accounts */
data: modified_mt5_accounts,
...mt5_accounts_rest,
};
};

export default useMT5LoginList;
32 changes: 32 additions & 0 deletions packages/api/src/hooks/useSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useCallback, useMemo } from 'react';
import useFetch from '../useFetch';
import useInvalidateQuery from '../useInvalidateQuery';
import useRequest from '../useRequest';

type TSetSettingsPayload = NonNullable<
NonNullable<NonNullable<Parameters<ReturnType<typeof useRequest<'set_settings'>>['mutate']>>[0]>['payload']
>;

/** A custom hook to get user settings (email, date of birth, address etc) */
const useSettings = () => {
const { data, ...rest } = useFetch('get_settings');
const invalidate = useInvalidateQuery();
const { mutate, ...mutate_rest } = useRequest('set_settings', {
onSuccess: () => invalidate('get_settings'),
});

const update = useCallback((values: TSetSettingsPayload) => mutate({ payload: { ...values } }), [mutate]);

const modified_data = useMemo(() => ({ ...data?.get_settings }), [data?.get_settings]);

return {
/** User information and settings */
data: modified_data,
/** Function to update user settings */
update,
mutation: mutate_rest,
...rest,
};
};

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

/** A custom hook that gets the list of created other CFD accounts. */
const useTradingPlatformAccounts = () => {
const { data: derivez_accounts, ...derivez_rest } = useFetch('trading_platform_accounts', {
payload: { platform: 'derivez' },
});
const { data: dxtrade_accounts, ...dxtrade_rest } = useFetch('trading_platform_accounts', {
payload: { platform: 'dxtrade' },
});

/** Adding neccesary properties to derivez accounts */
const modified_derivez_accounts = useMemo(
() =>
derivez_accounts?.trading_platform_accounts?.map(account => ({
...account,
loginid: account.login,
})),
[derivez_accounts?.trading_platform_accounts]
);

/** Adding neccesary properties to dxtrade accounts */
const modified_dxtrade_accounts = useMemo(
() =>
dxtrade_accounts?.trading_platform_accounts?.map(account => ({
...account,
loginid: account.account_id,
})),
[dxtrade_accounts?.trading_platform_accounts]
);

const data = useMemo(
() => ({
dxtrade_accounts: modified_dxtrade_accounts || [],
derivez_accounts: modified_derivez_accounts || [],
}),
[modified_dxtrade_accounts, modified_derivez_accounts]
);

return {
/** List of all created other CFD accounts */
data,
...{ ...derivez_rest, ...dxtrade_rest },
};
};

export default useTradingPlatformAccounts;
17 changes: 16 additions & 1 deletion packages/api/src/hooks/useWalletAccountsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,24 @@ const useWalletAccountsList = () => {
});
}, [filtered_accounts]);

// Sort wallet accounts alphabetically by fiat, crypto, then virtual.
const sorted_accounts = useMemo(() => {
if (!modified_accounts) return [];

return [...modified_accounts].sort((a, b) => {
if (a.is_virtual !== b.is_virtual) {
return a.is_virtual ? 1 : -1;
} else if (a.currency_config?.is_crypto !== b.currency_config?.is_crypto) {
return a.currency_config?.is_crypto ? 1 : -1;
}

return (a.currency || 'USD').localeCompare(b.currency || 'USD');
});
}, [modified_accounts]);

return {
/** List of all wallet accounts for the current user. */
data: modified_accounts,
data: sorted_accounts,
...rest,
};
};
Expand Down
Loading

0 comments on commit 5282366

Please sign in to comment.