|
| 1 | +// app-client.js |
| 2 | +import React, { Component } from 'react' |
| 3 | +import { render } from 'react-dom' |
| 4 | +import EventBus from 'vertx3-eventbus-client' |
| 5 | +import S from 'shorti' |
| 6 | +import _ from 'lodash' |
| 7 | +import { Input } from 'react-bootstrap' |
| 8 | + |
| 9 | +class App extends Component { |
| 10 | + |
| 11 | + constructor() { |
| 12 | + super(); |
| 13 | + this.state = { |
| 14 | + data: { |
| 15 | + messages: [] |
| 16 | + } |
| 17 | + }; |
| 18 | + this.eventBus = null; |
| 19 | + } |
| 20 | + |
| 21 | + componentDidMount() { |
| 22 | + let data = this.state.data; |
| 23 | + setTimeout(() => { |
| 24 | + this.refs.author.refs.input.focus(); |
| 25 | + }, 100); |
| 26 | + |
| 27 | + this.eventBus = new EventBus('http://localhost:8080/eventbus'); |
| 28 | + const eb = this.eventBus; |
| 29 | + const self = this; |
| 30 | + // Listen for messages coming in |
| 31 | + eb.onopen = function () { |
| 32 | + eb.registerHandler('chat.message', (err, message) => { |
| 33 | + if (!err) { |
| 34 | + data = self.state.data; |
| 35 | + const messages = self.state.data.messages; |
| 36 | + if (data.author !== message.body.author) { |
| 37 | + messages.push(message.body); |
| 38 | + self.setState({ |
| 39 | + data: { |
| 40 | + author: data.author, |
| 41 | + messages |
| 42 | + } |
| 43 | + }) |
| 44 | + } |
| 45 | + } |
| 46 | + }) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + componentDidUpdate() { |
| 51 | + if (this.refs.message) |
| 52 | + this.refs.message.refs.input.focus(); |
| 53 | + if (this.refs.messages_scroll_area) |
| 54 | + this.refs.messages_scroll_area.scrollTop = this.refs.messages_scroll_area.scrollHeight |
| 55 | + } |
| 56 | + |
| 57 | + setAuthor() { |
| 58 | + const author = this.refs.author.refs.input.value.trim(); |
| 59 | + if (!author) |
| 60 | + return; |
| 61 | + this.refs.author.refs.input.value = ''; |
| 62 | + const messages = this.state.data.messages; |
| 63 | + this.setState({ |
| 64 | + data: { |
| 65 | + author, |
| 66 | + messages |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + createMessage() { |
| 72 | + const data = this.state.data; |
| 73 | + const messages = data.messages; |
| 74 | + const eb = this.eventBus; |
| 75 | + const message_text = this.refs.message.refs.input.value.trim(); |
| 76 | + if (!message_text) |
| 77 | + return; |
| 78 | + const message = { |
| 79 | + message: message_text, |
| 80 | + author: data.author |
| 81 | + }; |
| 82 | + // Send message out |
| 83 | + eb.publish('chat.message', message); |
| 84 | + // Render to browser |
| 85 | + messages.push(message); |
| 86 | + this.setState({ |
| 87 | + data: { |
| 88 | + author: data.author, |
| 89 | + messages |
| 90 | + } |
| 91 | + }); |
| 92 | + this.refs.message.refs.input.value = '' |
| 93 | + } |
| 94 | + |
| 95 | + handleSubmit(e) { |
| 96 | + e.preventDefault(); |
| 97 | + const data = this.state.data; |
| 98 | + if (data.author) |
| 99 | + this.createMessage(); |
| 100 | + else |
| 101 | + this.setAuthor(); |
| 102 | + } |
| 103 | + |
| 104 | + render() { |
| 105 | + const data = this.state.data; |
| 106 | + let form_input; |
| 107 | + if (!data.author) { |
| 108 | + form_input = ( |
| 109 | + <div> |
| 110 | + Hi, what is your name?<br /> |
| 111 | + <Input type="text" ref="author" /> |
| 112 | + </div> |
| 113 | + ) |
| 114 | + } else { |
| 115 | + form_input = ( |
| 116 | + <div> |
| 117 | + Hello { data.author }, type a message:<br /> |
| 118 | + <Input type="text" ref="message" /> |
| 119 | + </div> |
| 120 | + ) |
| 121 | + } |
| 122 | + const messages = data.messages; |
| 123 | + let messages_list; |
| 124 | + if (messages) { |
| 125 | + // order by created |
| 126 | + const sorted_messages = _.sortBy(messages, message => { |
| 127 | + return message.created |
| 128 | + }); |
| 129 | + messages_list = sorted_messages.map(message_object => { |
| 130 | + if (message_object) { |
| 131 | + return ( |
| 132 | + <li style={ { listStyle: 'none', ...S('mb-5') } }> |
| 133 | + <b>{ message_object.author }</b><br/> |
| 134 | + { message_object.message } |
| 135 | + </li> |
| 136 | + ) |
| 137 | + } |
| 138 | + }) |
| 139 | + } |
| 140 | + const scroll_area_style = { |
| 141 | + ...S('h-' + (window.innerHeight - 140)), |
| 142 | + overflowY: 'scroll' |
| 143 | + }; |
| 144 | + return ( |
| 145 | + <div> |
| 146 | + <div style={ S('pl-15') }> |
| 147 | + <h2>React Chat App</h2> |
| 148 | + <div ref="messages_scroll_area" style={ scroll_area_style }> |
| 149 | + <ul style={ S('p-0') }>{ messages_list }</ul> |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + <div style={ S('absolute b-0 w-100p pl-15 pr-15') }> |
| 153 | + <form onSubmit={ this.handleSubmit.bind(this) }> |
| 154 | + { form_input } |
| 155 | + </form> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + ) |
| 159 | + } |
| 160 | +} |
| 161 | +const app = document.getElementById('app'); |
| 162 | +render(<App />, app); |
0 commit comments