15088: Adds link-account state and service
[arvados-workbench2.git] / src / store / link-account-panel / link-account-panel-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { RootState } from "~/store/store";
7 import { ServiceRepository } from "~/services/services";
8 import { setBreadcrumbs } from "~/store/breadcrumbs/breadcrumbs-actions";
9 import { LinkAccountType, AccountToLink } from "~/models/link-account";
10 import { logout } from "~/store/auth/auth-action";
11 import { unionize, ofType, UnionOf } from '~/common/unionize';
12
13 export const linkAccountPanelActions = unionize({
14     LOAD_LINKING: ofType<AccountToLink>()
15 });
16
17 export type LinkAccountPanelAction = UnionOf<typeof linkAccountPanelActions>;
18
19 export const loadLinkAccountPanel = () =>
20     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
21         dispatch(setBreadcrumbs([{ label: 'Link account'}]));
22
23         const linkAccountData = services.linkAccountService.getLinkAccount();
24         if (linkAccountData) {
25             dispatch(linkAccountPanelActions.LOAD_LINKING(linkAccountData));
26         }
27     };
28
29 export const saveAccountLinkData = (t: LinkAccountType) =>
30     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
31         const accountToLink = {type: t, userToken: services.authService.getApiToken()} as AccountToLink;
32         services.linkAccountService.saveLinkAccount(accountToLink);
33         dispatch(logout());
34     };
35
36 export const getAccountLinkData = () =>
37     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
38         return services.linkAccountService.getLinkAccount();
39     };