From 2f5384f0e6883d8c289c17976032c6b81d68a008 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Sun, 11 Nov 2018 14:49:11 -0500 Subject: [PATCH] add being able to initiate direct message --- app/components/ContextFilter.js | 4 +-- app/components/ContextPanel.js | 6 ++-- app/components/Home.js | 59 ++++++++++++++++++++++++++++----- app/utils/parsers.js | 2 ++ 4 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 app/utils/parsers.js diff --git a/app/components/ContextFilter.js b/app/components/ContextFilter.js index 3c5a89d..8188e22 100644 --- a/app/components/ContextFilter.js +++ b/app/components/ContextFilter.js @@ -24,7 +24,7 @@ class ContextFilter extends React.Component { render() { const { open } = this.state; - const { joinChannel, name } = this.props; + const { joinConversation, name } = this.props; return ( @@ -35,7 +35,7 @@ class ContextFilter extends React.Component { initialValues={{ channel: '' }} onSubmit={(values, { setSubmitting, resetForm }) => { const { channel } = values; - joinChannel(channel); + joinConversation(channel); resetForm(); setSubmitting(false); this.handleClose(); diff --git a/app/components/ContextPanel.js b/app/components/ContextPanel.js index 7558013..ca0c670 100644 --- a/app/components/ContextPanel.js +++ b/app/components/ContextPanel.js @@ -1,14 +1,12 @@ import React, { Fragment } from 'react'; -import Divider from '@material-ui/core/Divider'; import ChannelList from './ChannelList'; import ContextFilter from './ContextFilter'; import styles from './ContextPanel.css'; -const borderStyle = { }; -const ContextPanel = ({ channels, joinChannel }) => ( +const ContextPanel = ({ channels, joinConversation }) => (

Status

- +
); diff --git a/app/components/Home.js b/app/components/Home.js index f0597ef..f5af2ec 100644 --- a/app/components/Home.js +++ b/app/components/Home.js @@ -8,8 +8,9 @@ import ChatRoom from './ChatRoom'; import ContextPanel from './ContextPanel'; import { User } from '../utils/actors'; import { ChatContext } from '../context'; +import { isContactCode } from '../utils/parsers'; -let typingNotificationsTimestamp = {}; +const typingNotificationsTimestamp = {}; const DEFAULT_CHANNEL = "mytest"; const status = new StatusJS(); @@ -41,21 +42,42 @@ export default class Home extends Component { this.setState({ currentChannel: channelName, }); } - joinChannel = channelName => { + joinConversation = contact => { + const { joinChannel, addDirectMessage } = this; + if (isContactCode(contact)) { + addDirectMessage(contact) + } else { + joinChannel(contact) + } + } + + addDirectMessage = contactCode => { + status.addContact(contactCode, () => { + this.addConversationEntry(contactCode); + this.createOnUserMessageHandler(contactCode); + }) + } + + addConversationEntry = code => { const { channels } = this.state; + this.setState({ + currentChannel: code, + channels: { + ...channels, + [code]: { users: {} } + } + }) + } + + joinChannel = channelName => { status.joinChat(channelName, () => { - this.setState({ - currentChannel: channelName, - channels: { ...channels, [channelName]: { users: {} } } - }) + this.addConversationEntry(channelName); console.log(`joined channel ${channelName}`); status.onMessage(channelName, (err, data) => { const msg = JSON.parse(data.payload)[1][0]; if (JSON.parse(data.payload)[1][1] === 'content/json') { return this.handleProtocolMessages(channelName, data); - } else { - //channels.addMessage(DEFAULT_CHANNEL, msg, data.data.sig, data.username) } const message = { username: data.username, message: msg, data }; this.setState((prevState) => { @@ -71,6 +93,25 @@ export default class Home extends Component { }); } + createOnUserMessageHandler = contactCode => { + status.onUserMessage((err, data) => { + const payload = JSON.parse(data.payload); + const msg = payload[1][0]; + //const sender = data.sig; + + const message = { username: data.username, message: msg, data }; + this.setState((prevState) => { + const existing = prevState.messages[contactCode]; + return { + messages: { + ...prevState.messages, + [contactCode]: existing ? [ ...existing, message ] : [ message ] + } + } + }) + }); + } + sendMessage = message => { const { currentChannel } = this.state; status.sendMessage(currentChannel, message); @@ -164,7 +205,7 @@ export default class Home extends Component { {!isNil(channels) && } + joinConversation={this.joinConversation} />} CONTACT_CODE_REGEXP.test(str);