1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 04:46:40 +07:00

listening for backend events

This commit is contained in:
Fedor Katurov 2020-07-27 17:35:59 +07:00
parent 2466f01b0b
commit 3ae22fb63d
3 changed files with 24 additions and 8 deletions

View file

@ -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 mapStateToProps> & typeof mapDispatchToProps & {};
@ -27,21 +29,29 @@ const SOCIAL_ICONS: Record<ISocialProvider, string> = {
const ProfileAccountsUnconnected: FC<IProps> = ({
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');
},
[]

View file

@ -121,3 +121,8 @@ export const authSetSocials = (socials: Partial<IAuthState['profile']['socials']
type: AUTH_USER_ACTIONS.SET_SOCIALS,
socials,
});
export const authAttachSocial = (token: string) => ({
type: AUTH_USER_ACTIONS.ATTACH_SOCIAL,
token,
});

View file

@ -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 = {