Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="row" >
  <div class="col-md-6">
    <p>Who Are You?</p>
    <select id="writeRequesterInfo"><select>
  </div>
  <div class="col-md-6">
    <p>Who do you want to message?</p>
    <select id="writeRequestInfo"><select>
  </div>
</div>
<p id="testWrite1"></p>
    <p id="testWrite2"></p>
    <input id="messageInput">
    <button id="submitMessage">Send Message</button>
<ul id="writeMessages" class="list-group"></ul>
<script src="https://www.gstatic.com/firebasejs/4.12.1/firebase.js"></script>

              
            
!

CSS

              
                .row {
  padding:1%;
}
              
            
!

JS

              
                //configure firebase 
var config = {
    apiKey: "AIzaSyDXt_hs0IcLFfphGBeeQKdp9ecafo08FxY",
    authDomain: "signupdatabase-fa959.firebaseapp.com",
    databaseURL: "https://signupdatabase-fa959.firebaseio.com",
    projectId: "signupdatabase-fa959",
    storageBucket: "signupdatabase-fa959.appspot.com",
    messagingSenderId: "647038833859"
  };
  firebase.initializeApp(config);
//establish a reference to the database
  var dbUserRef = firebase.database().ref().child("users");
  var dbConnectionsRef = firebase.database().ref().child("connections");
//call that reference when any value in the database changes. Activate the callbackfunction as gotData
  dbUserRef.on("value", gotUserData);
  dbConnectionsRef.on("value", gotConnectionsData);
//process callback function of dbRef
  function gotUserData(result){
    console.log(result);
    //store result variable of callback function into a variable results.
    var results = result.val();
    //set a variable to store everything to write to the page in.
    var usernames = "<option>Select a person to message</option>";
    var i;
    // Loop through database data to assign a <option> column and id to each element and create a button for each username.
    for(i=0;i<results.length;i++){
      var usernameInter = results[i].username;
      usernames = usernames + "<option value='"+usernameInter+"' class='btn btn-primary'>" + usernameInter +"</option>";
    }
    //write all info to the page after loop is finished
    $("#writeRequestInfo").html(usernames);
    $("#writeRequesterInfo").html(usernames);
  }
function writeNewMessage(userName, messageIndex, messageSubIndex, message) {
  firebase.database().ref('connections/'+messageIndex+"/messages/"+messageSubIndex).set({
    message: message,
    user: userName
  });}
var requestSelection;
$("#writeRequestInfo").on("change", function(){
  requestSelection = $("#writeRequestInfo").val();
  $("#testWrite1").html(requestSelection);
  return requestSelection;
})
var myUsername;
$("#writeRequesterInfo").on("change", function(){
  myUsername = $("#writeRequesterInfo").val();
  $("#testWrite2").html(myUsername);
  return myUsername;
})

var connectionsResult;
function gotConnectionsData(result){
  connectionsResult = result.val();
  console.log(connectionsResult);
  return connectionsResult;
}
var connectionsCounter;
function createNewConnection(myUsername,otherUser,message){
  connectionsCounter = connectionsResult.length+1;
  firebase.database().ref('connections/'+connectionsCounter).set({
    messages: [{message:message,user:myUsername}],
    user1: myUsername,
    user2:otherUser
  });
  return connectionsCounter;
}
var messageIndex;
function findMessageIndex(){
  var y;
  for(y=0;y<=connectionsResult.length;y++){
    var user1i = connectionsResult[y].user1;
    var user2i = connectionsResult[y].user2;
    var meTrue;
    var otherTrue;
    if(myUsername == user1i || myUsername == user2i){
       meTrue = 1;
      }
      if(requestSelection == user1i || requestSelection == user2i){
        otherTrue = 1
      }
      if(meTrue == 1 && otherTrue==1){
        messageIndex = y;
        break;
      }
  }
}
var user1;
var user2;
var meTrue;
var otherTrue;
var bothTrue;
function evaluationBoth(){
  myUsername = $("#writeRequesterInfo").val();
  requestSelection = $("#writeRequestInfo").val();
      if(myUsername == user1 || myUsername == user2){
       meTrue = 1;
      }
      if(requestSelection == user1 || requestSelection == user2){
        otherTrue = 1
      }
      if(meTrue == 1 && otherTrue==1){
        bothTrue = 1;
      }
}
function zeroOutEvaluation(){
  bothTrue=0;
  meTrue = 0;
  otherTrue = 0;
}
var messageIndex;
function cycleForConnectionAndEvaluation(){
  var a;
  for(a=0;a<=connectionsResult.length;a++){
    user1 = connectionsResult[a].user1;
    user2 = connectionsResult[a].user2;
    if(user1==myUsername && user2==requestSelection){
      bothTrue=1;
      messageIndex=a;
      }
    if(user2==myUsername && user1==requestSelection){
      bothTrue=1;
      messageIndex=a;
      }
  }
}
var message;
function newMessage(){
  var y;
  for(y=0;y<=connectionsResult.length;y++){
    user1 = connectionsResult[y].user1;
    user2 = connectionsResult[y].user2;
    message = $("#messageInput").val();
    evaluationBoth();
    if (bothTrue===1){
      messageIndex = y;
      var messageSubIndex = connectionsResult[y].messages.length;
      var messagesInDbWrite ="";
      var z;
      writeNewMessage(myUsername, messageIndex, messageSubIndex, message);
      for(z=0;z<=connectionsResult[y].messages.length;z++){
        var userInter = connectionsResult[y].messages[z].user;
        var messageInter = connectionsResult[y].messages[z].message;
        messagesInDbWrite = messagesInDbWrite + "<li><strong>" + userInter + "</strong>: " + messageInter + "</li>";     
      }
      $("#writeMessages").html(messagesInDbWrite);
      zeroOutEvaluation();
    } else if (bothTrue===0){
      user1 = connectionsResult[y].user1;
      user2= connectionsResult[y].user2;
      var otherUser;
      if (user1 = myUsername){
        otherUser = user2;
      }else {
        otherUser = user1;
        myUsername = user2;
      }
      createNewConnection(myUsername,requestSelection,message)
      var a;
      var lengthResultNow = connectionsCounter;
      for(a=0;a<=connectionsResult[lengthResultNow].messages.length;a++){
        var userInter = connectionsResult[lengthResultNow].messages[a].user;
        var messageInter = connectionsResult[lengthResultNow].messages[a].message;
        messagesInDbWrite = messagesInDbWrite + "<li class='list-group-item'><strong>" + userInter + "</strong>: " + messageInter + "</li>";
      }
      $("#writeMessages").html(messagesInDbWrite);
      zeroOutEvaluation();
    }
  }
}
$("#submitMessage").click(function(){
  $("#writeMessages").html("");
  newMessage();
})




              
            
!
999px

Console