5f9ce08c9682ab820cfabe2f2e10b0737c58c130
[arvados.git] / src / views / site-manager-panel / site-manager-panel-root.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     Card,
8     CardContent,
9     CircularProgress,
10     Grid,
11     StyleRulesCallback,
12     Table,
13     TableBody,
14     TableCell,
15     TableHead,
16     TableRow,
17     Typography,
18     WithStyles,
19     withStyles
20 } from '@material-ui/core';
21 import { ArvadosTheme } from '~/common/custom-theme';
22 import { Session, SessionStatus } from "~/models/session";
23 import Button from "@material-ui/core/Button";
24 import { compose } from "redux";
25 import { Field, FormErrors, InjectedFormProps, reduxForm, reset, stopSubmit } from "redux-form";
26 import { TextField } from "~/components/text-field/text-field";
27 import { addSession } from "~/store/auth/auth-action-session";
28 import { SITE_MANAGER_REMOTE_HOST_VALIDATION } from "~/validators/validators";
29
30 type CssRules = 'root' | 'link' | 'buttonContainer' | 'table' | 'tableRow' |
31     'remoteSiteInfo' | 'buttonAdd' | 'buttonLoggedIn' | 'buttonLoggedOut' |
32     'statusCell';
33
34 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
35     root: {
36        width: '100%',
37        overflow: 'auto'
38     },
39     link: {
40         color: theme.palette.primary.main,
41         textDecoration: 'none',
42         margin: '0px 4px'
43     },
44     buttonContainer: {
45         textAlign: 'right'
46     },
47     table: {
48         marginTop: theme.spacing.unit
49     },
50     tableRow: {
51         '& td, th': {
52             whiteSpace: 'nowrap'
53         }
54     },
55     statusCell: {
56         minWidth: 160
57     },
58     remoteSiteInfo: {
59         marginTop: 20
60     },
61     buttonAdd: {
62         marginLeft: 10,
63         marginTop: theme.spacing.unit * 3
64     },
65     buttonLoggedIn: {
66         minHeight: theme.spacing.unit,
67         padding: 5,
68         color: '#fff',
69         backgroundColor: '#009966',
70         '&:hover': {
71             backgroundColor: '#008450',
72         }
73     },
74     buttonLoggedOut: {
75         minHeight: theme.spacing.unit,
76         padding: 5,
77         color: '#000',
78         backgroundColor: '#FFC414',
79         '&:hover': {
80             backgroundColor: '#eaaf14',
81         }
82     }
83 });
84
85 export interface SiteManagerPanelRootActionProps {
86     toggleSession: (session: Session) => void;
87 }
88
89 export interface SiteManagerPanelRootDataProps {
90     sessions: Session[];
91 }
92
93 type SiteManagerPanelRootProps = SiteManagerPanelRootDataProps & SiteManagerPanelRootActionProps & WithStyles<CssRules> & InjectedFormProps;
94 const SITE_MANAGER_FORM_NAME = 'siteManagerForm';
95
96 export const SiteManagerPanelRoot = compose(
97     reduxForm<{remoteHost: string}>({
98         form: SITE_MANAGER_FORM_NAME,
99         touchOnBlur: false,
100         onSubmit: (data, dispatch) => {
101             dispatch<any>(addSession(data.remoteHost)).then(() => {
102                 dispatch(reset(SITE_MANAGER_FORM_NAME));
103             }).catch((e: any) => {
104                 const errors = {
105                     remoteHost: e
106                 } as FormErrors;
107                 dispatch(stopSubmit(SITE_MANAGER_FORM_NAME, errors));
108             });
109         }
110     }),
111     withStyles(styles))
112     (({ classes, sessions, handleSubmit, toggleSession }: SiteManagerPanelRootProps) =>
113         <Card className={classes.root}>
114             <CardContent>
115                 <Grid container direction="row">
116                     <Grid item xs={12}>
117                         <Typography variant='body1' paragraph={true} >
118                             You can log in to multiple Arvados sites here, then use the multi-site search page to search collections and projects on all sites at once.
119                         </Typography>
120                     </Grid>
121                 </Grid>
122                 <Grid item xs={12}>
123                     {sessions.length > 0 && <Table className={classes.table}>
124                         <TableHead>
125                             <TableRow className={classes.tableRow}>
126                                 <TableCell>Cluster ID</TableCell>
127                                 <TableCell>Username</TableCell>
128                                 <TableCell>Email</TableCell>
129                                 <TableCell>Status</TableCell>
130                             </TableRow>
131                         </TableHead>
132                         <TableBody>
133                             {sessions.map((session, index) => {
134                                 const validating = session.status === SessionStatus.BEING_VALIDATED;
135                                 return <TableRow key={index} className={classes.tableRow}>
136                                     <TableCell>{session.clusterId}</TableCell>
137                                     <TableCell>{validating ? <CircularProgress size={20}/> : session.username}</TableCell>
138                                     <TableCell>{validating ? <CircularProgress size={20}/> : session.email}</TableCell>
139                                     <TableCell className={classes.statusCell}>
140                                         <Button fullWidth
141                                             disabled={validating || session.status === SessionStatus.INVALIDATED || session.active}
142                                             className={session.loggedIn ? classes.buttonLoggedIn : classes.buttonLoggedOut}
143                                             onClick={() => toggleSession(session)}>
144                                             {validating ? "Validating" : (session.loggedIn ? "Logged in" : "Logged out")}
145                                         </Button>
146                                     </TableCell>
147                                 </TableRow>;
148                             })}
149                         </TableBody>
150                     </Table>}
151                 </Grid>
152                 <form onSubmit={handleSubmit}>
153                     <Grid container direction="row">
154                         <Grid item xs={12}>
155                             <Typography variant='body1' paragraph={true} className={classes.remoteSiteInfo}>
156                                 To add a remote Arvados site, paste the remote site's host here (see "ARVADOS_API_HOST" on the "current token" page).
157                             </Typography>
158                         </Grid>
159                         <Grid item xs={8}>
160                             <Field
161                                 name='remoteHost'
162                                 validate={SITE_MANAGER_REMOTE_HOST_VALIDATION}
163                                 component={TextField}
164                                 placeholder="zzzz.arvadosapi.com"
165                                 margin="normal"
166                                 label="New cluster"
167                                 autoFocus/>
168                         </Grid>
169                         <Grid item xs={3}>
170                             <Button type="submit" variant="contained" color="primary"
171                                 className={classes.buttonAdd}>
172                                 {"ADD"}</Button>
173                         </Grid>
174                     </Grid>
175                 </form>
176             </CardContent>
177         </Card>
178     );