ny klæbb

This commit is contained in:
Felix Albrigtsen 2022-04-23 15:17:07 +02:00
parent c20cea953d
commit 2677793b28
2 changed files with 60 additions and 42 deletions

View File

@ -200,48 +200,72 @@ function Home() {
); );
} }
class LoginManager {
user = {
name: "",
email: "",
googleId: "",
asuraId: -1,
isManager: false
};
checkLogin() { // class LoginManager {
fetch(process.env.REACT_APP_API_URL + `/users/getSavedUser`) // checkLogin() {
.then(res => res.json()) // fetch(process.env.REACT_APP_API_URL + `/users/getSavedUser`)
.then(data => { // .then(res => res.json())
if (data.status !== "OK") { // .then(data => {
console.error(data.data); // if (data.status !== "OK") {
return; // console.error(data.data);
} // return;
console.log(data); // }
this.user = data.data; // console.log(data);
return this.user; // setUser(data.data);
}) // return user;
.catch((err) => console.log(err.message)); // })
} // .catch((err) => console.log(err.message));
// }
isLoggedIn() { // isLoggedIn() {
let loggedIn = this.user.googleId !== "" && this.user.asuraId !== -1; // let loggedIn = user.googleId !== "" && user.asuraId !== -1;
console.log(loggedIn); // console.log(loggedIn);
return loggedIn; // return loggedIn;
} // }
isManager() { // isManager() {
return this.isLoggedIn() && this.user.isManager; // return this.isLoggedIn() && user.isManager;
} // }
} // }
let login = new LoginManager(); // let login = new LoginManager();
login.checkLogin(); // login.checkLogin();
let showSuccess = (message) => {}; let showSuccess = (message) => {};
let showError = (message) => {}; let showError = (message) => {};
export default function App() { export default function App() {
const [user, setUser] = React.useState({});
let checkLogin = () => {
fetch(process.env.REACT_APP_API_URL + `/users/getSavedUser`)
.then(res => res.json())
.then(data => {
if (data.status !== "OK") {
setUser({
isManager: false,
isLoggedIn: false
});
console.error(data.data);
return;
}
console.log(data);
let u = data.data;
u.isLoggedIn = true;
setUser(u);
})
.catch((err) => {
console.log(err.message);
setUser({
isManager: false,
isLoggedIn: false
});
});
}
React.useEffect(() => {
checkLogin();
}, []);
const [openError, setOpenError] = React.useState(false); const [openError, setOpenError] = React.useState(false);
const [errorMessage, setErrorMessage] = React.useState(""); const [errorMessage, setErrorMessage] = React.useState("");
showError = (message) => { showError = (message) => {
@ -269,7 +293,7 @@ export default function App() {
<Route path="/tournament/:tournamentId/teams" element={<TournamentTeams showError={showError} showSuccess={showSuccess} />} /> <Route path="/tournament/:tournamentId/teams" element={<TournamentTeams showError={showError} showSuccess={showSuccess} />} />
<Route path="/history" element={<TournamentHistory showError={showError} showSuccess={showSuccess} />} /> <Route path="/history" element={<TournamentHistory showError={showError} showSuccess={showSuccess} />} />
<Route path="/login" element={<LoginPage />} /> <Route path="/login" element={<LoginPage />} />
<Route path="/profile" element={<ProfilePage login={login} />} /> <Route path="/profile" element={<ProfilePage user={user} checkLogin={checkLogin} />} />
<Route path="/admins" element={<AdminsOverview />} /> <Route path="/admins" element={<AdminsOverview />} />
</Routes> </Routes>
</Router> </Router>

View File

@ -5,17 +5,11 @@ import ErrorSnackbar from "./components/ErrorSnackbar";
import { Button, TextField, Stack, InputLabel, Select, Container, Slider, Paper, Box, Grid, Typography } from '@mui/material'; import { Button, TextField, Stack, InputLabel, Select, Container, Slider, Paper, Box, Grid, Typography } from '@mui/material';
export default function ProfilePage(props) { export default function ProfilePage(props) {
const [loggedIn, setLoggedIn] = React.useState(props.login.isLoggedIn()); let user = props.user;
React.useEffect(() => {
setLoggedIn(props.login.isLoggedIn());
}, [props.login]);
let user = props.login.user;
return (<> return (<>
<AppBar pageTitle="Profile" /> <AppBar pageTitle="Profile" />
<Container sx={{minHeight: "30vh", width: "90vw", padding: "20px 20px"}} component={Container} direction="column" align="center"> <Container sx={{minHeight: "30vh", width: "90vw", padding: "20px 20px"}} component={Container} direction="column" align="center">
{loggedIn ? <> {user.isLoggedIn ? <>
<Paper sx={{minHeight: "30vh", width: "90vw", margin: "10px auto"}} component={Stack} direction="column" justifyContent="center"> <Paper sx={{minHeight: "30vh", width: "90vw", margin: "10px auto"}} component={Stack} direction="column" justifyContent="center">
<div align="center"> <div align="center">
<h2><b>Your profile</b></h2> <h2><b>Your profile</b></h2>