Skip to content

Commit

Permalink
thisyahlen/chore: move useWalletMigration to api package (deriv-com#1…
Browse files Browse the repository at this point in the history
…0690)

* chore: move useWalletMigration to api package

* chore: rename to camelcase
  • Loading branch information
thisyahlen-deriv authored Oct 16, 2023
1 parent c6926ae commit 9c6fe66
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/api/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
51 changes: 51 additions & 0 deletions packages/api/src/hooks/useWalletMigration.ts
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 3 additions & 1 deletion packages/hooks/src/useWalletMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9c6fe66

Please sign in to comment.