import React, {useState, useEffect} from 'react'; import Paper from '@material-ui/core/Paper'; import Button from '@material-ui/core/Button'; import Divider from '@material-ui/core/Divider'; import TextField from '@material-ui/core/TextField'; //const tmpdata = { // "username": "frikky", // "firstname": "fred", // "lastname": "ode", // "title": "topkek", // "companyname": "company here", // "email": "your email pls", // "phone": "PHONE!!", //} // FIXME - add fetch for data fields // FIXME - remove tmpdata // FIXME: Use isLoggedIn :) const Settings = (props) => { const { globalUrl, isLoaded, userdata, surfaceColor, inputColor } = props; const [username, setUsername] = useState(""); const [firstname, setFirstname] = useState(""); const [lastname, setLastname] = useState(""); const [title, setTitle] = useState(""); const [companyname, setCompanyname] = useState(""); const [email, setEmail] = useState(""); const [phone, setPhone] = useState(""); const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [newPassword2, setNewPassword2] = useState(""); // Used for error messages etc const [formMessage, ] = useState(""); const [passwordFormMessage, setPasswordFormMessage] = useState(""); const [firstrequest, setFirstRequest] = useState(true) const [userInfo, ] = useState(userdata) const [userSettings, setUserSettings] = useState({}) const bodyDivStyle = { margin: "auto", textAlign: "center", width: "1100px", } const boxStyle = { flex: "1", marginLeft: "10px", marginRight: "10px", paddingLeft: "30px", paddingRight: "30px", paddingBottom: "30px", paddingTop: "30px", backgroundColor: surfaceColor, display: "flex", flexDirection: "column" } const onPasswordChange = () => { const data = {"currentpassword": currentPassword, "newpassword": newPassword, "newpassword2": newPassword2} const url = globalUrl+'/api/v1/passwordchange'; fetch(url, { mode: 'cors', method: 'POST', body: JSON.stringify(data), credentials: 'include', crossDomain: true, withCredentials: true, headers: { 'Content-Type': 'application/json; charset=utf-8', }, }) .then(response => response.json().then(responseJson => { if (responseJson["success"] === false) { setPasswordFormMessage(responseJson["reason"]) } }), ) .catch(error => { setPasswordFormMessage("Something went wrong.") }); } const generateApikey = () => { fetch(globalUrl+"/api/v1/generateapikey", { method: 'GET', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', }, credentials: "include", }) .then((response) => { if (response.status !== 200) { console.log("Status not 200 for WORKFLOW EXECUTION :O!") } return response.json() }) .then((responseJson) => { setUserSettings(responseJson) }) .catch(error => { console.log(error) }); } const getSettings = () => { fetch(globalUrl+"/api/v1/getsettings", { method: 'GET', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', }, credentials: "include", }) .then((response) => { if (response.status !== 200) { console.log("Status not 200 for WORKFLOW EXECUTION :O!") } return response.json() }) .then((responseJson) => { console.log(responseJson) setUserSettings(responseJson) }) .catch(error => { console.log(error) }); } // Gotta be a better way of doing this rofl const setFields = () => { if (userInfo.username !== undefined) { if (userInfo.username.length > 0) { setUsername(userInfo.username) } if (userInfo.firstname.length > 0) { setFirstname(userInfo.firstname) } if (userInfo.lastname.length > 0) { setLastname(userInfo.lastname) } if (userInfo.title.length > 0) { setTitle(userInfo.title) } if (userInfo.companyname.length > 0) { setCompanyname(userInfo.companyname) } if (userInfo.phone.length > 0) { setPhone(userInfo.phone) } if (userInfo.email.length > 0) { setEmail(userInfo.email) } } } // This should "always" have data useEffect(() => { if (firstrequest) { setFirstRequest(false) getSettings() } if (Object.getOwnPropertyNames(userInfo).length > 0 && (username === "" && email === "")) { setFields() } }) // Random names for type & autoComplete. Didn't research :^) const landingpageData =

APIKEY

Settings

setUsername(e.target.value)} />
setFirstname(e.target.value)} /> setLastname(e.target.value)} />
setTitle(e.target.value)} /> setCompanyname(e.target.value)} />
setEmail(e.target.value)} /> setPhone(e.target.value)} />

{formMessage}

Password

setCurrentPassword(e.target.value)} />
setNewPassword(e.target.value)} /> setNewPassword2(e.target.value)} />

{passwordFormMessage}

const loadedCheck = isLoaded && !firstrequest ?
{landingpageData}
:
return(
{loadedCheck}
) } export default Settings;