forked from deriv-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
thisyahlen/chore: move useWalletMigration to api package (deriv-com#1…
…0690) * chore: move useWalletMigration to api package * chore: rename to camelcase
- Loading branch information
1 parent
c6926ae
commit 9c6fe66
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters