<commit_before>import React, {Component} from 'react';
import ChatBar from './ChatBar.jsx';
import MessageList from './MessageList.jsx';

class App extends Component {
  constructor(props) {
    super(props);

    this.ws = new WebSocket('ws://localhost:3001');

    this.state = {
      message:[],
      currentUser: {name: "" }
    }
  }

  componentDidMount() {
    let newMessage = this.state.message;
    // Recieve response from server
    this.ws.addEventListener('message', (serverResponse) => {
        //push new responses into messages array
        newMessage.push(JSON.parse(serverResponse.data));
        //set the new state to be new messages
        this.setState({
          message: newMessage
        });
    })
  }

  _sendMessageToServer = (msg) => {
    this.ws.send(JSON.stringify(msg));
  }

  _getCurrentUser = (event) => {
    if(event.key === 'Enter' && event.target.value.length > 1) {
      this.state.currentUser.name = event.target.value;
    } else {
      this.state.currentUser.name = "Anonymous";
    }
  }

  _getUserMessage = (event) => {
    if(event.key === 'Enter' && event.target.value.length > 1) {
      let newMessage = {username: this.state.currentUser.name, content: event.target.value};
      this._sendMessageToServer(newMessage);
    }
  }

  render() {
    return (
       <div>
        <nav className="navbar">
          <a href="/" className="navbar-brand">Chatty</a>
        </nav>

        <ChatBar getUserMessage = {this._getUserMessage} getUser = {this._getCurrentUser} /><commit_msg>refactored componentDidMount<commit_after>import React, {Component} from 'react';
import ChatBar from './ChatBar.jsx';
import MessageList from './MessageList.jsx';

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      message:[],
      currentUser: {name: "" }
    }
  }

  componentDidMount() {
    console.log("componentDidMount <App />");

    // create new websocket connection once compenent mounds
    this.ws = new WebSocket('ws://localhost:3001');

    //Open socket connection on once component mounds.
    this.ws.onopen = () =>{
      console.log("Connection to server")
    }

    let newMessage = this.state.message;
    // Recieve response from server
    this.ws.onmessage = (serverResponse) => {
        //push new responses into messages array
        newMessage.push(JSON.parse(serverResponse.data));
        //set the new state to be new messages
        this.setState({
          message: newMessage
        });
    }
  }

  _sendMessageToServer = (msg) => {
    this.ws.send(JSON.stringify(msg));
  }

  _getCurrentUser = (event) => {
    if(event.key === 'Enter' && event.target.value.length > 1) {
      this.state.currentUser.name = event.target.value;
    } else {
      this.state.currentUser.name = "Anonymous";
    }
  }

  _getUserMessage = (event) => {
    if(event.key === 'Enter' && event.target.value.length > 1) {
      let newMessage = {username: this.state.currentUser.name, content: event.target.value};
      this._sendMessageToServer(newMessage);
    }
  }

  render() {
    return (
       <div>
        <nav className="navbar">
          <a href="/" className="navbar-brand">Chatty</a>
        </nav>

        <ChatBar getUserMessage = {this._getUserMessage} getUser = {this._getCurrentUser} />
{'fraction_non_alphanumeric': 0.14397590361445783, 'fraction_numerical': 0.0036144578313253013, 'mean_word_length': 2.685904550499445, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6556759', 'n_tokens_mistral': 1027, 'n_tokens_neox': 999, 'n_words': 211}