Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.93 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import fire from './config/Fire';
  3.  
  4. import './App.css';
  5.  
  6. class ConversationList extends Component{
  7. constructor(props){
  8. super(props);
  9. this.state = {
  10. conversations: props.conversations,
  11. doc_ids: props.doc_ids,
  12. index: -1,
  13. }
  14. }
  15.  
  16. handleClick = (e) =>{
  17. e.preventDefault();
  18. let otherUser = document.getElementById(e.target.id).innerHTML;
  19. let position = this.state.conversations.indexOf(otherUser);
  20. let doc_id = this.state.doc_ids[position];
  21. this.props.setRefreshed();
  22. this.props.pickConversation(otherUser,doc_id);
  23. }
  24.  
  25. render(){
  26. return(
  27. <div>
  28. <ul>
  29. {this.state.conversations.map(i =>{
  30. return <li><a id = {i} onClick = {this.handleClick}>{i}</a></li>
  31. })}
  32. </ul>
  33. </div>
  34. )
  35. }
  36. }
  37.  
  38. class Conversations extends Component{
  39. constructor(props){
  40. super(props);
  41. this.state = {
  42. thisUser: props.username,
  43. hasConversationOpen: false,
  44. otherUser : "",
  45. conversationID : "",
  46. conversations: [],
  47. doc_ids: [],
  48. numConversations: 0,
  49. finishedLoading: false,
  50. justRefreshed: false,
  51. }
  52. }
  53.  
  54. setRefreshed = () =>{
  55. this.setState({justRefreshed: !this.state.justRefreshed});
  56. }
  57.  
  58. loadConversations = () =>{
  59. let collectionRef = fire.firestore().collection('messages');
  60. let x=this;
  61. collectionRef.get().then(function(collection){
  62. document.getElementById('conversations').innerHTML = "";
  63. let finishedLoading = x.state.finishedLoading;
  64. if (!finishedLoading){
  65. collection.forEach(function(doc){
  66. let user1 = doc.data().user1;
  67. let user2 = doc.data().user2;
  68. let conversationID = doc.id;
  69.  
  70. if(user1 === x.state.thisUser || user2 === x.state.thisUser){
  71. if (user1 === x.state.thisUser){
  72. x.state.conversations[x.state.numConversations] = user2;
  73. x.state.doc_ids[x.state.numConversations] = conversationID; }
  74. else{
  75. x.state.conversations[x.state.numConversations] = user1;
  76. x.state.doc_ids[x.state.numConversations] = conversationID;
  77. }
  78.  
  79. x.setState({numConversations: x.state.numConversations+1})
  80. }
  81.  
  82. });
  83. }
  84. x.setState({finishedLoading:true})
  85.  
  86. }).catch((error)=>{
  87. let errorMessage = error;
  88. console.log("error retrieving messages collection: ", errorMessage);
  89. });
  90.  
  91. }
  92.  
  93. loadExisting = (e) =>{
  94. let otherUser = document.getElementById(e.target.id).innerHTML;
  95. let conversationID = e.target.id;
  96. this.pickConversation(otherUser,conversationID);
  97. }
  98.  
  99. pickConversation = (other, conversationID) => {
  100. if (this.state.justRefreshed){
  101. this.setState({thisUser: this.state.thisUser, hasConversationOpen: false, otherUser: other, conversationID:conversationID});
  102. }
  103. else{
  104. this.setState({thisUser: this.state.thisUser, hasConversationOpen: true, otherUser: other, conversationID:conversationID});
  105. }
  106. }
  107.  
  108. checkForExistingConversation = (e) => {
  109. e.preventDefault();
  110. //check if user already has conversation with this person
  111. if (this.state.thisUser === "guest"){
  112. document.getElementById("error-message4").innerHTML = "Only logged in users can message other users";
  113. }
  114. else{
  115. let messagesCollectionRef = fire.firestore().collection('messages');
  116. let x = this;
  117. messagesCollectionRef.get().then(function(collection){
  118. let foundConvo = false;
  119. collection.forEach(function(doc){
  120. let user1 = doc.data().user1;
  121. let user2 = doc.data().user2;
  122. if((user1 == x.state.thisUser && user2 == x.state.otherUser) ||
  123. (user2 == x.state.thisUser && user1 == x.state.otherUser)){
  124. //conversation exists
  125. foundConvo = true;
  126. x.pickConversation(x.state.otherUser, doc.id);
  127.  
  128. }
  129. });
  130. if(!foundConvo){
  131. x.newConversation();
  132. }
  133. });
  134. }
  135. }
  136.  
  137. newConversation = () => {
  138. //check if user exists
  139. let userCollectionRef = fire.firestore().collection('users');
  140. let x = this;
  141. userCollectionRef.get().then(function(collection){
  142. let userExists = false;
  143.  
  144. collection.forEach(function(doc){
  145. let user = doc.data().username;
  146. if(user == x.state.otherUser){
  147. userExists = true;
  148. }
  149. });
  150. if(!userExists){
  151. //TODO write error message or something
  152. alert("user does not exist");
  153. }
  154. else{
  155. //create conversation
  156. fire.firestore().collection("messages").doc().set({
  157. messages: {}, user1: x.state.thisUser, user2: x.state.otherUser
  158. }).then(function(){
  159. //join conversation
  160. x.loadConversations();
  161.  
  162. x.pickConversation(x.state.otherUser, x.state.conversationID);
  163. }).catch((error)=>{
  164. console.log("error while creating conversation: ",error);
  165. });
  166. }
  167.  
  168. }).catch((error)=>{
  169. let errorMessage = error;
  170. console.log("error retrieving users collection: ",errorMessage);
  171. });
  172.  
  173.  
  174. }
  175.  
  176. checkForAllowableConversation = (e) =>{
  177. e.preventDefault();
  178.  
  179. if (this.state.thisUser === "guest"){
  180. document.getElementById("error-message4").innerHTML = "Only logged in users can message other users";
  181. }
  182.  
  183. else if(this.state.otherUser === "guest" || this.state.otherUser === this.state.thisUser || this.state.otherUser === ""){
  184. //bad
  185. document.getElementById("error-message4").innerHTML = "Cannot create conversation with yourself or a guest.";
  186. //alert("cannot create conversation with guest or yourself");
  187. this.setState({otherUser:""});
  188. }
  189. else{
  190. this.checkForExistingConversation(e);
  191. }
  192. }
  193.  
  194. handleChange = (e) => {
  195. this.setState({[e.target.name]: e.target.value});
  196. }
  197.  
  198. render(){
  199. return(
  200. <div>
  201. <h2>Messages</h2>
  202. {this.loadConversations()}
  203. <form>
  204. <label>New Conversation with:</label>
  205. <input type = "text" id = "requestedUser" name="otherUser"value = {this.state.otherUser} onChange = {this.handleChange}/>
  206. <button type = "submit" onClick = {this.checkForAllowableConversation}>Begin</button>
  207. </form>
  208. <ul id = "conversations"></ul>
  209. <p id = "error-message4"></p>
  210. {this.state.finishedLoading ? <ConversationList conversations = {this.state.conversations} doc_ids={this.state.doc_ids} pickConversation={this.pickConversation} setRefreshed={this.setRefreshed}/>: null}
  211. {(this.state.hasConversationOpen)?(<Messages username={this.state.thisUser} otherUser={this.state.otherUser} conversationID={this.state.conversationID}/>):(null)}
  212. </div>
  213. )
  214. }
  215. }
  216.  
  217. class Messages extends Component{
  218. constructor(props){
  219. super(props);
  220. this.state = {
  221. msg: "",
  222. otherUser: props.otherUser,
  223. thisUser: props.username,
  224. conversationID: props.conversationID,
  225. messages: new Array (0),
  226. isLoaded: false,
  227. }
  228. }
  229.  
  230. loadMessages = () =>{
  231. if (this.state.conversationID !== "" && this.state.conversationID !== null){
  232. let conversationDocRef = fire.firestore().collection("messages").doc(this.state.conversationID);
  233. let x = this;
  234. conversationDocRef.get().then(function(doc){
  235. let messages = doc.data().messages;
  236. document.getElementById("chat").innerHTML ="";
  237.  
  238. x.setState({messages:messages});
  239. for(let i=0; i<Object.keys(messages).length; i=i+2){
  240. let author = messages[i];
  241. let msg = messages[i+1];
  242. document.getElementById("chat").innerHTML = document.getElementById("chat").innerHTML + "<p>" + author + ": " + msg + "</p>";
  243. }
  244. }).catch((error)=>{
  245. console.log("messages loading error: ", error);
  246. })
  247. }
  248. }
  249.  
  250. writeMessage = (e) =>{
  251. e.preventDefault();
  252. let conversationDocRef = fire.firestore().collection("messages").doc(this.state.conversationID);
  253. let x = this;
  254. let messagesLength = Object.keys(this.state.messages).length;
  255.  
  256. if(messagesLength<0 || messagesLength==null){
  257. messagesLength=0;
  258. }
  259. let messages = this.state.messages;
  260.  
  261. messages[messagesLength] = x.state.thisUser;
  262. messages[messagesLength+1] = x.state.msg;
  263.  
  264. conversationDocRef.update({
  265. messages:messages
  266. }).then(function(doc){
  267. x.loadMessages();
  268. });
  269.  
  270. }
  271.  
  272. handleChange = (e) =>{
  273. this.setState({[e.target.name]: e.target.value});
  274. }
  275.  
  276. render(){
  277. return(
  278. <div>
  279. {this.loadMessages()}
  280. <h2>Conversation with {this.state.otherUser}</h2>
  281. <p id = "messages"></p>
  282. <form>
  283. <label>Type a message:
  284. <input type = "text" name = "msg" id= "msg" value = {this.state.msg} onChange = {this.handleChange}/>
  285. <button type = "submit" onClick = {this.writeMessage}>Send</button>
  286. </label>
  287. <div id = "chat"></div>
  288.  
  289. </form>
  290.  
  291.  
  292. </div>
  293.  
  294. )
  295. }
  296.  
  297.  
  298.  
  299. }
  300.  
  301. export default Conversations;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement