b282311814a7f2650bbc2ff9cd99de657debbf60
[arvados.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
15 interface ApiTokenProps {
16     authService: AuthService;
17     config: Config;
18     loadMainApp: boolean;
19 }
20
21 export const ApiToken = connect()(
22     class extends React.Component<ApiTokenProps & RouteProps & DispatchProp<any>, {}> {
23         componentDidMount() {
24             const search = this.props.location ? this.props.location.search : "";
25             const apiToken = getUrlParameter(search, 'api_token');
26             const loadMainApp = this.props.loadMainApp;
27             this.props.dispatch<any>(saveApiToken(apiToken)).finally(() => {
28                 const redirectURL = this.props.authService.getTargetURL();
29
30                 if (loadMainApp) {
31                     if (redirectURL) {
32                         this.props.authService.removeTargetURL();
33                         window.location.href = redirectURL;
34                     }
35                     else if (this.props.dispatch(getAccountLinkData())) {
36                         this.props.dispatch(navigateToLinkAccount);
37                     }
38                     else {
39                         this.props.dispatch(navigateToRootProject);
40                     }
41                 }
42             });
43         }
44         render() {
45             return <div />;
46         }
47     }
48 );