fae099d5717b3f12bde13763746d26e3f4c9a944
[arvados-workbench2.git] / src / views-components / api-token / api-token.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { RouteProps } from "react-router";
6 import * as React from "react";
7 import { connect, DispatchProp } from "react-redux";
8 import { authActions, getUserDetails, saveApiToken } from "~/store/auth/auth-action";
9 import { getUrlParameter } from "~/common/url";
10 import { AuthService } from "~/services/auth-service/auth-service";
11 import { navigateToRootProject, navigateToLinkAccount } from "~/store/navigation/navigation-action";
12 import { User } from "~/models/user";
13 import { Config } from "~/common/config";
14 import { initSessions } from "~/store/auth/auth-action-session";
15 import { getAccountLinkData } from "~/store/link-account-panel/link-account-panel-actions";
16
17 interface ApiTokenProps {
18     authService: AuthService;
19     config: Config;
20     loadMainApp: boolean;
21 }
22
23 export const ApiToken = connect()(
24     class extends React.Component<ApiTokenProps & RouteProps & DispatchProp<any>, {}> {
25         componentDidMount() {
26             const search = this.props.location ? this.props.location.search : "";
27             const apiToken = getUrlParameter(search, 'api_token');
28             const loadMainApp = this.props.loadMainApp;
29             this.props.dispatch(saveApiToken(apiToken));
30             this.props.dispatch<any>(getUserDetails()).then((user: User) => {
31                 this.props.dispatch(initSessions(this.props.authService, this.props.config, user));
32             }).finally(() => {
33                 if (loadMainApp) {
34                     if (this.props.dispatch(getAccountLinkData())) {
35                         this.props.dispatch(navigateToLinkAccount);
36                     }
37                     else {
38                         this.props.dispatch(navigateToRootProject);
39                     }
40                 }
41             });
42         }
43         render() {
44             return <div />;
45         }
46     }
47 );