init metadata
[arvados.git] / src / views-components / advanced-tab-dialog / advanced-tab-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 { Dialog, DialogActions, Button, StyleRulesCallback, WithStyles, withStyles, DialogTitle, DialogContent, Tabs, Tab, DialogContentText } from '@material-ui/core';
7 import { WithDialogProps } from '~/store/dialog/with-dialog';
8 import { withDialog } from "~/store/dialog/with-dialog";
9 import { compose } from 'redux';
10 import { ADVANCED_TAB_DIALOG } from "~/store/advanced-tab/advanced-tab";
11 import { DefaultCodeSnippet } from "~/components/default-code-snippet/default-code-snippet";
12 import { MetadataTab } from '~/views-components/advanced-tab-dialog/metadataTab';
13
14 type CssRules = 'content' | 'codeSnippet' | 'spacing';
15
16 const styles: StyleRulesCallback<CssRules> = theme => ({
17     content: {
18         paddingTop: theme.spacing.unit * 3,
19         minHeight: '400px',
20         minWidth: '1232px'
21     },
22     codeSnippet: {
23         borderRadius: theme.spacing.unit * 0.5,
24         border: '1px solid',
25         borderColor: theme.palette.grey["400"],
26         maxHeight: '400px'
27     },
28     spacing: {
29         paddingBottom: theme.spacing.unit * 2
30     },
31 });
32
33 export const AdvancedTabDialog = compose(
34     withDialog(ADVANCED_TAB_DIALOG),
35     withStyles(styles),
36 )(
37     class extends React.Component<WithDialogProps<any> & WithStyles<CssRules>>{
38         state = {
39             value: 0,
40         };
41
42         componentDidMount() {
43             this.setState({ value: 0 });
44         }
45
46         handleChange = (event: React.MouseEvent<HTMLElement>, value: number) => {
47             this.setState({ value });
48         }
49         render() {
50             const { classes, open, closeDialog } = this.props;
51             const { value } = this.state;
52             const {
53                 apiResponse,
54                 metadata,
55                 pythonHeader,
56                 pythonExample,
57                 cliGetHeader,
58                 cliGetExample,
59                 cliUpdateHeader,
60                 cliUpdateExample,
61                 curlHeader,
62                 curlExample
63             } = this.props.data;
64             return <Dialog
65                 open={open}
66                 maxWidth="lg"
67                 onClose={closeDialog}
68                 onExit={() => this.setState({ value: 0 })} >
69                 <DialogTitle>Advanced</DialogTitle>
70                 <Tabs value={value} onChange={this.handleChange} fullWidth>
71                     <Tab label="API RESPONSE" />
72                     <Tab label="METADATA" />
73                     <Tab label="PYTHON EXAMPLE" />
74                     <Tab label="CLI EXAMPLE" />
75                     <Tab label="CURL EXAMPLE" />
76                 </Tabs>
77                 <DialogContent className={classes.content}>
78                     {value === 0 && <div>{dialogContentExample(apiResponse, classes)}</div>}
79                     {value === 1 && <div>{metadata.items.length > 0 ? <MetadataTab items={metadata.items}/>: dialogContentHeader('(No metadata links found)')}</div>}
80                     {value === 2 && dialogContent(pythonHeader, pythonExample, classes)}
81                     {value === 3 && <div>
82                         {dialogContent(cliGetHeader, cliGetExample, classes)}
83                         {dialogContent(cliUpdateHeader, cliUpdateExample, classes)}
84                     </div>}
85                     {value === 4 && dialogContent(curlHeader, curlExample, classes)}
86                 </DialogContent>
87                 <DialogActions>
88                     <Button variant='flat' color='primary' onClick={closeDialog}>
89                         Close
90                     </Button>
91                 </DialogActions>
92             </Dialog>;
93         }
94     }
95 );
96
97 const dialogContent = (header: string, example: string, classes: any) =>
98     <div className={classes.spacing}>
99         {dialogContentHeader(header)}
100         {dialogContentExample(example, classes)}
101     </div>;
102
103 const dialogContentHeader = (header: string) =>
104     <DialogContentText>
105         {header}
106     </DialogContentText>;
107
108 const dialogContentExample = (example: string, classes: any) =>
109     <DefaultCodeSnippet
110         className={classes.codeSnippet}
111         lines={[example]} />;