Last active
July 3, 2023 21:43
-
-
Save gustavoFreireS/ab8a387400701743ab2b7cb270c4c2b5 to your computer and use it in GitHub Desktop.
Chameleon test
This file contains 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
// definetly not advisable to add data on window object, mainly because you lose control of state of application, better get this from a global state library/context and send | |
// this information as props for the components | |
window.currentUser = { id: '19', name: 'Jane', email: 'jane@chameleon.io' }; | |
// users are like authors and profiles like commentors | |
// open a modal with commentor info when clicked | |
... | |
export const ActiveProfiles({ profiles, onLaunchProfile }) => { | |
var active = []; | |
/* | |
it's dificult to read what the for loop does its better to separate in pieces of code, also the timeframe/islast24 function/constant better be added on a utils file/folder to be reusable | |
One way to improve the for loop initial implementation is to create a filter loop, that way we can even remove the if statement that comes after the for loop and doing all the checks/formating | |
of data at once | |
``` | |
const timeframe = new Date(new Date().getTime()-(24*60*1000)).toISOString() | |
const isLast24 = (profile) => profiles['last_seen_time'] > timeframe | |
const active = !!profiles.length && profiles.filter(profile => !profile.disable && isLast24(profile) && profile.email !== window.currentUser.email) | |
``` | |
*/ | |
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; | |
} | |
/* | |
here if there is not data on the active array it will show nothing to the user, better have text for when there is no data like so: | |
``` | |
!!active.length ? | |
active.map(function(a) { return <div onClick={() => onLaunchProfile(a.name, a.email)}>{a.name} - {a.email}</div> }) | |
: <h3> No active profiles </h3> | |
``` | |
*/ | |
return ( | |
<div> | |
{active.map(function(a) { return <div onClick={() => onLaunchProfile(a.name, a.email)}>{a.name} - {a.email}</div> })} | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment