diff --git a/packages/api/src/hooks/index.ts b/packages/api/src/hooks/index.ts index db9c782955b0..b528d09c689d 100644 --- a/packages/api/src/hooks/index.ts +++ b/packages/api/src/hooks/index.ts @@ -36,3 +36,4 @@ export { default as useTransactions } from './useTransactions'; export { default as useTransferBetweenAccounts } from './useTransferBetweenAccounts'; export { default as useWalletAccountsList } from './useWalletAccountsList'; export { default as useVerifyEmail } from './useVerifyEmail'; +export { default as useWalletMigration } from './useWalletMigration'; diff --git a/packages/api/src/hooks/useWalletMigration.ts b/packages/api/src/hooks/useWalletMigration.ts new file mode 100644 index 000000000000..e760bb2de16f --- /dev/null +++ b/packages/api/src/hooks/useWalletMigration.ts @@ -0,0 +1,51 @@ +import { useCallback } from 'react'; +import useAuthorize from './useAuthorize'; +import useInvalidateQuery from '../useInvalidateQuery'; +import useMutation from '../useMutation'; +import useQuery from '../useQuery'; + +/** A custom hook to get the status of wallet_migration API and to start/reset the migration process */ +const useWalletMigration = () => { + const invalidate = useInvalidateQuery(); + + /** Make a request to wallet_migration API and onSuccess it will invalidate the cached data */ + const { mutate } = useMutation('wallet_migration', { onSuccess: () => invalidate('wallet_migration') }); + + const { isSuccess } = useAuthorize(); + + /** Fetch the wallet_migration API and refetch it every second if the status is in_progress */ + const { data } = useQuery('wallet_migration', { + payload: { wallet_migration: 'state' }, + options: { + refetchInterval: response => (response?.wallet_migration?.state === 'in_progress' ? 500 : false), + enabled: isSuccess, + }, + }); + + const startMigration = useCallback(() => mutate({ payload: { wallet_migration: 'start' } }), [mutate]); + + const resetMigration = useCallback(() => mutate({ payload: { wallet_migration: 'reset' } }), [mutate]); + + const state = data?.wallet_migration?.state; + + return { + /** The status of the wallet_migration API */ + state, + /** A boolean to check if the status is not_eligible */ + is_ineligible: state === 'ineligible', + /** A boolean to check if the status is eligible */ + is_eligible: state === 'eligible', + /** A boolean to check if the status is in_progress */ + is_in_progress: state === 'in_progress', + /** A boolean to check if the status is completed */ + is_migrated: state === 'migrated', + /** A boolean to check if the status is failed */ + is_failed: state === 'failed', + /** Sends a request to wallet_migration API to start the migration process */ + startMigration, + /** Sends a request to wallet_migration API to reset the migration process */ + resetMigration, + }; +}; + +export default useWalletMigration; diff --git a/packages/hooks/src/useWalletMigration.ts b/packages/hooks/src/useWalletMigration.ts index 1c2d4a7186d3..bd1bbd53a3c0 100644 --- a/packages/hooks/src/useWalletMigration.ts +++ b/packages/hooks/src/useWalletMigration.ts @@ -2,7 +2,9 @@ import { useCallback } from 'react'; import { useAuthorize, useFetch, useInvalidateQuery, useRequest } from '@deriv/api'; import { useStore } from '@deriv/stores'; -/** A custom hook to get the status of wallet_migration API and to start/reset the migration process */ +/** A custom hook to get the status of wallet_migration API and to start/reset the migration process + * @deprecated This hook is deprecated. Please use the hook from @deriv/api instead. + */ const useWalletMigration = () => { // TODO: delete it later, it's a temporary solution // because we have to check for authorize from client store before doing API call