Skip to content

Commit

Permalink
thisyahlen/feat: use account list (binary-com#9768)
Browse files Browse the repository at this point in the history
* 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: add useAccountsList to deriv/api

* fix: address comments

* fix: comments

* fix: comments

* fix: comments

* fix: remove duplicated

---------

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 committed Aug 23, 2023
1 parent 28c010e commit b7ed164
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/api/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as useAuthorize } from './useAuthorize';
export { default as useAccountsList } from './useAccountsList';
40 changes: 40 additions & 0 deletions packages/api/src/hooks/useAccountsList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useMemo } from 'react';
import useAuthorize from './useAuthorize';

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

// Add additional information to the authorize response.
const modified_accounts = useMemo(() => {
return authorize_data.account_list?.map(account => {
return {
...account,
/** Creation time of the account. */
created_at: account.created_at ? new Date(account.created_at) : undefined,
/** Date till client has excluded him/herself from the website, only present if client is self excluded. */
excluded_until: account.excluded_until ? new Date(account.excluded_until) : undefined,
/** Indicating whether the wallet is the currently active account. */
is_active: account.loginid === authorize_data.loginid,
/** indicating whether the account is a virtual-money account. */
is_virtual: Boolean(account.is_virtual),
/** indicating whether the account is marked as disabled or not. */
is_disabled: Boolean(account.is_disabled),
/** indicating whether the account is a trading account. */
is_trading: account.account_category === 'trading',
/** indicating whether the account is a wallet account. */
is_wallet: account.account_category === 'wallet',
/** The account ID of specified account. */
loginid: `${account.loginid}`,
} as const;
});
}, [authorize_data.account_list, authorize_data.loginid]);

return {
/** The list of accounts. */
data: modified_accounts,
...rest,
};
};

export default useAccountsList;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getActiveAuthTokenIDFromLocalStorage } from '@deriv/utils';
import { useMemo } from 'react';
import useFetch from './useFetch';
import useFetch from '../useFetch';

/** A custom hook that authorize the user with the given token. If no token is given, it will use the current token.
*
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { default as useInvalidateQuery } from './useInvalidateQuery';
export { default as useRequest } from './useRequest';
export { default as usePaginatedFetch } from './usePaginatedFetch';
export { default as useSubscription } from './useSubscription';
export * from './hooks';

0 comments on commit b7ed164

Please sign in to comment.