{"size": 895, "ext": "js", "max_stars_repo_path": "public/js/actions/ChatMessageActions.js", "max_stars_repo_name": "CodeRevelRepeat/flashback-chat", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "public/js/actions/ChatMessageActions.js", "max_issues_repo_name": "CodeRevelRepeat/flashback-chat", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "public/js/actions/ChatMessageActions.js", "max_forks_repo_name": "CodeRevelRepeat/flashback-chat", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.3409090909, "max_line_length": 60, "alphanum_fraction": 0.6134078212}
'use strict';

var AppDispatcher = require('../dispatcher/AppDispatcher');
var AppConstants = require('../constants/AppConstants');

var socket = io.connect();

socket.on('new message', function(data) {
  AppDispatcher.dispatch({
    actionType: AppConstants.CREATE_MESSAGE,
    data: data
  });
})

var MessageActions = {

  createMessage: function(text){
    var name = window.localStorage.getItem('flashbackChat');
    var newMessage = {
      createdAt: Date.now(),
      authorName: name,
      text: text
    };

    socket.emit('send message', newMessage);
    
  }, 

  getMessages: function() {
    $.ajax({
      url: '/api/messages/',
      dataType: 'json',
      type: 'GET',
      success: function(data){
        AppDispatcher.dispatch({
          actionType: AppConstants.RECEIVE_MESSAGES,
          data: data
        });
      }
    })
  }
}; 

module.exports = MessageActions;