15088: Adds link-account state and service
authorEric Biagiotti <ebiagiotti@veritasgenetics.com>
Fri, 26 Apr 2019 19:14:28 +0000 (15:14 -0400)
committerEric Biagiotti <ebiagiotti@veritasgenetics.com>
Fri, 26 Apr 2019 19:14:28 +0000 (15:14 -0400)
Arvados-DCO-1.1-Signed-off-by: Eric Biagiotti <ebiagiotti@veritasgenetics.com>

src/services/link-account-service/link-account-service.ts [new file with mode: 0644]
src/services/services.ts
src/store/link-account-panel/link-account-panel-actions.ts
src/store/link-account-panel/link-account-panel-reducer.ts [new file with mode: 0644]
src/store/store.ts
src/views/link-account-panel/link-account-panel-root.tsx
src/views/link-account-panel/link-account-panel.tsx

diff --git a/src/services/link-account-service/link-account-service.ts b/src/services/link-account-service/link-account-service.ts
new file mode 100644 (file)
index 0000000..fe78d48
--- /dev/null
@@ -0,0 +1,29 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { AxiosInstance } from "axios";
+import { ApiActions } from "~/services/api/api-actions";
+import { AccountToLink } from "~/models/link-account";
+
+export const USER_LINK_ACCOUNT_KEY = 'accountToLink';
+
+export class LinkAccountService {
+
+    constructor(
+        protected apiClient: AxiosInstance,
+        protected actions: ApiActions) { }
+
+    public saveLinkAccount(account: AccountToLink) {
+        sessionStorage.setItem(USER_LINK_ACCOUNT_KEY, JSON.stringify(account));
+    }
+
+    public removeLinkAccount() {
+        sessionStorage.removeItem(USER_LINK_ACCOUNT_KEY);
+    }
+
+    public getLinkAccount() {
+        const data = sessionStorage.getItem(USER_LINK_ACCOUNT_KEY);
+        return data ? JSON.parse(data) as AccountToLink : undefined;
+    }
+}
\ No newline at end of file
index 78ea714b93cb272afcf8c6518dfd621ed64d9d9e..dd3178790a05bdca84c7456418d85562286f1a66 100644 (file)
@@ -31,6 +31,7 @@ import { AuthorizedKeysService } from '~/services/authorized-keys-service/author
 import { VocabularyService } from '~/services/vocabulary-service/vocabulary-service';
 import { NodeService } from '~/services/node-service/node-service';
 import { FileViewersConfigService } from '~/services/file-viewers-config-service/file-viewers-config-service';
+import { LinkAccountService } from "./link-account-service/link-account-service";
 
 export type ServiceRepository = ReturnType<typeof createServices>;
 
@@ -56,6 +57,7 @@ export const createServices = (config: Config, actions: ApiActions) => {
     const userService = new UserService(apiClient, actions);
     const virtualMachineService = new VirtualMachinesService(apiClient, actions);
     const workflowService = new WorkflowService(apiClient, actions);
+    const linkAccountService = new LinkAccountService(apiClient, actions);
 
     const ancestorsService = new AncestorService(groupsService, userService);
     const authService = new AuthService(apiClient, config.rootUrl, actions);
@@ -94,6 +96,7 @@ export const createServices = (config: Config, actions: ApiActions) => {
         webdavClient,
         workflowService,
         vocabularyService,
+        linkAccountService
     };
 };
 
index ff7df9b40b359557ed27d16362f41dec531d3882..3a8661b70a851f118ef44c2c1374b8a1665713cb 100644 (file)
@@ -8,15 +8,32 @@ 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 { unionize, ofType, UnionOf } from '~/common/unionize';
+
+export const linkAccountPanelActions = unionize({
+    LOAD_LINKING: ofType<AccountToLink>()
+});
+
+export type LinkAccountPanelAction = UnionOf<typeof linkAccountPanelActions>;
 
 export const loadLinkAccountPanel = () =>
-    (dispatch: Dispatch<any>) => {
-       dispatch(setBreadcrumbs([{ label: 'Link account'}]));
+    (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
+        dispatch(setBreadcrumbs([{ label: 'Link account'}]));
+
+        const linkAccountData = services.linkAccountService.getLinkAccount();
+        if (linkAccountData) {
+            dispatch(linkAccountPanelActions.LOAD_LINKING(linkAccountData));
+        }
     };
 
 export const saveAccountLinkData = (t: LinkAccountType) =>
     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
         const accountToLink = {type: t, userToken: services.authService.getApiToken()} as AccountToLink;
-        sessionStorage.setItem("accountToLink", JSON.stringify(accountToLink));
+        services.linkAccountService.saveLinkAccount(accountToLink);
         dispatch(logout());
+    };
+
+export const getAccountLinkData = () =>
+    (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
+        return services.linkAccountService.getLinkAccount();
     };
\ No newline at end of file
diff --git a/src/store/link-account-panel/link-account-panel-reducer.ts b/src/store/link-account-panel/link-account-panel-reducer.ts
new file mode 100644 (file)
index 0000000..4515cff
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { linkAccountPanelActions, LinkAccountPanelAction } from "~/store/link-account-panel/link-account-panel-actions";
+import { AccountToLink } from "~/models/link-account";
+
+export interface LinkAccountPanelState {
+    accountToLink: AccountToLink | undefined;
+}
+
+const initialState = {
+    accountToLink: undefined
+};
+
+export const linkAccountPanelReducer = (state: LinkAccountPanelState = initialState, action: LinkAccountPanelAction) =>
+    linkAccountPanelActions.match(action, {
+        default: () => state,
+        LOAD_LINKING: (accountToLink) => ({ ...state, accountToLink }),
+    });
\ No newline at end of file
index 9d7dcdd4579b41f9295e6dad16bdff1662c2a19d..6a37572bc553254eb9c6e1446f6f2d204c658223 100644 (file)
@@ -62,6 +62,7 @@ import { ApiClientAuthorizationMiddlewareService } from '~/store/api-client-auth
 import { PublicFavoritesMiddlewareService } from '~/store/public-favorites-panel/public-favorites-middleware-service';
 import { PUBLIC_FAVORITE_PANEL_ID } from '~/store/public-favorites-panel/public-favorites-action';
 import { publicFavoritesReducer } from '~/store/public-favorites/public-favorites-reducer';
+import { linkAccountPanelReducer } from './link-account-panel/link-account-panel-reducer';
 
 const composeEnhancers =
     (process.env.NODE_ENV === 'development' &&
@@ -163,5 +164,6 @@ const createRootReducer = (services: ServiceRepository) => combineReducers({
     searchBar: searchBarReducer,
     virtualMachines: virtualMachinesReducer,
     repositories: repositoriesReducer,
-    keepServices: keepServicesReducer
+    keepServices: keepServicesReducer,
+    linkAccountPanel: linkAccountPanelReducer
 });
index ce27d2658b7d4a04276c091d6d4f57c9b9fd4909..3b2e204198aa637f4685554b6e31e2b7a3e65032 100644 (file)
@@ -15,7 +15,7 @@ import {
 } from '@material-ui/core';
 import { ArvadosTheme } from '~/common/custom-theme';
 import { User } from "~/models/user";
-import { LinkAccountType } from "~/models/link-account";
+import { LinkAccountType, AccountToLink } from "~/models/link-account";
 import { formatDate }from "~/common/formatters";
 
 type CssRules = 'root';// | 'gridItem' | 'label' | 'title' | 'actions';
@@ -29,6 +29,7 @@ const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
 
 export interface LinkAccountPanelRootDataProps {
     user?: User;
+    accountToLink?: AccountToLink;
 }
 
 export interface LinkAccountPanelRootActionProps {
index eff8ca80a488d842839067e8ec477a91670a7c0b..491f2055a82b95f28e6da8479920b4e23f675970 100644 (file)
@@ -15,7 +15,8 @@ import {
 
 const mapStateToProps = (state: RootState): LinkAccountPanelRootDataProps => {
     return {
-        user: state.auth.user
+        user: state.auth.user,
+        accountToLink: state.linkAccountPanel.accountToLink
     };
 };