15088: Updates state to account for both types of linking and adds UI
[arvados.git] / src / store / link-account-panel / link-account-panel-actions.ts
index dd3a79e93193f659d61206d8bd4de9af57736664..42fd6c52481d0b286d412f5f10411f95c74b684b 100644 (file)
@@ -7,11 +7,13 @@ import { RootState } from "~/store/store";
 import { ServiceRepository } from "~/services/services";
 import { setBreadcrumbs } from "~/store/breadcrumbs/breadcrumbs-actions";
 import { LinkAccountType, AccountToLink } from "~/models/link-account";
-import { logout } from "~/store/auth/auth-action";
+import { logout, saveApiToken, saveUser } from "~/store/auth/auth-action";
 import { unionize, ofType, UnionOf } from '~/common/unionize';
+import { UserResource } from "~/models/user";
+import { navigateToRootProject } from "~/store/navigation/navigation-action";
 
 export const linkAccountPanelActions = unionize({
-    LOAD_LINKING: ofType<AccountToLink>(),
+    LOAD_LINKING: ofType<{ user: UserResource | undefined, userToLink: UserResource | undefined }>(),
     REMOVE_LINKING: {}
 });
 
@@ -21,15 +23,33 @@ export const loadLinkAccountPanel = () =>
     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
         dispatch(setBreadcrumbs([{ label: 'Link account'}]));
 
-        const linkAccountData = services.linkAccountService.getLinkAccount();
-        if (linkAccountData) {
-            dispatch(linkAccountPanelActions.LOAD_LINKING(linkAccountData));
+        const curUser = getState().auth.user;
+        if (curUser) {
+            services.userService.get(curUser.uuid).then(curUserResource => {
+                const linkAccountData = services.linkAccountService.getLinkAccount();
+                if (linkAccountData) {
+                    services.userService.get(linkAccountData.userUuid).then(savedUserResource => {
+                        if (linkAccountData.type === LinkAccountType.ADD_OTHER_LOGIN) {
+                            dispatch<any>(linkAccountPanelActions.LOAD_LINKING({ userToLink: curUserResource, user: savedUserResource }));
+                        }
+                        else if (linkAccountData.type === LinkAccountType.ACCESS_OTHER_ACCOUNT) {
+                            dispatch<any>(linkAccountPanelActions.LOAD_LINKING({ userToLink: savedUserResource, user: curUserResource }));
+                        }
+                        else {
+                            throw new Error('Invalid link account type.');
+                        }
+                    });
+                }
+                else {
+                    dispatch<any>(linkAccountPanelActions.LOAD_LINKING({ userToLink: undefined, user: curUserResource }));
+                }
+            });
         }
     };
 
 export const saveAccountLinkData = (t: LinkAccountType) =>
     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
-        const accountToLink = {type: t, userToken: services.authService.getApiToken()} as AccountToLink;
+        const accountToLink = {type: t, userUuid: services.authService.getUuid(), token: services.authService.getApiToken()} as AccountToLink;
         services.linkAccountService.saveLinkAccount(accountToLink);
         dispatch(logout());
     };
@@ -41,6 +61,19 @@ export const getAccountLinkData = () =>
 
 export const removeAccountLinkData = () =>
     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
+        const linkAccountData = services.linkAccountService.getLinkAccount();
         services.linkAccountService.removeLinkAccount();
         dispatch(linkAccountPanelActions.REMOVE_LINKING());
+        if (linkAccountData) {
+            services.userService.get(linkAccountData.userUuid).then(savedUser => {
+                dispatch(setBreadcrumbs([{ label: ''}]));
+                dispatch<any>(saveUser(savedUser));
+                dispatch<any>(saveApiToken(linkAccountData.token));
+                dispatch<any>(navigateToRootProject);
+            });
+        }
+    };
+
+export const linkAccount = () =>
+    (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
     };
\ No newline at end of file