Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import './Chat.css';
  3. import { If, Then, Else } from 'react-if';
  4. import ReactDOM from 'react-dom';
  5. import Message from './Message.js';
  6.  
  7. class Chat extends React.Component {
  8.  
  9. constructor(props) {
  10. super(props);
  11.  
  12. this.state = {
  13. data:[],
  14. chats: [] };
  15.  
  16. this.submitMessage = this.submitMessage.bind(this);
  17. }
  18.  
  19. componentDidMount(){
  20. let URL = 'stackTeste'
  21.  
  22. fetch(URL)
  23. .then(function(response) {
  24. let data = response.json()
  25. return data;
  26. })
  27. .then((json) => {
  28. console.log('Vetor JSON: ', json)
  29. this.setState({data : json});
  30. })
  31. .catch(function(ex) {
  32. console.log('parsing failed', ex)
  33. })
  34. this.scrollToBot();
  35. }
  36.  
  37.  
  38. componentDidUpdate() {
  39. this.scrollToBot();
  40. }
  41.  
  42. scrollToBot() {
  43. ReactDOM.findDOMNode(this.refs.data).scrollTop = ReactDOM.findDOMNode(this.refs.data).scrollHeight;
  44. }
  45.  
  46.  
  47. submitMessage(e) {
  48. e.preventDefault();
  49.  
  50. this.setState({
  51. chats: this.state.chats.concat([{
  52. username: "Eu",
  53. content: <p>{ReactDOM.findDOMNode(this.refs.msg).value}</p>,
  54. img: "https://www.sitelovers.com.br/franquias/2/0e01938fc48a2cfb5f2217fbfb00722d/36452e720502e4da486d2f9f6b48a7bb/editor/image/circulo_laranja.png",
  55. time: "1 min ago",
  56. }])
  57. }, () => {
  58. ReactDOM.findDOMNode(this.refs.msg).value = "";
  59. });
  60. }
  61.  
  62.  
  63. render(){
  64. const username = "Kevin su";
  65. const { chats } = this.state;
  66. return(
  67. <div className="container">
  68. <div className="chatroom ">
  69. <div className="box-header with-border" >
  70. <i className="fa fa-commenting-o fa-2x"> </i>
  71. <h3 className="box-title">Chat</h3>
  72. </div>
  73. {
  74. this.state.data.map((obj) =>
  75. <div className="box-body">
  76. <div className="direct-chat-msg undefined">
  77. <div className="direct-chat-info clearfix">
  78. </div>
  79. <If condition={obj.displayPortraitLeft == true}>
  80. <Then>
  81. <div className="direct-chat-info clearfix">
  82. <span className="direct-chat-name pull-left">{obj.userName}</span>
  83. <span className="direct-chat-timestamp pull-left">{obj.time}</span>
  84. </div>
  85. <img className="direct-chat-img" src={obj.portrait} alt="message user image"/>
  86. </Then>
  87. <Else>
  88. <div nameClass="direct-chat-msg right">
  89. <div className="direct-chat-info clearfix">
  90. <span className="direct-chat-name pull-right">{obj.userName}</span>
  91. <span className="direct-chat-timestamp pull-right">{obj.time}</span>
  92. </div>
  93. <img className="direct-chat-img-right" src={obj.portrait} alt="message user image"/>
  94. </div>
  95. </Else>
  96. </If>
  97. <div className="direct-chat-text">{obj.message}</div>
  98.  
  99. </div>
  100. </div>
  101. )
  102.  
  103. }
  104. {
  105. chats.map((chat) =>
  106. <div className="box-body">
  107. <div className="direct-chat-msg undefined">
  108. <div className="direct-chat-info clearfix">
  109. </div>
  110. <Message chat={chat} user={username} />
  111. </div>
  112. </div>
  113. )
  114. }
  115. <div className="input-group">
  116. <form onSubmit={(e) => this.submitMessage(e)}>
  117. <input type="text" ref="msg" className="form-control"/>
  118. <span className="input-group-btn">
  119. <input type="submit" className="btn btn-flat btn-primary" value="Submit" />
  120. </span>
  121. </form>
  122. </div>
  123. </div>
  124. </div>
  125. );
  126. }
  127. }
  128. export default Chat;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement