import React, { useEffect} from 'react'; import Paper from '@material-ui/core/Paper'; import List from '@material-ui/core/List'; import Divider from '@material-ui/core/Divider'; import TextField from '@material-ui/core/TextField'; import ListItem from '@material-ui/core/ListItem'; import Button from '@material-ui/core/Button'; import Tabs from '@material-ui/core/Tabs'; import Tab from '@material-ui/core/Tab'; import { useAlert } from "react-alert"; import Dialog from '@material-ui/core/Dialog'; import DialogTitle from '@material-ui/core/DialogTitle'; import DialogActions from '@material-ui/core/DialogActions'; import DialogContent from '@material-ui/core/DialogContent'; const surfaceColor = "#27292D" const inputColor = "#383B40" const Admin = (props) => { const { globalUrl, } = props; const [firstRequest, setFirstRequest] = React.useState(true); const [modalUser, setModalUser] = React.useState({}); const [modalOpen, setModalOpen] = React.useState(false); const [loginInfo, setLoginInfo] = React.useState(""); const [curTab, setCurTab] = React.useState(0); const [users, setUsers] = React.useState([]); const [environments, setEnvironments] = React.useState([]); const alert = useAlert() const submitUser = (data) => { // FIXME - add some check here ROFL console.log("INPUT: ", data) // Just use this one? var data = {"username": data.Username, "password": data.Password} var baseurl = globalUrl const url = baseurl+'/api/v1/register'; fetch(url, { method: 'POST', credentials: "include", body: JSON.stringify(data), headers: { 'Content-Type': 'application/json', }, }) .then(response => response.json().then(responseJson => { if (responseJson["success"] === false) { setLoginInfo("Error in input: "+responseJson.reason) } else { setLoginInfo("") setModalOpen(false) getUsers() } }), ) .catch(error => { console.log("Error in userdata: ", error) }); } const deleteEnvironment = (name) => { // FIXME - add some check here ROFL var newEnv = [] for (var key in environments) { if (environments[key].Name == name) { continue } newEnv.push(environments[key]) } // Just use this one? const url = globalUrl+'/api/v1/setenvironments'; fetch(url, { method: 'PUT', credentials: "include", body: JSON.stringify(newEnv), headers: { 'Content-Type': 'application/json', }, }) .then(response => response.json().then(responseJson => { if (responseJson["success"] === false) { alert.error(responseJson.reason) } else { setLoginInfo("") setModalOpen(false) getEnvironments() } }), ) //.catch(error => { // console.log("Error in userdata: ", error) //}); } const submitEnvironment = (data) => { // FIXME - add some check here ROFL environments.push({"name": data.environment, "type": "onprem"}) // Just use this one? var baseurl = globalUrl const url = baseurl+'/api/v1/setenvironments'; fetch(url, { method: 'PUT', credentials: "include", body: JSON.stringify(environments), headers: { 'Content-Type': 'application/json', }, }) .then(response => response.json().then(responseJson => { if (responseJson["success"] === false) { setLoginInfo("Error in input: "+responseJson.reason) } else { setLoginInfo("") setModalOpen(false) getEnvironments() } }), ) .catch(error => { console.log("Error in userdata: ", error) }); } const getEnvironments = () => { fetch(globalUrl+"/api/v1/getenvironments", { method: 'GET', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', }, credentials: "include", }) .then((response) => { if (response.status !== 200) { console.log("Status not 200 for apps :O!") return } return response.json() }) .then((responseJson) => { setEnvironments(responseJson) }) .catch(error => { alert.error(error.toString()) }); } const getUsers = () => { fetch(globalUrl+"/api/v1/getusers", { method: 'GET', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', }, credentials: "include", }) .then((response) => { if (response.status !== 200) { console.log("Status not 200 for apps :O!") return } return response.json() }) .then((responseJson) => { setUsers(responseJson) }) .catch(error => { alert.error(error.toString()) }); } if (firstRequest) { setFirstRequest(false) getUsers() } const paperStyle = { minWidth: "100%", maxWidth: "100%", color: "white", backgroundColor: surfaceColor, marginBottom: 10, padding: 20, } const changeModalData = (field, value) => { modalUser[field] = value } const modalView = {setModalOpen(false)}} PaperProps={{ style: { backgroundColor: surfaceColor, color: "white", minWidth: "800px", minHeight: "320px", }, }} > Add user {curTab === 0 ?
Username changeModalData("Username", event.target.value)} /> Password changeModalData("Password", event.target.value)} />
: curTab === 1 ?
Environment Name changeModalData("environment", event.target.value)} />
: null } {loginInfo}
const usersView = curTab === 0 ?

User management

{users === undefined ? null : users.map(data => { console.log(data) return ( {data.Username} ) })}
: null const environmentView = curTab === 1 ?

Environments

{environments === undefined ? null : environments.map(environment => { return ( - {environment.Name} ) })}
: null const setConfig = (event, newValue) => { if (newValue === 1) { getEnvironments() } setModalUser({}) setCurTab(newValue) } const data =
{usersView} {environmentView}
return (
{modalView} {data}
) } export default Admin