From 3bc17e1883778e59f2c719bf5784590f06b1a4a9 Mon Sep 17 00:00:00 2001 From: Hawk Date: Sun, 17 May 2020 00:43:45 +0200 Subject: [PATCH] added change username and pw close #68 --- .../src/components/profile/changePassword.js | 148 ++++++++++++++++++ .../src/components/profile/changeUsername.js | 117 ++++++++++++++ frontend/src/components/profile/profile.js | 43 ++++- 3 files changed, 304 insertions(+), 4 deletions(-) create mode 100644 frontend/src/components/profile/changePassword.js create mode 100644 frontend/src/components/profile/changeUsername.js diff --git a/frontend/src/components/profile/changePassword.js b/frontend/src/components/profile/changePassword.js new file mode 100644 index 0000000..13dd678 --- /dev/null +++ b/frontend/src/components/profile/changePassword.js @@ -0,0 +1,148 @@ +import React, { useEffect } from 'react'; +import Typography from '@material-ui/core/Typography'; +import Grid from '@material-ui/core/Grid'; +import Button from '@material-ui/core/Button'; +import TextField from '@material-ui/core/TextField'; +import LoginFeedback from '../login/loginfeedback'; +import Box from '@material-ui/core/Box'; + +export default function ChangePassword(props) { + const url = "/simcompanies/API/user/setpassword" + const [password, setPassword] = React.useState(""); + const [passwordNew, setPasswordNew] = React.useState(""); + const [passwordNew2, setPasswordNew2] = React.useState(""); + const [disabled, setDisabled] = React.useState(false); + const [error, setError] = React.useState(false); + const [statusText, setStatusText] = React.useState(null); + const [status, setStatus] = React.useState(null); + + const postSetPassword = async (url, data) => { + + const response = await fetch(url, { + method: 'POST', + mode: 'cors', + cache: 'no-cache', + headers: { + 'Content-Type': 'application/json' + }, + redirect: 'follow', + referrerPolicy: 'no-referrer', + body: JSON.stringify(data) + }); + + const text = await response.text() + await setStatusText(text) + setStatus(response.status) + setDisabled(false) + setTimeout(() => { setStatus(null) }, 2000) + } + + const handleClick = (e) => { + + if (passwordNew !== "" && password !== "" && passwordNew2 === passwordNew) { + setDisabled(true) + postSetPassword(url, { oldpassword: password, newpassword: passwordNew }) + + setPassword("") + setPasswordNew("") + setPasswordNew2("") + } + } + + const handleKeyDown = (e) => { + if (e.key === "Enter") { + if (passwordNew !== "" && password !== "" && passwordNew2 === passwordNew) { + setDisabled(true) + postSetPassword(url, { oldpassword: password, newpassword: passwordNew }) + + setPassword("") + setPasswordNew("") + setPasswordNew2("") + } + } + } + useEffect(() => { + if (passwordNew2 === passwordNew) { + setError(false) + } else { + setError(true) + } + }, [passwordNew, passwordNew2, setError]) + + const handleChangePWNew = (e) => { + setPasswordNew(e.target.value) + } + const handleChangePWNew2 = (e) => { + setPasswordNew2(e.target.value) + } + const handleChangePW = (e) => { + setPassword(e.target.value) + } + return ( + + + + + Change Password + + + + + + + + + + + + + + + + + + + + + + + + + + ); +} diff --git a/frontend/src/components/profile/changeUsername.js b/frontend/src/components/profile/changeUsername.js new file mode 100644 index 0000000..cdfe03f --- /dev/null +++ b/frontend/src/components/profile/changeUsername.js @@ -0,0 +1,117 @@ +import React from 'react'; +import Typography from '@material-ui/core/Typography'; +import Grid from '@material-ui/core/Grid'; +import Button from '@material-ui/core/Button'; +import TextField from '@material-ui/core/TextField'; +import LoginFeedback from '../login/loginfeedback'; +import Box from '@material-ui/core/Box'; + +export default function ChangeUsername(props) { + const url = "/simcompanies/API/user/setname" + const [username, setUsername] = React.useState(""); + const [password, setPassword] = React.useState(""); + const [disabled, setDisabled] = React.useState(false); + const [statusText, setStatusText] = React.useState(null); + const [status, setStatus] = React.useState(null); + + const postSetName = async (url, data) => { + + const response = await fetch(url, { + method: 'POST', + mode: 'cors', + cache: 'no-cache', + headers: { + 'Content-Type': 'application/json' + }, + redirect: 'follow', + referrerPolicy: 'no-referrer', + body: JSON.stringify(data) + }); + + const text = await response.text() + await setStatusText(text) + setStatus(response.status) + setDisabled(false) + setTimeout(() => { setStatus(null) }, 2000) + } + + const handleClick = (e) => { + + if (username !== "" && password !== "") { + setDisabled(true) + postSetName(url, { email: username, password: password }) + + setUsername("") + setPassword("") + } + } + + const handleKeyDown = (e) => { + if (e.key === "Enter") { + if (username !== "" && password !== "") { + setDisabled(true) + postSetName(url, { email: username, password: password }) + + setUsername("") + setPassword("") + } + } + } + + const handleChangeUSR = (e) => { + setUsername(e.target.value) + } + const handleChangePW = (e) => { + setPassword(e.target.value) + } + + return ( + + + + + Change Username + + + + + + + + + + + + + + + + + + + + + ); +} diff --git a/frontend/src/components/profile/profile.js b/frontend/src/components/profile/profile.js index 0fc1d69..f9889bf 100644 --- a/frontend/src/components/profile/profile.js +++ b/frontend/src/components/profile/profile.js @@ -1,12 +1,47 @@ import React from 'react'; import Typography from '@material-ui/core/Typography'; +import Card from '@material-ui/core/Card'; +import CardContent from '@material-ui/core/CardContent'; +import { makeStyles } from '@material-ui/core/styles'; +import Grid from '@material-ui/core/Grid'; +import ChangeUsername from './changeUsername'; +import ChangePassword from './changePassword'; + +const useStyles = makeStyles((theme) => ({ + root: { + display: 'flex', + }, + card: { + padding: 20 + } +})); export default function Profile(props) { + const classes = useStyles(); + return ( -
- - Profile Page - +
+ + + + Profile Page + + + + + + + + + + + + + + + + +
); }