left-side-panel-small-refactor
[arvados-workbench2.git] / src / 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 { Redirect, RouteProps } from "react-router";
6 import * as React from "react";
7 import { connect, DispatchProp } from "react-redux";
8 import authActions from "../../store/auth/auth-action";
9 import { authService, projectService } from "../../services/services";
10
11 interface ApiTokenProps {
12 }
13
14 class ApiToken extends React.Component<ApiTokenProps & RouteProps & DispatchProp<any>, {}> {
15     static getUrlParameter(search: string, name: string) {
16         const safeName = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
17         const regex = new RegExp('[\\?&]' + safeName + '=([^&#]*)');
18         const results = regex.exec(search);
19         return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
20     }
21
22     componentDidMount() {
23         const search = this.props.location ? this.props.location.search : "";
24         const apiToken = ApiToken.getUrlParameter(search, 'api_token');
25         this.props.dispatch(authActions.SAVE_API_TOKEN(apiToken));
26         this.props.dispatch<any>(authService.getUserDetails()).then(() => {
27             const rootUuid = authService.getRootUuid();
28             this.props.dispatch(projectService.getProjectList(rootUuid));
29         });
30     }
31     render() {
32         return <Redirect to="/"/>;
33     }
34 }
35
36 export default connect()(ApiToken);