firebase example 2.0

In this example below you will see how to do a firebase example 2.0 with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by rcmeisty, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright rcmeisty ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>firebase example 2.0</title>
  
  
  <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'>

      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <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>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/rcmeisty/firebase-example-20-wjdBem */
.row {
  padding:1%;
}

/*Downloaded from https://www.codeseek.co/rcmeisty/firebase-example-20-wjdBem */
//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();
})

Comments