diff --git a/src/components/profile/ProfileAccounts/index.tsx b/src/components/profile/ProfileAccounts/index.tsx index a720dae0..fa239dbf 100644 --- a/src/components/profile/ProfileAccounts/index.tsx +++ b/src/components/profile/ProfileAccounts/index.tsx @@ -15,6 +15,8 @@ const mapStateToProps = (state: IState) => selectAuthProfile(state).socials; const mapDispatchToProps = { authGetSocials: AUTH_ACTIONS.authGetSocials, authDropSocial: AUTH_ACTIONS.authDropSocial, + authAttachSocial: AUTH_ACTIONS.authAttachSocial, + authSetSocials: AUTH_ACTIONS.authSetSocials, }; type IProps = ReturnType & typeof mapDispatchToProps & {}; @@ -27,21 +29,29 @@ const SOCIAL_ICONS: Record = { const ProfileAccountsUnconnected: FC = ({ authGetSocials, authDropSocial, + authAttachSocial, + authSetSocials, accounts, is_loading, }) => { - const onMessage = useCallback(event => { - // TODO: handle errors - if (event?.data?.type !== 'oauth_attach' || !event?.data?.payload?.token) return; + const onMessage = useCallback( + (event: MessageEvent) => { + if (!event?.data?.type) return; - const token = event?.data?.payload?.token; - - console.log('GOT TOKEN!!!', token); - }, []); + switch (event?.data?.type) { + case 'oauth_processed': + return authAttachSocial(event?.data?.payload?.token); + case 'oauth_error': + return authSetSocials({ error: event?.data?.payload?.error || '' }); + default: + return; + } + }, + [authAttachSocial, authSetSocials] + ); const openOauthWindow = useCallback( (provider: ISocialProvider) => () => { - console.log(API.USER.OAUTH_WINDOW(provider)); window.open(API.USER.OAUTH_WINDOW(provider), '', 'width=600,height=400'); }, [] diff --git a/src/redux/auth/actions.ts b/src/redux/auth/actions.ts index 461d7a61..0f32fd18 100644 --- a/src/redux/auth/actions.ts +++ b/src/redux/auth/actions.ts @@ -121,3 +121,8 @@ export const authSetSocials = (socials: Partial ({ + type: AUTH_USER_ACTIONS.ATTACH_SOCIAL, + token, +}); diff --git a/src/redux/auth/constants.ts b/src/redux/auth/constants.ts index 65b72686..722fc34a 100644 --- a/src/redux/auth/constants.ts +++ b/src/redux/auth/constants.ts @@ -29,6 +29,7 @@ export const AUTH_USER_ACTIONS = { DROP_SOCIAL: 'DROP_SOCIAL', ADD_SOCIAL: 'ADD_SOCIAL', SET_SOCIALS: 'SET_SOCIALS', + ATTACH_SOCIAL: 'ATTACH_SOCIAL', }; export const USER_ERRORS = {