make the unread count work

This commit is contained in:
Connor Turland 2016-12-19 15:52:05 -05:00
parent 125e1c86a3
commit b5e8fa0fc2
4 changed files with 25 additions and 26 deletions

View file

@ -117,7 +117,6 @@
background: url(<%= asset_path 'junto_spinner_dark.gif' %>) no-repeat 2px 8px, url(<%= asset_path 'tray_tab.png' %>) no-repeat !important; background: url(<%= asset_path 'junto_spinner_dark.gif' %>) no-repeat 2px 8px, url(<%= asset_path 'tray_tab.png' %>) no-repeat !important;
} }
.chat-box .chat-button .chat-unread { .chat-box .chat-button .chat-unread {
display: none;
background: #DAB539; background: #DAB539;
position: absolute; position: absolute;
top: -3px; top: -3px;

View file

@ -17,6 +17,7 @@ const linker = new Autolinker({ newWindow: true, truncate: 50, email: false, pho
const ChatView = { const ChatView = {
isOpen: false, isOpen: false,
mapChat: null,
init: function(messages, mapper, room, opts = {}) { init: function(messages, mapper, room, opts = {}) {
const self = ChatView const self = ChatView
self.room = room self.room = room
@ -26,7 +27,6 @@ const ChatView = {
self.alertSound = true // whether to play sounds on arrival of new messages or not self.alertSound = true // whether to play sounds on arrival of new messages or not
self.cursorsShowing = true self.cursorsShowing = true
self.videosShowing = true self.videosShowing = true
self.unreadMessages = 0
self.participants = [] self.participants = []
self.sound = new Howl({ self.sound = new Howl({
@ -43,14 +43,13 @@ const ChatView = {
}, },
render: () => { render: () => {
const self = ChatView const self = ChatView
ReactDOM.render(React.createElement(MapChat, { self.mapChat = ReactDOM.render(React.createElement(MapChat, {
onOpen: self.onOpen, onOpen: self.onOpen,
onClose: self.onClose, onClose: self.onClose,
leaveCall: Realtime.leaveCall, leaveCall: Realtime.leaveCall,
joinCall: Realtime.joinCall, joinCall: Realtime.joinCall,
participants: self.participants, participants: self.participants,
messages: self.messages.models.map(m => m.attributes), messages: self.messages.models.map(m => m.attributes),
unreadMessages: self.unreadMessages,
videoToggleClick: self.videoToggleClick, videoToggleClick: self.videoToggleClick,
cursorToggleClick: self.cursorToggleClick, cursorToggleClick: self.cursorToggleClick,
soundToggleClick: self.soundToggleClick, soundToggleClick: self.soundToggleClick,
@ -63,11 +62,9 @@ const ChatView = {
}), document.getElementById('chat-box-wrapper')) }), document.getElementById('chat-box-wrapper'))
}, },
onOpen: () => { onOpen: () => {
ChatView.isOpen = true
$(document).trigger(ChatView.events.openTray) $(document).trigger(ChatView.events.openTray)
}, },
onClose: () => { onClose: () => {
ChatView.isOpen = false
$(document).trigger(ChatView.events.closeTray) $(document).trigger(ChatView.events.closeTray)
}, },
addParticipant: participant => { addParticipant: participant => {
@ -81,13 +78,9 @@ const ChatView = {
ChatView.participants = ChatView.participants.filter(p => p.id !== remove_id) ChatView.participants = ChatView.participants.filter(p => p.id !== remove_id)
ChatView.render() ChatView.render()
}, },
incrementUnread: (render = false) => {
ChatView.unreadMessages += 1
if (render) ChatView.render()
},
addMessage: (message, isInitial, wasMe) => { addMessage: (message, isInitial, wasMe) => {
const self = ChatView const self = ChatView
if (!self.isOpen && !isInitial) ChatView.incrementUnread(false) // TODO don't need to render, right? if (!isInitial) self.mapChat.newMessage()
function addZero(i) { function addZero(i) {
if (i < 10) { if (i < 10) {
@ -141,14 +134,14 @@ const ChatView = {
close: () => { close: () => {
// TODO how to do focus with react // TODO how to do focus with react
// this.$messageInput.blur() // this.$messageInput.blur()
ChatView.mapChat.close()
}, },
open: () => { open: () => {
ChatView.unreadMessages = 0 ChatView.mapChat.open()
// TODO how to do focus with react // TODO how to do focus with react
// this.$messageInput.focus() // this.$messageInput.focus()
// TODO reimplement scrollMessages // TODO reimplement scrollMessages
// this.scrollMessages(0) // this.scrollMessages(0)
ChatView.render()
}, },
videoToggleClick: function() { videoToggleClick: function() {
const self = ChatView const self = ChatView

View file

@ -1,10 +1,7 @@
import React from 'react' import React from 'react'
const Unread = props => { const Unread = props => {
if (props.count <= 0) { return props.count ? <div className="chat-unread">{props.count}</div> : null
return null
}
return <div className="chat-unread"></div>
} }
export default Unread export default Unread

View file

@ -8,20 +8,29 @@ class MapChat extends Component {
super(props) super(props)
this.state = { this.state = {
unreadMessages: 0,
open: false, open: false,
messageText: '' messageText: ''
} }
} }
toggleDrawer = () => { close = () => {
if (this.state.open) {
this.setState({open: false}) this.setState({open: false})
this.props.onClose() this.props.onClose()
} }
else if (!this.state.open) {
this.setState({open: true}) open = () => {
this.setState({open: true, unreadMessages: 0})
this.props.onOpen() this.props.onOpen()
} }
newMessage = () => {
if (!this.state.open) this.setState({ unreadMessages: this.state.unreadMessages + 1 })
}
toggleDrawer = () => {
if (this.state.open) this.close()
else if (!this.state.open) this.open()
} }
handleChange = key => e => { handleChange = key => e => {
@ -42,6 +51,7 @@ class MapChat extends Component {
render = () => { render = () => {
const rightOffset = this.state.open ? '0' : '-300px' const rightOffset = this.state.open ? '0' : '-300px'
const { videosShowing, cursorsShowing, alertSound } = this.props const { videosShowing, cursorsShowing, alertSound } = this.props
const { unreadMessages } = this.state
return ( return (
<div className="chat-box" <div className="chat-box"
style={{ right: rightOffset }} style={{ right: rightOffset }}
@ -71,7 +81,7 @@ class MapChat extends Component {
</div> </div>
<div className="chat-button" onClick={this.toggleDrawer}> <div className="chat-button" onClick={this.toggleDrawer}>
<div className="tooltips">Chat</div> <div className="tooltips">Chat</div>
<Unread count={this.props.unreadMessages} /> <Unread count={unreadMessages} />
</div> </div>
<div className="chat-messages"> <div className="chat-messages">
{this.props.messages.map(message => { {this.props.messages.map(message => {