Commit 37cbeeb1 authored by Vincent Seyller's avatar Vincent Seyller
Browse files

Clean home page, and edit working

parent 4ec5e540
......@@ -70,7 +70,34 @@ const API = {
method: 'POST',
headers: { 'Authorization': 'Bearer ' + window.localStorage.getItem('token') }
}).then(checkStatus);
}
},
signup: (data) => {
return fetch(`${ApiURL}/signup?email=${data.email}&password=${data.password}&firstname=${data.firstName}&lastname=${data.lastName}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' }
}).then(checkStatus);
},
signin: (data) => {
return fetch(`${ApiURL}/signin?email=${data.email}&password=${data.password}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' }
}).then(checkStatus).then(res => res.text());
},
whoami: () => {
return fetch(`${ApiURL}/whoami`, {
headers: { 'Authorization': 'Bearer ' + window.localStorage.getItem('token') }
}).then(checkStatus).then(res => res.json());
},
edit: (data) => {
return fetch(`${ApiURL}/edit?email=${data.email}&password=${data.password}&firstname=${data.firstName}&lastname=${data.lastName}`, {
method: 'POST',
headers: { 'Authorization': 'Bearer ' + window.localStorage.getItem('token') }
}).then(checkStatus).then(res => res.json());
},
};
export default API;
\ No newline at end of file
......@@ -10,8 +10,6 @@ let Colocations = () => {
const queryCache = useQueryCache();
let ApiURL = 'http://localhost:8080/ShareLoc/api';
let createColocation = colocation => {
return API.createColocation({name: colocation.name});
};
......
import {useState} from 'react';
import {Button, TextField, Typography} from '@material-ui/core/'
let Edit = ({edit}) => {
let user = JSON.parse(window.localStorage.getItem('user'));
let [firstName, setFirstName] = useState(user.firstName);
let [lastName, setLastName] = useState(user.lastName);
let [password, setPassword] = useState(user.password);
let [error, setError] = useState('');
let handleSubmit = e => {
e.preventDefault();
edit({email: user.email, firstName: firstName, lastName: lastName, password: password})
.catch(err => { setError(err.message); });
setFirstName('');
setLastName('');
setPassword('');
};
let handleFirstNameChange = e => {
setFirstName(e.target.value);
};
let handleLastNameChange = e => {
setLastName(e.target.value);
};
let handlePasswordChange = e => {
setPassword(e.target.value);
};
return (<div>
<br/>
<Typography variant="h5">Edit your profile</Typography>
<form onSubmit={handleSubmit}>
<p></p>
<TextField id="firstname" label="Your first name" variant="outlined" value={firstName} onChange={handleFirstNameChange}/>
<p></p>
<TextField id="lastname" label="Your last name" variant="outlined" value={lastName} onChange={handleLastNameChange}/>
<p></p>
<TextField id="password" label="Your password" variant="outlined" value={password} onChange={handlePasswordChange}/>
<p></p>
<Button variant="contained" color="primary" type='submit'>Submit</Button>
<Typography color="error">{error}</Typography>
</form>
</div>)
};
export default Edit;
......@@ -6,16 +6,19 @@ import {
import {useEffect, useState} from 'react';
import {AppBar, Toolbar, Typography, Button, makeStyles} from '@material-ui/core'
import {AppBar, Toolbar, Typography, makeStyles} from '@material-ui/core'
import {Button} from 'react-bootstrap'
import './App.css'
import Signin from './Signin'
import Signup from './Signup'
import Colocations from './Colocation'
import Details from './Details';
import Services from './AchievedServices';
import RequestedServices from './RequestedServices';
import Details from './Details'
import Services from './AchievedServices'
import RequestedServices from './RequestedServices'
import API from './API'
import Edit from './Edit'
let useStyles = makeStyles((theme) => ({
root: {
......@@ -36,46 +39,27 @@ let ShareLoc = () => {
let [token, setToken] = useState(null);
let classes = useStyles();
let ApiURL = 'http://localhost:8080/ShareLoc/api';
let checkStatus = res => {
if (res.ok) {
return res;
} else {
return res.text()
.then(msg => { throw new Error(msg); });
}
};
let signin = (email, password) => {
let URL = ApiURL + '/signin?email=' + email + '&password=' + password;
return fetch(URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' }
})
.then(checkStatus)
.then(res => res.text())
.then(data => {
let signin = (data) => {
return API.signin(data).then(data => {
window.localStorage.setItem('token', data);
setToken(data);
history.push('/');
});
};
let signup = (email, firstname, lastname, password) => {
let URL = ApiURL + '/signup?email=' + email + '&password=' + password + '&firstname=' + firstname + '&lastname=' + lastname;
return fetch(URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' }
})
.then(checkStatus)
.then(res => res.text())
.then(() => {
history.push('/signin');
});
let signup = (data) => {
return API.signup(data).then(() => { history.push('/signin'); });
};
let edit = (data) => {
return API.edit(data).then(data => {
window.localStorage.setItem('user', JSON.stringify(data));
setUser(data);
history.push('/');
});
}
let signout = () => {
setUser(null);
setToken(null);
......@@ -92,18 +76,9 @@ let ShareLoc = () => {
if(user) {
setUser(JSON.parse(user));
} else if(token) {
fetch('http://localhost:8080/ShareLoc/api/whoami', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
}
})
.then(checkStatus)
.then(res => res.json())
API.whoami()
.then(data => {
setUser(data);
console.log(data);
window.localStorage.setItem('user', JSON.stringify(data));
})
.catch(err => { console.log(err) });
......@@ -119,19 +94,22 @@ let ShareLoc = () => {
<Toolbar>
{user ?
<>
<Button color='inherit' href='/colocation'>
<Button variant='outline-light' href='/colocation'>
Your colocations
</Button>
<Typography variant='h6' className={classes.title}>
Connected as {user.email}
Connected as {user.email} ({user.firstName} {user.lastName})
</Typography>
<Button variant='contained' color='secondary' onClick={signout}>Signout</Button>
<Button variant='outline-light' style={{'marginRight':"5px"}} href='/edit'>
Edit your profile
</Button>
<Button variant='danger' onClick={signout}>Signout</Button>
</> :
<>
<Button color='inherit' href='/Signin'>
<Button variant='success' style={{'marginRight':"5px"}} href='/Signin'>
Signin
</Button>
<Button color='inherit' href='/Signup'>
<Button variant='info' href='/Signup'>
Signup
</Button>
</>
......@@ -145,6 +123,9 @@ let ShareLoc = () => {
<Route exact path='/colocation/:id'>
<Details />
</Route>
<Route path='/edit'>
<Edit edit={edit}/>
</Route>
<Route path='/colocation/:id/services'>
<Services />
</Route>
......
......@@ -8,7 +8,7 @@ let Signin = ({signin}) => {
let handleSubmit = e => {
e.preventDefault();
signin(email, password)
signin({email, password})
.catch(err => { setError(err.message); });
setEmail('');
setPassword('');
......
......@@ -10,7 +10,7 @@ let Signup = ({signup}) => {
let handleSubmit = e => {
e.preventDefault();
signup(email, firstName, lastName, password)
signup({email: email, firstName: firstName, lastName: lastName, password: password})
.catch(err => { setError(err.message); });
setEmail('');
setFirstName('');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment