15397: Fix usage of deprecated assertRegexpMatches.
[arvados.git] / services / workbench2 / 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 React from "react";
7 import { RootState } from "store/store";
8 import { connect, DispatchProp } from "react-redux";
9 import { saveApiToken } from "store/auth/auth-action";
10 import { getUrlParameter } from "common/url";
11 import { AuthService } from "services/auth-service/auth-service";
12 import { navigateToRootProject, navigateToLinkAccount } from "store/navigation/navigation-action";
13 import { Config } from "common/config";
14 import { getAccountLinkData } from "store/link-account-panel/link-account-panel-actions";
15 import { replace } from "react-router-redux";
16 import { User } from "models/user";
17
18 interface ApiTokenProps {
19     authService: AuthService;
20     config: Config;
21     loadMainApp: boolean;
22     user?: User;
23 }
24
25 export const ApiToken = connect((state: RootState) => ({
26     user: state.auth.user,
27 }), null)(
28     class extends React.Component<ApiTokenProps & RouteProps & DispatchProp<any>, {}> {
29         componentDidMount() {
30             const search = this.props.location ? this.props.location.search : "";
31             const apiToken = getUrlParameter(search, 'api_token');
32             this.props.dispatch<any>(saveApiToken(apiToken));
33         }
34
35         componentDidUpdate() {
36             const redirectURL = this.props.authService.getTargetURL();
37
38             if (this.props.loadMainApp && this.props.user) {
39                 if (redirectURL) {
40                     this.props.authService.removeTargetURL();
41                     this.props.dispatch(replace(redirectURL));
42                 }
43                 else if (this.props.dispatch(getAccountLinkData())) {
44                     this.props.dispatch(navigateToLinkAccount);
45                 }
46                 else {
47                     this.props.dispatch(navigateToRootProject);
48                 }
49             }
50         }
51
52         render() {
53             return <div />;
54         }
55     }
56 );