colouring-montreal/app/src/frontend/app.js

177 lines
6.0 KiB
JavaScript
Raw Normal View History

import React, { Fragment } from 'react';
2018-09-13 15:41:42 -04:00
import { Route, Switch, Link } from 'react-router-dom';
import '../../node_modules/bootstrap/dist/css/bootstrap.min.css';
import './app.css';
import AboutPage from './about';
2018-09-10 18:34:56 -04:00
import BuildingEdit from './building-edit';
import BuildingView from './building-view';
import ColouringMap from './map';
import Header from './header';
2018-11-29 17:00:53 -05:00
import Overview from './overview';
import Login from './login';
2018-09-10 18:34:56 -04:00
import MyAccountPage from './my-account';
import SignUp from './signup';
import Welcome from './welcome';
class App extends React.Component {
constructor(props) {
super(props);
2018-09-10 18:34:56 -04:00
this.state = {
user: props.user,
building: props.building,
building_like: props.building_like
2018-09-10 18:34:56 -04:00
};
this.login = this.login.bind(this);
2018-10-20 07:20:10 -04:00
this.updateUser = this.updateUser.bind(this);
this.logout = this.logout.bind(this);
2018-09-10 18:34:56 -04:00
this.selectBuilding = this.selectBuilding.bind(this);
}
login(user) {
2018-09-30 15:28:33 -04:00
if (user.error) {
this.logout();
return
}
this.setState({user: user});
}
2018-10-20 07:20:10 -04:00
updateUser(user){
console.log(user);
this.setState({user: { ...this.state.user, ...user }});
}
2018-09-10 18:34:56 -04:00
logout() {
this.setState({user: undefined});
}
2018-09-10 18:34:56 -04:00
selectBuilding(building) {
// get UPRNs and update
fetch(`/building/${building.building_id}/uprns.json`, {
method: 'GET',
headers:{
'Content-Type': 'application/json'
},
credentials: 'same-origin'
}).then(
res => res.json()
).then((res) => {
if (res.error) {
console.error(res);
} else {
building.uprns = res.uprns;
this.setState({building: building});
}
}).catch((err) => {
console.error(err)
this.setState({building: building});
});
// get if liked and update
fetch(`/building/${building.building_id}/like.json`, {
method: 'GET',
headers:{
'Content-Type': 'application/json'
},
credentials: 'same-origin'
}).then(
res => res.json()
).then((res) => {
if (res.error) {
console.error(res);
} else {
this.setState({building_like: res.like});
}
}).catch((err) => {
console.error(err)
this.setState({building_like: false});
});
2018-09-10 18:34:56 -04:00
}
render() {
return (
<Fragment>
<Header user={this.state.user} />
2018-11-13 04:24:36 -05:00
<main>
2019-01-22 13:39:14 -05:00
<Switch>
<Route exact path="/">
<Welcome />
</Route>
<Route exact path="/view/:cat.html" render={(props) => (
<Overview
{...props}
mode='view' user={this.state.user}
/>
) } />
<Route exact path="/edit/:cat.html" render={(props) => (
<Overview
{...props}
mode='edit' user={this.state.user}
/>
) } />
<Route exact path="/view/:cat/building/:building.html" render={(props) => (
<BuildingView
{...props}
{...this.state.building}
user={this.state.user}
building_like={this.state.building_like}
/>
) } />
<Route exact path="/edit/:cat/building/:building.html" render={(props) => (
<BuildingEdit
{...props}
{...this.state.building}
user={this.state.user}
building_like={this.state.building_like}
selectBuilding={this.selectBuilding}
/>
) } />
</Switch>
2018-09-10 07:41:00 -04:00
<Switch>
2018-11-29 17:00:53 -05:00
<Route exact path="/(edit.*|view.*)?" render={(props) => (
2018-10-03 16:46:51 -04:00
<ColouringMap
{...props}
building={this.state.building}
selectBuilding={this.selectBuilding}
/>
2018-09-10 17:14:09 -04:00
) } />
2018-09-10 07:41:00 -04:00
<Route exact path="/about.html" component={AboutPage} />
<Route exact path="/login.html">
<Login user={this.state.user} login={this.login} />
</Route>
<Route exact path="/sign-up.html">
<SignUp user={this.state.user} login={this.login} />
</Route>
<Route exact path="/my-account.html">
2018-10-20 07:20:10 -04:00
<MyAccountPage
user={this.state.user}
updateUser={this.updateUser}
logout={this.logout}
/>
2018-09-10 07:41:00 -04:00
</Route>
<Route component={NotFound} />
</Switch>
</main>
</Fragment>
);
}
}
const NotFound = () => (
<article>
<section className="main-col">
2018-10-03 16:46:51 -04:00
<h1 className="h1">Page not found</h1>
2018-09-13 15:41:42 -04:00
<p className="lead">
We can't find that one anywhere.
</p>
<Link className="btn btn-outline-dark" to="/">Back home</Link>
</section>
</article>
);
export default App;