548dfe772291690adbb5b728618033c04af6fcf1
[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 { 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 { Config } from "~/common/config";
13 import { getAccountLinkData } from "~/store/link-account-panel/link-account-panel-actions";
14 import { replace } from "react-router-redux";
15
16 interface ApiTokenProps {
17     authService: AuthService;
18     config: Config;
19     loadMainApp: boolean;
20 }
21
22 export const ApiToken = connect()(
23     class extends React.Component<ApiTokenProps & RouteProps & DispatchProp<any>, {}> {
24         componentDidMount() {
25             const search = this.props.location ? this.props.location.search : "";
26             const apiToken = getUrlParameter(search, 'api_token');
27             const loadMainApp = this.props.loadMainApp;
28             this.props.dispatch<any>(saveApiToken(apiToken)).finally(() => {
29                 const redirectURL = this.props.authService.getTargetURL();
30
31                 setTimeout(() => {
32                     if (loadMainApp) {
33                         if (redirectURL) {
34                             this.props.authService.removeTargetURL();
35                             this.props.dispatch(replace(redirectURL));
36                         }
37                         else if (this.props.dispatch(getAccountLinkData())) {
38                             this.props.dispatch(navigateToLinkAccount);
39                         }
40                         else {
41                             this.props.dispatch(navigateToRootProject);
42                         }
43                     }
44                 }, 0);
45             });
46         }
47         render() {
48             return <div />;
49         }
50     }
51 );