/menu.jsx Secret
Last active
April 28, 2025 20:09
-
Star
(10)
You must be signed in to star a gist -
Fork
(107)
You must be signed in to fork a gist
-
-
Save bnorton/be30b6e7d7f8dc1f0493e732f0540eae to your computer and use it in GitHub Desktop.
Frontend engineer @ Chameleon | Technical exercise: Menu system
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
window.currentUser = { id: '19', name: 'Jane', email: '[email protected]' }; | |
// users are like authors and profiles like commentors | |
// open a modal with commentor info when clicked | |
... | |
export const ActiveProfiles({ profiles, onLaunchProfile }) => { | |
var active = []; | |
for(i=0; i < profiles.length; i++) { | |
if(!profiles[i].disabled && profiles[i]['last_seen_time'] > new Date(new Date().getTime()-(24*60*1000)).toISOString()) { // within the last 24 hours | |
active.push(profiles[i]); | |
} | |
} | |
if(active.length == 1 && active[0].email === window.currentUser.email) { | |
active.length = 0; | |
} | |
return ( | |
<div> | |
{active.map(function(a) { return <div onClick={() => onLaunchProfile(a.name, a.email)}>{a.name} - {a.email}</div> })} | |
</div> | |
) | |
} |
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
// Example usage: | |
<UserLoggedInOrNotMenu handleLogin={() => httpRedirectTo('https://www.chmln.co/login?return_to=app')} /> | |
// --------- | |
import React, { useState, useEffect } from 'react'; | |
import { httpGet, httpRedirectTo } from 'lib/http'; | |
const Menu = () => ( | |
<ul> | |
<li>Home</li> | |
<li>{user.firstName}'s Profile</li> | |
<li>Settings</li> | |
<li>Logout</li> | |
</ul> | |
); | |
const LoginButton = ({ onLogin }) => ( | |
<a onClick={onLogin}>Log In</a> | |
); | |
const UserLoggedInOrNotMenu = ({ handleLogin }) => { | |
const [isLoggedIn, setIsLoggedIn] = useState(false); | |
const [loading, setLoading] = useState(true); | |
useEffect(() => { | |
httpGet('users/ping').then(({ user }) => setIsLoggedIn(!!user.id),setLoading(false)); | |
}, []); | |
if (loading) { | |
return <div>Loading...</div>; | |
} | |
return ( | |
<div> | |
{isLoggedIn ? <Menu /> : <LoginButton onLogin={handleLogin} />} | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment