6fe3eee2aeb6a5425615ae9dec713ec6d4b57bb7
[arvados-workbench2.git] / src / views / login-panel / login-panel.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 { connect, DispatchProp } from 'react-redux';
7 import { Grid, Typography, Button, Select } from '@material-ui/core';
8 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
9 import { login, authActions } from '~/store/auth/auth-action';
10 import { ArvadosTheme } from '~/common/custom-theme';
11 import { RootState } from '~/store/store';
12
13 type CssRules = 'root' | 'container' | 'title' | 'content' | 'content__bolder' | 'button';
14
15 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
16     root: {
17         position: 'relative',
18         backgroundColor: theme.palette.grey["200"],
19         '&::after': {
20             content: `''`,
21             position: 'absolute',
22             top: 0,
23             left: 0,
24             bottom: 0,
25             right: 0,
26             opacity: 0.2,
27         }
28     },
29     container: {
30         width: '560px',
31         zIndex: 10
32     },
33     title: {
34         marginBottom: theme.spacing.unit * 6,
35         color: theme.palette.grey["800"]
36     },
37     content: {
38         marginBottom: theme.spacing.unit * 3,
39         lineHeight: '1.2rem',
40         color: theme.palette.grey["800"]
41     },
42     'content__bolder': {
43         fontWeight: 'bolder'
44     },
45     button: {
46         boxShadow: 'none'
47     }
48 });
49
50 type LoginPanelProps = DispatchProp<any> & WithStyles<CssRules> & {
51     remoteHosts: { [key: string]: string },
52     homeCluster: string,
53     uuidPrefix: string,
54     loginCluster: string,
55     welcomePage: string
56 };
57
58 export const LoginPanel = withStyles(styles)(
59     connect((state: RootState) => ({
60         remoteHosts: state.auth.remoteHosts,
61         homeCluster: state.auth.homeCluster,
62         uuidPrefix: state.auth.localCluster,
63         loginCluster: state.auth.loginCluster,
64         welcomePage: state.auth.config.clusterConfig.Workbench.WelcomePageHTML
65     }))(({ classes, dispatch, remoteHosts, homeCluster, uuidPrefix, loginCluster, welcomePage }: LoginPanelProps) =>
66         <Grid container justify="center" alignItems="center"
67             className={classes.root}
68             style={{ marginTop: 56, overflowY: "auto", height: "100%" }}>
69             <Grid item className={classes.container}>
70                 <Typography>
71                     <div dangerouslySetInnerHTML={{ __html: welcomePage }} style={{ margin: "1em" }} />
72                 </Typography>
73                 {Object.keys(remoteHosts).length > 1 && loginCluster === "" &&
74
75                     <Typography component="div" align="right">
76                         <label>Please select the cluster that hosts your user account:</label>
77                         <Select native value={homeCluster} style={{ margin: "1em" }}
78                             onChange={(event) => dispatch(authActions.SET_HOME_CLUSTER(event.target.value))}>
79                             {Object.keys(remoteHosts).map((k) => <option key={k} value={k}>{k}</option>)}
80                         </Select>
81                     </Typography>}
82
83                 <Typography component="div" align="right">
84                     <Button variant="contained" color="primary" style={{ margin: "1em" }} className={classes.button}
85                         onClick={() => dispatch(login(uuidPrefix, homeCluster, loginCluster, remoteHosts))}>
86                         Log in
87                         {uuidPrefix !== homeCluster && loginCluster !== homeCluster &&
88                             <span>&nbsp;to {uuidPrefix} with user from {homeCluster}</span>}
89                     </Button>
90                 </Typography>
91             </Grid>
92         </Grid >
93     ));