Skip to content

Commit

Permalink
Add UserInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
making committed Dec 30, 2020
1 parent 6ecdaa4 commit 1fede9b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions shop-ui/ui/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import {Sock} from "./routes/Sock";
import {Tag} from "./routes/Tag";
import {Home} from "./routes/Home";
import {CartSummary} from "./components/CartSummary";
import {UserInfo} from "./components/UserInfo";

export default function App() {
return (
<Router>
<div>
<h1><Link to="/">Spring Socks</Link></h1>
<UserInfo/>
<CartSummary/>
<Switch>
<Route path="/cart">
Expand Down
25 changes: 25 additions & 0 deletions shop-ui/ui/src/components/UserInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React, {useEffect, useState} from "react";

export function UserInfo() {
const [me, setMe] = useState({});
useEffect(() => {
fetchMe().then(setMe);
}, []);
if (me.family_name && me.given_name) {
return <p>
{`${me.family_name} ${me.given_name}`}<br/>
<a href={"/logout"}>Logout</a>
</p>;
}
return <p><a href={"/login"}>Login</a></p>;
}

function fetchMe() {
return fetch('/me', {
method: 'GET',
headers: {
'Accept': 'application/json'
},
})
.then(res => res.json());
}

0 comments on commit 1fede9b

Please sign in to comment.