Commit a0831593 authored by Vincent Seyller's avatar Vincent Seyller
Browse files

Show all colocations of user and add one

parent 7f77eebd
[{"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\index.js":"1","C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\App.js":"2","C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\reportWebVitals.js":"3","C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\Signin.js":"4","C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\Signup.js":"5","C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\ShareLoc.js":"6"},{"size":500,"mtime":499162500000,"results":"7","hashOfConfig":"8"},{"size":295,"mtime":1611066401392,"results":"9","hashOfConfig":"8"},{"size":362,"mtime":499162500000,"results":"10","hashOfConfig":"8"},{"size":1302,"mtime":1611060947418,"results":"11","hashOfConfig":"8"},{"size":1965,"mtime":1611060929098,"results":"12","hashOfConfig":"8"},{"size":4616,"mtime":1611068311406,"results":"13","hashOfConfig":"8"},{"filePath":"14","messages":"15","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"16"},"1gwdenx",{"filePath":"17","messages":"18","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"16"},{"filePath":"19","messages":"20","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"16"},{"filePath":"21","messages":"22","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"16"},{"filePath":"23","messages":"24","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"16"},{"filePath":"25","messages":"26","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\index.js",[],["27","28"],"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\App.js",[],"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\reportWebVitals.js",[],"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\Signin.js",[],"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\Signup.js",[],"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\ShareLoc.js",[],{"ruleId":"29","replacedBy":"30"},{"ruleId":"31","replacedBy":"32"},"no-native-reassign",["33"],"no-negated-in-lhs",["34"],"no-global-assign","no-unsafe-negation"]
\ No newline at end of file
[{"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\index.js":"1","C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\App.js":"2","C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\reportWebVitals.js":"3","C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\Signin.js":"4","C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\Signup.js":"5","C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\ShareLoc.js":"6","C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\Colocation.js":"7"},{"size":500,"mtime":499162500000,"results":"8","hashOfConfig":"9"},{"size":295,"mtime":1611066401392,"results":"10","hashOfConfig":"9"},{"size":362,"mtime":499162500000,"results":"11","hashOfConfig":"9"},{"size":1302,"mtime":1611060947418,"results":"12","hashOfConfig":"9"},{"size":1965,"mtime":1611060929098,"results":"13","hashOfConfig":"9"},{"size":4750,"mtime":1611073125305,"results":"14","hashOfConfig":"9"},{"size":2570,"mtime":1611076386804,"results":"15","hashOfConfig":"9"},{"filePath":"16","messages":"17","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"18"},"1gwdenx",{"filePath":"19","messages":"20","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"18"},{"filePath":"21","messages":"22","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"18"},{"filePath":"23","messages":"24","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"18"},{"filePath":"25","messages":"26","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"18"},{"filePath":"27","messages":"28","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"29","messages":"30","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\index.js",[],["31","32"],"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\App.js",[],"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\reportWebVitals.js",[],"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\Signin.js",[],"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\Signup.js",[],"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\ShareLoc.js",[],"C:\\Users\\vseyl\\Desktop\\Cours\\LP2\\ShareLoc-React\\src\\Colocation.js",[],{"ruleId":"33","replacedBy":"34"},{"ruleId":"35","replacedBy":"36"},"no-native-reassign",["37"],"no-negated-in-lhs",["38"],"no-global-assign","no-unsafe-negation"]
\ No newline at end of file
......@@ -12569,6 +12569,14 @@
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",
"integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ=="
},
"react-query": {
"version": "2.26.4",
"resolved": "https://registry.npmjs.org/react-query/-/react-query-2.26.4.tgz",
"integrity": "sha512-sXGG0gh1ah11AcfptYOCRpGDoYMnssq6riQUpQaLSM2EOodVkexp3zNLk1MFDgfRGuXQst40Tnu17oNwni66aA==",
"requires": {
"@babel/runtime": "^7.5.5"
}
},
"react-refresh": {
"version": "0.8.3",
"resolved": "https://registry.npmjs.org/react-refresh/-/react-refresh-0.8.3.tgz",
......
import {useQuery, useMutation, useQueryCache} from 'react-query';
import {useState} from 'react';
import {Route, useHistory} from 'react-router-dom';
import {Button, TextField} from '@material-ui/core/'
let Colocations = () => {
const queryCache = useQueryCache();
let ApiURL = 'http://localhost:8080/ShareLoc/api';
let history = useHistory();
let createColocation = colocation => {
let URL = ApiURL + '/colocation/create?name=' + colocation.name;
return fetch(URL, {
method:'POST',
headers: {
'Authorization': 'Bearer ' + window.localStorage.getItem('token'),
}
});
};
let AddColocationForm = () => {
let [name, setName] = useState('');
const [mutationCreate] = useMutation(createColocation, {
onSuccess: () => {queryCache.invalidateQueries('Colocations')}
});
const handleSubmit = event => {
event.preventDefault();
mutationCreate({ name: name });
};
return <form onSubmit={handleSubmit}>
<TextField id="name" label="New colocation name" size="small" variant="outlined"
onChange={(e)=>{setName(e.target.value)}}/> <Button variant="contained" color="primary" type='submit'>Add</Button>
</form>;
};
let fetchColocation = async () =>{
let URL = ApiURL + '/colocation'
let res = await fetch(URL, {
headers: {
'Authorization': 'Bearer ' + window.localStorage.getItem('token')
}
});
return res.json();
};
const {data, status} = useQuery('Colocations', fetchColocation);
return <div>
<h1>Colocations</h1>
{status === 'loading' && (<div>Loading data...</div>)}
{status === 'error' && (<div>Error fetching data</div>)}
{status === 'success' && (
<Route path='/colocations' exact>
<AddColocationForm />
<div>
<ul>
{data.map((colocation, idx) => {
return <li key={idx}>
{colocation.name}
<button onClick={() => history.push("/colocation/" + colocation.id)}>Details</button>
</li>
})}
</ul>
</div>
</Route>
)}
</div>;
}
export default Colocations;
\ No newline at end of file
......@@ -10,8 +10,9 @@ import {AppBar, Toolbar, Typography, Button, makeStyles} from '@material-ui/core
import './App.css'
import Signin from './Signin.js'
import Signup from './Signup.js'
import Signin from './Signin'
import Signup from './Signup'
import Colocations from './Colocation'
let useStyles = makeStyles((theme) => ({
root: {
......@@ -75,9 +76,12 @@ let ShareLoc = () => {
let signout = () => {
setUser(null);
window.localStorage.removeItem('token');
window.localStorage.removeItem('user');
history.push('/');
}
useEffect(() => {
console.log("check");
setUserCheck(true);
let token = window.localStorage.getItem('token');
......@@ -93,6 +97,7 @@ let ShareLoc = () => {
.then(res => res.json())
.then(data => {
setUser(data);
window.localStorage.setItem('user', data);
})
.catch(err => { console.log(err) });
}
......@@ -107,35 +112,35 @@ let ShareLoc = () => {
<Toolbar>
{user ?
<>
<Button color="inherit" href='/colocations'>
Colocations
</Button>
<Typography variant="h6" className={classes.title}>
Connected as {user.email}
</Typography>
<Button variant="contained" color="secondary" onClick={signout}>Signout</Button>
<Button color='inherit' href='/colocations'>
Your colocations
</Button>
<Typography variant='h6' className={classes.title}>
Connected as {user.email}
</Typography>
<Button variant='contained' color='secondary' onClick={signout}>Signout</Button>
</> :
<>
<Button color="inherit" href='/Signin'>
Signin
</Button>
<Button color="inherit" href='/Signup'>
Signup
</Button>
<Button color='inherit' href='/Signin'>
Signin
</Button>
<Button color='inherit' href='/Signup'>
Signup
</Button>
</>
}
</Toolbar>
</AppBar>
<Switch>
<Route path='/colocations'>
<Colocations />
</Route>
<Route path='/signin'>
<Signin signin={signin}/>
</Route>
<Route path='/signup'>
<Signup signup={signup}/>
</Route>
{/* <Suspense fallback={<div>Loading...</div>}>
<Lists />
</Suspense> */}
</Switch>
</div>
}
......
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