2018-09-09 17:22:44 -04:00
|
|
|
import React, { Fragment } from 'react';
|
2018-09-13 15:41:42 -04:00
|
|
|
import { Route, Switch, Link } from 'react-router-dom';
|
2018-09-13 17:13:53 -04:00
|
|
|
import { TransitionGroup, CSSTransition } from 'react-transition-group'
|
2018-09-09 17:22:44 -04:00
|
|
|
|
2018-09-17 16:27:52 -04:00
|
|
|
import '../../node_modules/bootstrap/dist/css/bootstrap.min.css';
|
2018-10-20 06:33:27 -04:00
|
|
|
import './app.css';
|
2018-09-17 16:27:52 -04:00
|
|
|
|
2018-09-09 17:22:44 -04:00
|
|
|
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';
|
2018-09-09 17:22:44 -04:00
|
|
|
import Header from './header';
|
2018-09-10 18:34:56 -04:00
|
|
|
import Legend from './legend';
|
2018-09-09 17:22:44 -04:00
|
|
|
import Login from './login';
|
2018-09-10 18:34:56 -04:00
|
|
|
import MyAccountPage from './my-account';
|
2018-09-09 17:22:44 -04:00
|
|
|
import SignUp from './signup';
|
|
|
|
import Welcome from './welcome';
|
2018-10-25 06:18:42 -04:00
|
|
|
import BuildingEditAny from './building-edit-any';
|
2018-09-09 17:22:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
2018-09-09 17:22:44 -04:00
|
|
|
this.login = this.login.bind(this);
|
2018-10-20 07:20:10 -04:00
|
|
|
this.updateUser = this.updateUser.bind(this);
|
2018-09-09 17:22:44 -04:00
|
|
|
this.logout = this.logout.bind(this);
|
2018-09-10 18:34:56 -04:00
|
|
|
this.selectBuilding = this.selectBuilding.bind(this);
|
2018-09-09 17:22:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
login(user) {
|
2018-09-30 15:28:33 -04:00
|
|
|
if (user.error) {
|
|
|
|
this.logout();
|
|
|
|
return
|
|
|
|
}
|
2018-09-09 17:22:44 -04:00
|
|
|
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() {
|
2018-09-09 17:22:44 -04:00
|
|
|
this.setState({user: undefined});
|
|
|
|
}
|
|
|
|
|
2018-09-10 18:34:56 -04:00
|
|
|
selectBuilding(building) {
|
2018-10-25 09:36:52 -04:00
|
|
|
// 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});
|
|
|
|
});
|
2018-09-10 18:34:56 -04:00
|
|
|
}
|
|
|
|
|
2018-09-09 17:22:44 -04:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<Header user={this.state.user} />
|
2018-11-13 04:24:36 -05:00
|
|
|
<main>
|
2018-10-03 16:46:51 -04:00
|
|
|
<TransitionGroup>
|
|
|
|
<CSSTransition timeout={3000} classNames='fade'>
|
|
|
|
<Switch>
|
|
|
|
<Route exact path="/">
|
|
|
|
<Welcome />
|
|
|
|
</Route>
|
2018-10-25 06:18:42 -04:00
|
|
|
<Route exact path="/select.html">
|
2018-11-21 17:19:51 -05:00
|
|
|
<BuildingEditAny user={this.state.user} />
|
2018-10-25 06:18:42 -04:00
|
|
|
</Route>
|
2018-10-03 16:46:51 -04:00
|
|
|
<Route exact path="/map/:map.html" component={Legend} />
|
|
|
|
<Route exact path="/building/:building.html" render={(props) => (
|
|
|
|
<BuildingView
|
|
|
|
{...props}
|
|
|
|
{...this.state.building}
|
|
|
|
user={this.state.user}
|
|
|
|
/>
|
|
|
|
) } />
|
|
|
|
<Route exact path="/building/:building/edit.html" render={(props) => (
|
|
|
|
<BuildingEdit
|
|
|
|
{...props}
|
|
|
|
{...this.state.building}
|
|
|
|
user={this.state.user}
|
|
|
|
selectBuilding={this.selectBuilding}
|
|
|
|
/>
|
|
|
|
) } />
|
|
|
|
</Switch>
|
|
|
|
</CSSTransition>
|
|
|
|
</TransitionGroup>
|
2018-09-10 07:41:00 -04:00
|
|
|
<Switch>
|
2018-10-25 06:18:42 -04:00
|
|
|
<Route exact path="/(select.html|map.*|building.*)?" 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>
|
2018-09-09 17:22:44 -04:00
|
|
|
</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>
|
2018-09-09 17:22:44 -04:00
|
|
|
</section>
|
|
|
|
</article>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default App;
|