|
| 1 | +import React, { useRef, useState } from 'react'; |
| 2 | + |
| 3 | +import Layout from '@theme/Layout'; |
| 4 | +import Head from '@docusaurus/Head'; |
| 5 | +import { Button, Card, CardActions, CardContent, Divider, Tabs, Typography } from '@mui/material'; |
| 6 | +import { HubExamples } from '../../components/HubFeatures/HubFeatures'; |
| 7 | +import { Editor } from '@monaco-editor/react'; |
| 8 | + |
| 9 | +import Box from '@mui/material/Box'; |
| 10 | +import Tab from '@mui/material/Tab'; |
| 11 | + |
| 12 | +import Grid from '@mui/material/Grid2'; |
| 13 | +import SrmViewer from '../../components/SrmViewer'; |
| 14 | +import GraphComponent from '../../components/Graph/graph'; |
| 15 | +import { LslRequest, LslResponse } from '@site/src/services/models'; |
| 16 | +import LassoService from '@site/src/services/LassoService'; |
| 17 | +import AuthService from '@site/src/services/AuthService'; |
| 18 | +import { useHistory, useLocation } from '@docusaurus/router'; |
| 19 | + |
| 20 | +interface TabPanelProps { |
| 21 | + children?: React.ReactNode; |
| 22 | + index: number; |
| 23 | + value: number; |
| 24 | +} |
| 25 | + |
| 26 | +function CustomTabPanel(props: TabPanelProps) { |
| 27 | + const { children, value, index, ...other } = props; |
| 28 | + |
| 29 | + return ( |
| 30 | + <div |
| 31 | + role="tabpanel" |
| 32 | + hidden={value !== index} |
| 33 | + id={`simple-tabpanel-${index}`} |
| 34 | + aria-labelledby={`simple-tab-${index}`} |
| 35 | + {...other} |
| 36 | + > |
| 37 | + {value === index && <Box sx={{ p: 3 }}>{children}</Box>} |
| 38 | + </div> |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +function a11yProps(index: number) { |
| 43 | + return { |
| 44 | + id: `simple-tab-${index}`, |
| 45 | + 'aria-controls': `simple-tabpanel-${index}`, |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +const SubmitPage = () => { |
| 50 | + const [value, setValue] = React.useState(0); |
| 51 | + |
| 52 | + const handleChange = (event: React.SyntheticEvent, newValue: number) => { |
| 53 | + setValue(newValue); |
| 54 | + }; |
| 55 | + |
| 56 | + const location = useLocation() |
| 57 | + const history = useHistory() |
| 58 | + |
| 59 | + const [currentExampleId, setCurrentExampleId] = useState(location.search.split('=')[1]) |
| 60 | + |
| 61 | + const [lsl, setLsl] = useState("") |
| 62 | + |
| 63 | + const handleSubmit = () => { |
| 64 | + let lslRequest = new LslRequest() |
| 65 | + |
| 66 | + // get current value |
| 67 | + lslRequest.script = editorRef.current.getModel().getValue() |
| 68 | + const user = JSON.parse(localStorage.getItem('user') || '{}'); |
| 69 | + lslRequest.email = user.email; |
| 70 | + lslRequest.type = null //draft ? "DRAFT" : null; |
| 71 | + console.log(lslRequest); |
| 72 | + |
| 73 | + const responses = execute(lslRequest) |
| 74 | + }; |
| 75 | + |
| 76 | + const execute = async (lslRequest: LslRequest) => { |
| 77 | + return await Promise.all([AuthService.login("lasso", "lasso").then( |
| 78 | + (response) => { |
| 79 | + // login successful |
| 80 | + console.log("Successfully logged in") |
| 81 | + }, |
| 82 | + (error) => { |
| 83 | + const resMessage = |
| 84 | + (error.response && |
| 85 | + error.response.data && |
| 86 | + error.response.data.message) || |
| 87 | + error.message || |
| 88 | + error.toString(); |
| 89 | + |
| 90 | + // FIXME |
| 91 | + console.log("Login attempt failed " + error) |
| 92 | + } |
| 93 | + ), LassoService.execute(lslRequest).then( |
| 94 | + (response) => { |
| 95 | + let lslResponse: LslResponse = response.data |
| 96 | + console.log("Successfully executed. Execution ID is " + lslResponse.executionId) |
| 97 | + |
| 98 | + // redirect to result page |
| 99 | + history.push(`./result?executionId=${lslResponse.executionId}`) |
| 100 | + }, |
| 101 | + (error) => { |
| 102 | + const resMessage = |
| 103 | + (error.response && |
| 104 | + error.response.data && |
| 105 | + error.response.data.message) || |
| 106 | + error.message || |
| 107 | + error.toString(); |
| 108 | + |
| 109 | + // FIXME |
| 110 | + console.log("Execute attempt failed " + error) |
| 111 | + } |
| 112 | + )]) |
| 113 | + }; |
| 114 | + |
| 115 | + const editorRef = useRef<any>(null); |
| 116 | + const monacoRef = useRef<any>(null); |
| 117 | + |
| 118 | + function handleEditorDidMount(editor: any, monaco: any) { |
| 119 | + monaco.languages.register({ id: 'java' }); |
| 120 | + |
| 121 | + monacoRef.current = monaco; |
| 122 | + editorRef.current = editor; |
| 123 | + |
| 124 | + if (editorRef.current && editorRef.current.getModel() && currentExampleId) { |
| 125 | + editorRef.current.getModel().setValue(HubExamples.MAP[currentExampleId].lsl); |
| 126 | + } else { |
| 127 | + editorRef.current.getModel().setValue(""); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return ( |
| 132 | + <Layout> |
| 133 | + <Head> |
| 134 | + <title>LSL Pipeline Editor</title> |
| 135 | + <meta name="description" content="A hub for TDSEs" /> |
| 136 | + </Head> |
| 137 | + |
| 138 | + <Typography sx={{ margin: 2 }} variant="h5" component="div">Editor<Typography variant="h6" component="div">Write and Submit a LSL Script to the Public LASSO Demo Platform</Typography></Typography> |
| 139 | + |
| 140 | + <Grid container spacing={2}> |
| 141 | + <Grid size={12}> |
| 142 | + <Box sx={{ width: '100%' }}> |
| 143 | + <Box sx={{ borderBottom: 1, borderColor: 'divider' }}> |
| 144 | + <Tabs value={value} onChange={handleChange} aria-label="basic tabs example"> |
| 145 | + <Tab label="LSL Pipeline" {...a11yProps(0)} /> |
| 146 | + {/* <Tab label="Graph Viewer" {...a11yProps(1)} /> */} |
| 147 | + </Tabs> |
| 148 | + </Box> |
| 149 | + <CustomTabPanel value={value} index={0}> |
| 150 | + <><CardContent> |
| 151 | + <Typography sx={{ margin: 2 }} variant="h5" component="div">LSL Pipeline Editor</Typography> |
| 152 | + <Typography variant="h5" component="div"> |
| 153 | + <Editor |
| 154 | + height="500px" |
| 155 | + defaultLanguage="java" |
| 156 | + defaultValue={lsl} |
| 157 | + onMount={handleEditorDidMount} /> |
| 158 | + </Typography> |
| 159 | + <Button sx={{ float: "left" }} onClick={(event) => handleSubmit()}>Submit</Button> |
| 160 | + |
| 161 | + </CardContent><CardActions> |
| 162 | + </CardActions></> |
| 163 | + </CustomTabPanel> |
| 164 | + {/* <CustomTabPanel value={value} index={1}> |
| 165 | + <Typography variant="h5" component="div"> |
| 166 | + <GraphComponent exampleId={currentExampleId} /> |
| 167 | + </Typography> |
| 168 | + </CustomTabPanel> */} |
| 169 | + </Box> |
| 170 | + |
| 171 | + |
| 172 | + </Grid> |
| 173 | + </Grid> |
| 174 | + |
| 175 | + </Layout> |
| 176 | + ); |
| 177 | +} |
| 178 | + |
| 179 | +export default SubmitPage; |
0 commit comments