Skip to content

Commit ef63daa

Browse files
maxwellgordonansonws
authored andcommitted
Store User in App
1 parent b22e93f commit ef63daa

4 files changed

Lines changed: 68 additions & 18 deletions

File tree

src/components/App.js

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,53 @@
1-
import React from 'react';
1+
import React, {Component} from 'react';
22
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
33
import ProductShowPage from './ProductShowPage';
44
import ProductIndexPage from './ProductIndexPage';
55
import NewProductPage from './NewProductPage';
66
import SignInPage from './SignInPage';
77
import HomePage from './HomePage';
88
import NavBar from './NavBar';
9+
import { User } from '../requests'
910

10-
const App = () => {
11-
return (
12-
<Router>
13-
<div className="App">
14-
<NavBar />
15-
<Switch>
16-
<Route path="/session/new" exact component={SignInPage} />
17-
<Route path="/products/new" component={NewProductPage} />
18-
<Route path="/products/:id" component={ProductShowPage} />
19-
<Route path="/products" exact component={ProductIndexPage} />
20-
<Route path="/" exact component={HomePage} />
21-
</Switch>
22-
</div>
23-
</Router>
24-
);
11+
class App extends Component {
12+
constructor(props) {
13+
super(props);
14+
this.state = {
15+
currentUser: null
16+
}
17+
this.getUser = this.getUser.bind(this);
18+
}
19+
20+
getUser() {
21+
User.current().then(currentUser => {
22+
if (currentUser.id) {
23+
this.setState({ currentUser });
24+
}
25+
})
26+
}
27+
28+
componentDidMount() {
29+
this.getUser();
30+
}
31+
32+
render() {
33+
const { currentUser } = this.state;
34+
return (
35+
<Router>
36+
<div className="App">
37+
<NavBar currentUser={currentUser} />
38+
<Switch>
39+
<Route path="/session/new" exact render={
40+
(routeProps) => <SignInPage {...routeProps} onSignIn={this.getUser} />}
41+
/>
42+
<Route path="/products/new" component={NewProductPage} />
43+
<Route path="/products/:id" component={ProductShowPage} />
44+
<Route path="/products" exact component={ProductIndexPage} />
45+
<Route path="/" exact component={HomePage} />
46+
</Switch>
47+
</div>
48+
</Router>
49+
);
50+
}
2551
};
2652

2753
export default App;

src/components/NavBar.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@ import React from 'react';
22
import { NavLink } from 'react-router-dom';
33

44
function NavBar(props) {
5+
const {currentUser} = props;
56
return (
67
<div className="NavBar">
78
<NavLink to="/">Home</NavLink>|<NavLink to="/products">Products</NavLink>|
8-
<NavLink to="/products/new">New Product</NavLink>
9-
<NavLink to="/session/new">Sign In</NavLink>
9+
10+
{currentUser ? (
11+
<span>
12+
<>
13+
<NavLink to="/products/new">New Product</NavLink>
14+
|
15+
<span>{currentUser.full_name}</span>
16+
</>
17+
</span>
18+
) : (
19+
<NavLink exact to="/session/new">
20+
Sign In
21+
</NavLink>
22+
)}
1023
</div>
1124
);
1225
}

src/components/SignInPage.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import { Session } from "../requests";
2020
});
2121
return;
2222
}
23+
if (typeof this.props.onSignIn === 'function') {
24+
this.props.onSignIn();
25+
}
2326
this.props.history.push("/");
2427
});
2528
}

src/requests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ export const Session = {
3939
}).then(res => res.json());
4040
},
4141
};
42+
43+
export const User = {
44+
current() {
45+
return fetch(`${BASE_URL}/users/current`, {
46+
credentials: 'include',
47+
}).then(res => res.json());
48+
}
49+
};

0 commit comments

Comments
 (0)