From ce4d6daad292e391776aed465cab475449a8f757 Mon Sep 17 00:00:00 2001 From: simeng-li Date: Tue, 26 Apr 2022 17:31:00 +0800 Subject: [PATCH] refactor(ui): extract useBindSocial hook (#667) extract useBindSocial hook --- .../SocialCreateAccount/index.test.tsx | 8 +-- .../containers/SocialCreateAccount/index.tsx | 62 +++++++------------ packages/ui/src/hooks/use-bind-social.ts | 50 +++++++++++++++ .../ui/src/pages/SocialRegister/index.tsx | 2 +- 4 files changed, 76 insertions(+), 46 deletions(-) create mode 100644 packages/ui/src/hooks/use-bind-social.ts diff --git a/packages/ui/src/containers/SocialCreateAccount/index.test.tsx b/packages/ui/src/containers/SocialCreateAccount/index.test.tsx index 2182e9313..6f569f8f8 100644 --- a/packages/ui/src/containers/SocialCreateAccount/index.test.tsx +++ b/packages/ui/src/containers/SocialCreateAccount/index.test.tsx @@ -21,13 +21,13 @@ jest.mock('@/apis/social', () => ({ describe('SocialCreateAccount', () => { it('should match snapshot', () => { - const { queryByText } = render(); + const { queryByText } = render(); expect(queryByText('description.social_create_account')).not.toBeNull(); expect(queryByText('description.social_bind_account')).not.toBeNull(); }); it('should redirect to sign in page when click sign-in button', () => { - const { getByText } = render(); + const { getByText } = render(); const signInButton = getByText('action.sign_in'); fireEvent.click(signInButton); @@ -35,7 +35,7 @@ describe('SocialCreateAccount', () => { }); it('should call registerWithSocial when click create button', async () => { - const { getByText } = renderWithPageContext(); + const { getByText } = renderWithPageContext(); const createButton = getByText('action.create'); await waitFor(() => { @@ -46,7 +46,7 @@ describe('SocialCreateAccount', () => { }); it('should render bindUser Button when relatedUserInfo found', async () => { - const { getByText } = renderWithPageContext(); + const { getByText } = renderWithPageContext(); const bindButton = getByText('action.bind'); await waitFor(() => { fireEvent.click(bindButton); diff --git a/packages/ui/src/containers/SocialCreateAccount/index.tsx b/packages/ui/src/containers/SocialCreateAccount/index.tsx index 911e8139f..cda3d677e 100644 --- a/packages/ui/src/containers/SocialCreateAccount/index.tsx +++ b/packages/ui/src/containers/SocialCreateAccount/index.tsx @@ -1,69 +1,49 @@ -import { Optional } from '@silverhand/essentials'; import classNames from 'classnames'; -import React, { useEffect, useCallback } from 'react'; +import React, { useCallback } from 'react'; import { useTranslation } from 'react-i18next'; -import { useNavigate, useLocation } from 'react-router-dom'; +import { useNavigate } from 'react-router-dom'; -import { registerWithSocial, bindSocialRelatedUser } from '@/apis/social'; import Button from '@/components/Button'; -import useApi from '@/hooks/use-api'; +import useBindSocial from '@/hooks/use-bind-social'; import * as styles from './index.module.scss'; type Props = { className?: string; - connector: string; + connectorId: string; }; -type LocationState = { - relatedUser?: string; -}; - -const SocialCreateAccount = ({ connector, className }: Props) => { +const SocialCreateAccount = ({ connectorId, className }: Props) => { const navigate = useNavigate(); - const state = useLocation().state as Optional; const { t } = useTranslation(undefined, { keyPrefix: 'main_flow' }); - - const { result: registerResult, run: asyncRegisterWithSocial } = useApi(registerWithSocial); - const { result: bindUserResult, run: asyncBindSocialRelatedUser } = useApi(bindSocialRelatedUser); - - const createAccountHandler = useCallback(() => { - void asyncRegisterWithSocial(connector); - }, [asyncRegisterWithSocial, connector]); - - const bindRelatedUserHandler = useCallback(() => { - void asyncBindSocialRelatedUser(connector); - }, [asyncBindSocialRelatedUser, connector]); + const { relatedUser, registerWithSocial, bindSocialRelatedUser } = useBindSocial(); const signInHandler = useCallback(() => { // TODO: redirect to desired sign-in page - navigate('/sign-in/username/' + connector); - }, [connector, navigate]); - - useEffect(() => { - if (registerResult?.redirectTo) { - window.location.assign(registerResult.redirectTo); - } - }, [registerResult]); - - useEffect(() => { - if (bindUserResult?.redirectTo) { - window.location.assign(bindUserResult.redirectTo); - } - }, [bindUserResult]); + navigate('/sign-in/username/' + connectorId); + }, [connectorId, navigate]); return (
- {state?.relatedUser && ( + {relatedUser && ( <>
{t('description.social_bind_with_existing')}
- )}
{t('description.social_create_account')}
-
{t('description.social_bind_account')}
diff --git a/packages/ui/src/hooks/use-bind-social.ts b/packages/ui/src/hooks/use-bind-social.ts new file mode 100644 index 000000000..98d8848f5 --- /dev/null +++ b/packages/ui/src/hooks/use-bind-social.ts @@ -0,0 +1,50 @@ +import { Optional } from '@silverhand/essentials'; +import { useCallback, useEffect } from 'react'; +import { useLocation } from 'react-router-dom'; + +import { registerWithSocial, bindSocialRelatedUser } from '@/apis/social'; +import useApi from '@/hooks/use-api'; + +type LocationState = { + relatedUser?: string; +}; + +const useBindSocial = () => { + const state = useLocation().state as Optional; + const { result: registerResult, run: asyncRegisterWithSocial } = useApi(registerWithSocial); + const { result: bindUserResult, run: asyncBindSocialRelatedUser } = useApi(bindSocialRelatedUser); + + const createAccountHandler = useCallback( + (connectorId: string) => { + void asyncRegisterWithSocial(connectorId); + }, + [asyncRegisterWithSocial] + ); + + const bindRelatedUserHandler = useCallback( + (connectorId) => { + void asyncBindSocialRelatedUser(connectorId); + }, + [asyncBindSocialRelatedUser] + ); + + useEffect(() => { + if (registerResult?.redirectTo) { + window.location.assign(registerResult.redirectTo); + } + }, [registerResult]); + + useEffect(() => { + if (bindUserResult?.redirectTo) { + window.location.assign(bindUserResult.redirectTo); + } + }, [bindUserResult]); + + return { + relatedUser: state?.relatedUser, + registerWithSocial: createAccountHandler, + bindSocialRelatedUser: bindRelatedUserHandler, + }; +}; + +export default useBindSocial; diff --git a/packages/ui/src/pages/SocialRegister/index.tsx b/packages/ui/src/pages/SocialRegister/index.tsx index 82b70a804..73e99ea70 100644 --- a/packages/ui/src/pages/SocialRegister/index.tsx +++ b/packages/ui/src/pages/SocialRegister/index.tsx @@ -29,7 +29,7 @@ const SocialRegister = () => { return (
- +
); };