16848: Avoids showing API errors when the extra token creation fails.
[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
31 type CssRules = 'link' | 'paper' | 'button' | 'actionButton';
32
33 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
34     link: {
35         color: theme.palette.primary.main,
36         textDecoration: 'none',
37         margin: '0px 4px'
38     },
39     paper: {
40         padding: theme.spacing.unit,
41         marginBottom: theme.spacing.unit * 2,
42         backgroundColor: theme.palette.grey["200"],
43         border: `1px solid ${theme.palette.grey["300"]}`
44     },
45     button: {
46         fontSize: '0.8125rem',
47         fontWeight: 600
48     },
49     actionButton: {
50         boxShadow: 'none',
51         marginTop: theme.spacing.unit * 2,
52         marginBottom: theme.spacing.unit * 2,
53         marginRight: theme.spacing.unit * 2,
54     }
55 });
56
57 type TokenDialogProps = TokenDialogData & WithDialogProps<{}> & WithStyles<CssRules> & DispatchProp;
58
59 export class TokenDialogComponent extends React.Component<TokenDialogProps> {
60     onCopy = (message: string) => {
61         this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
62             message,
63             hideDuration: 2000,
64             kind: SnackbarKind.SUCCESS
65         }));
66     }
67
68     onGetNewToken = async () => {
69         const newToken = await this.props.dispatch<any>(getNewExtraToken());
70         if (newToken) {
71             this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
72                 message: 'New token retrieved',
73                 hideDuration: 2000,
74                 kind: SnackbarKind.SUCCESS
75             }));
76         } else {
77             this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
78                 message: 'Creating new tokens is not allowed',
79                 hideDuration: 2000,
80                 kind: SnackbarKind.WARNING
81             }));
82         }
83     }
84
85     getSnippet = ({ apiHost, token }: TokenDialogData) =>
86         `HISTIGNORE=$HISTIGNORE:'export ARVADOS_API_TOKEN=*'
87 export ARVADOS_API_TOKEN=${token}
88 export ARVADOS_API_HOST=${apiHost}
89 unset ARVADOS_API_HOST_INSECURE`
90
91     render() {
92         const { classes, open, closeDialog, ...data } = this.props;
93
94         return <Dialog
95             open={open}
96             onClose={closeDialog}
97             fullWidth={true}
98             maxWidth='md'>
99             <DialogTitle>Get API Token</DialogTitle>
100             <DialogContent>
101                 <Typography paragraph={true}>
102                     The Arvados API token is a secret key that enables the Arvados SDKs to access Arvados with the proper permissions.
103                         <Typography component='span'>
104                             For more information see
105                             <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' className={classes.link}>
106                                 Getting an API token.
107                             </a>
108                         </Typography>
109                 </Typography>
110                 <Typography paragraph={true}>
111                     Paste the following lines at a shell prompt to set up the necessary environment for Arvados SDKs to authenticate to your account.
112                 </Typography>
113                 <DefaultCodeSnippet lines={[this.getSnippet(data)]} />
114                 <CopyToClipboard text={this.getSnippet(data)} onCopy={() => this.onCopy('Token copied to clipboard')}>
115                     <Button
116                         color="primary"
117                         size="small"
118                         variant="contained"
119                         className={classes.actionButton}
120                     >
121                         COPY TO CLIPBOARD
122                     </Button>
123                 </CopyToClipboard>
124                 <Button
125                     onClick={() => this.onGetNewToken()}
126                     color="primary"
127                     size="small"
128                     variant="contained"
129                     className={classes.actionButton}
130                 >
131                     GET NEW TOKEN
132                 </Button>
133                 <Typography >
134                     Arvados
135                             <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' className={classes.link}>virtual machines</a>
136                     do this for you automatically. This setup is needed only when you use the API remotely (e.g., from your own workstation).
137                 </Typography>
138             </DialogContent>
139             <DialogActions>
140                 <Button onClick={closeDialog} className={classes.button} color="primary">CLOSE</Button>
141             </DialogActions>
142         </Dialog>;
143     }
144 }
145
146 export const TokenDialog =
147     withStyles(styles)(
148         connect(getTokenDialogData)(
149             withDialog(TOKEN_DIALOG_NAME)(TokenDialogComponent)));
150