Last active
February 12, 2019 20:53
-
-
Save spion/d214a1f07ce8b095bf8ff625868f08be to your computer and use it in GitHub Desktop.
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
const friendList = [ | |
{ id: 1, name: 'Phoebe' }, | |
{ id: 2, name: 'Rachel' }, | |
{ id: 3, name: 'Ross' }, | |
]; | |
function useFriendStatus(component, friendID) { | |
const onlineStatus = component.useState(null); | |
const handleStatusChange = (status) => onlineStatus.set(status.isOnline); | |
component.useEffect(() => { | |
let currentId = friendID.get(); | |
ChatAPI.subscribeToFriendStatus(currentId, handleStatusChange); | |
return () => { | |
ChatAPI.unsubscribeFromFriendStatus(currentId, handleStatusChange); | |
}; | |
}); | |
return () => onlineStatus.get(); | |
} | |
class ChatRecipientPicker { | |
recipientId = this.useState(1); | |
recipientOnline = useFriendStatus(this, this.recipientId); | |
render (props) { | |
return <> | |
<Circle color={this.recipientOnline() ? 'green' : 'red'} /> | |
<select | |
value={this.recipientId.get()} | |
onChange={e => this.recipientId.set(Number(e.target.value))} | |
> | |
{friendList.map(friend => ( | |
<option key={friend.id} value={friend.id}> | |
{friend.name} | |
</option> | |
))} | |
</select> | |
</> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment