Last active
April 1, 2022 09:52
-
-
Save martinseanhunt/61c328090a7be6d3729c2c6fa6741028 to your computer and use it in GitHub Desktop.
Needs refactoring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const App = () => { | |
const [userId, setUserId] = useState(null); | |
| |
return ( | |
<div> | |
<header> | |
<AppBar | |
userId={userId} | |
onLogin={userId => setUserId(userId)} | |
onLogout={() => setUserId(null)} | |
/> | |
</header> | |
<main> | |
Hello {userId} | |
</main> | |
</div> | |
); | |
}; | |
| |
const LoginButton = ({ onLogin }) => { | |
// assume we use a 3rd party script to authenticate | |
useEffect(() => { | |
window.auth.addEventListener('user_present', userId => onLogin(userId)); | |
}, []); | |
| |
return ( | |
<button onClick={() => window.auth.login()}>Login</button> | |
); | |
}; | |
| |
const LogoutButton = ({ onLogout }) => { | |
useEffect(() => { | |
window.auth.addEventListener('user_gone', () => onLogout()); | |
}, []); | |
| |
return ( | |
<button onClick={() => window.auth.logout()}>Logout</button> | |
); | |
}; | |
| |
const AppBar = ({ userId, onLogin, onLogout }) => { | |
return ( | |
<div> | |
<div id="logged-in-navigation" style={{ display: userId ? 'block' : 'none'}}> | |
<a href="/dashboard">Dashboard</a> | |
<a href="/my-account">My account</a> | |
<LogoutButton onLogout={onLogout} /> | |
</div> | |
<div id="logged-out-navigation" style={{ display: userId ? 'none' : 'block'}}> | |
<LoginButton onLogin={onLogin}/> | |
</div> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment