16848: Removed commented out code.
[arvados-workbench2.git] / src / views-components / token-dialog / token-dialog.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import {
7     Dialog,
8     DialogActions,
9     DialogTitle,
10     DialogContent,
11     WithStyles,
12     withStyles,
13     StyleRulesCallback,
14     Button,
15     Typography
16 } from '@material-ui/core';
17 import * as CopyToClipboard from 'react-copy-to-clipboard';
18 import { ArvadosTheme } from '~/common/custom-theme';
19 import { withDialog } from '~/store/dialog/with-dialog';
20 import { WithDialogProps } from '~/store/dialog/with-dialog';
21 import { connect, DispatchProp } from 'react-redux';
22 import {
23     TokenDialogData,
24     getTokenDialogData,
25     TOKEN_DIALOG_NAME,
26 } from '~/store/token-dialog/token-dialog-actions';
27 import { DefaultCodeSnippet } from '~/components/default-code-snippet/default-code-snippet';
28 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
29 import { getNewExtraToken } from '~/store/auth/auth-action';
30 import { DetailsAttributeComponent } from '~/components/details-attribute/details-attribute';
31
32 type CssRules = 'link' | 'paper' | 'button' | 'actionButton' | 'codeBlock';
33
34 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
35     link: {
36         color: theme.palette.primary.main,
37         textDecoration: 'none',
38         margin: '0px 4px'
39     },
40     paper: {
41         padding: theme.spacing.unit,
42         marginBottom: theme.spacing.unit * 2,
43         backgroundColor: theme.palette.grey["200"],
44         border: `1px solid ${theme.palette.grey["300"]}`
45     },
46     button: {
47         fontSize: '0.8125rem',
48         fontWeight: 600
49     },
50     actionButton: {
51         boxShadow: 'none',
52         marginTop: theme.spacing.unit * 2,
53         marginBottom: theme.spacing.unit * 2,
54         marginRight: theme.spacing.unit * 2,
55     },
56     codeBlock: {
57         fontSize: '0.8125rem',
58     },
59 });
60
61 type TokenDialogProps = TokenDialogData & WithDialogProps<{}> & WithStyles<CssRules> & DispatchProp;
62
63 export class TokenDialogComponent extends React.Component<TokenDialogProps> {
64     onCopy = (message: string) => {
65         this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
66             message,
67             hideDuration: 2000,
68             kind: SnackbarKind.SUCCESS
69         }));
70     }
71
72     onGetNewToken = async () => {
73         const newToken = await this.props.dispatch<any>(getNewExtraToken());
74         if (newToken) {
75             this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
76                 message: 'New token retrieved',
77                 hideDuration: 2000,
78                 kind: SnackbarKind.SUCCESS
79             }));
80         } else {
81             this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
82                 message: 'Creating new tokens is not allowed',
83                 hideDuration: 2000,
84                 kind: SnackbarKind.WARNING
85             }));
86         }
87     }
88
89     getSnippet = ({ apiHost, token }: TokenDialogData) =>
90         `HISTIGNORE=$HISTIGNORE:'export ARVADOS_API_TOKEN=*'
91 export ARVADOS_API_TOKEN=${token}
92 export ARVADOS_API_HOST=${apiHost}
93 unset ARVADOS_API_HOST_INSECURE`
94
95     render() {
96         const { classes, open, closeDialog, ...data } = this.props;
97         const tokenExpiration = data.tokenExpiration
98             ? data.tokenExpiration.toLocaleString()
99             : `This token does not have an expiration date`;
100
101         return <Dialog
102             open={open}
103             onClose={closeDialog}
104             fullWidth={true}
105             maxWidth='md'>
106             <DialogTitle>Get API Token</DialogTitle>
107             <DialogContent>
108                 <Typography paragraph={true}>
109                     The Arvados API token is a secret key that enables the Arvados SDKs to access Arvados with the proper permissions.
110                     <Typography component='span'>
111                         For more information see
112                         <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' className={classes.link}>
113                             Getting an API token.
114                         </a>
115                     </Typography>
116                 </Typography>
117
118                 <DetailsAttributeComponent label='API Host' value={data.apiHost} copyValue={data.apiHost} onCopy={this.onCopy} />
119                 <DetailsAttributeComponent label='API Token' value={data.token} copyValue={data.token} onCopy={this.onCopy} />
120                 <DetailsAttributeComponent label='Token expiration' value={tokenExpiration} />
121                 { this.props.canCreateNewTokens && <Button
122                     onClick={() => this.onGetNewToken()}
123                     color="primary"
124                     size="small"
125                     variant="contained"
126                     className={classes.actionButton}
127                 >
128                     GET NEW TOKEN
129                 </Button> }
130
131                 <Typography paragraph={true}>
132                     Paste the following lines at a shell prompt to set up the necessary environment for Arvados SDKs to authenticate to your account.
133                 </Typography>
134                 <DefaultCodeSnippet className={classes.codeBlock} lines={[this.getSnippet(data)]} />
135                 <CopyToClipboard text={this.getSnippet(data)} onCopy={() => this.onCopy('Shell code block copied')}>
136                     <Button
137                         color="primary"
138                         size="small"
139                         variant="contained"
140                         className={classes.actionButton}
141                     >
142                         COPY TO CLIPBOARD
143                     </Button>
144                 </CopyToClipboard>
145                 <Typography>
146                     Arvados
147                             <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' className={classes.link}>virtual machines</a>
148                     do this for you automatically. This setup is needed only when you use the API remotely (e.g., from your own workstation).
149                 </Typography>
150             </DialogContent>
151             <DialogActions>
152                 <Button onClick={closeDialog} className={classes.button} color="primary">CLOSE</Button>
153             </DialogActions>
154         </Dialog>;
155     }
156 }
157
158 export const TokenDialog =
159     withStyles(styles)(
160         connect(getTokenDialogData)(
161             withDialog(TOKEN_DIALOG_NAME)(TokenDialogComponent)));
162