Skip to content

Instantly share code, notes, and snippets.

@loriculberson
Created September 28, 2019 19:36
Show Gist options
  • Select an option

  • Save loriculberson/7040cbaea29cf933e8486c28269da7ab to your computer and use it in GitHub Desktop.

Select an option

Save loriculberson/7040cbaea29cf933e8486c28269da7ab to your computer and use it in GitHub Desktop.
CoderBay Solution 7.2 Intro to Firebase
//initialize firebase
//add config
//boilerplate fb stuff
var config = {
//insert your config data here
};
firebase.initializeApp(config);
var database = firebase.database();
//set initial values for bidder and bid
var highestBidder = "No one has bid yet";
var highestPrice = 0;
//when page loads
//if no one has bid yet
//display initial values
database.ref().on("value", function(snapshot){
if (snapshot.child("highestBidder").exists() && snapshot.child("highestPrice").exists()) {
highestBidder = snapshot.val().highestBidder;
highestPrice = parseInt(snapshot.val().highestPrice);
}
//get data and append to html
$("#highest-bidder").text(highestBidder);
$("#highest-price").text(highestPrice);
})
//when user adds input
//on submit
//check to see if bidder's bid is higher than highest bid
//if YES -> update bidding area with new name and bid
//and show alert that you are highest bidder
//save new winner data to db
//if NO -> alert that bid was too low
$('#submit-bid').on('click', function(event){
event.preventDefault()
//store bidder name
//store bidder bid
var currentBidder = $('#bidder-name').val().trim()
var currentPrice = $('#bidder-price').val().trim()
currentPrice = parseInt(currentPrice)
if(currentPrice > highestPrice) {
highestPrice = currentPrice;
highestBidder = currentBidder
alert('You are the highest bidder!')
$('#highest-bidder').text(highestBidder)
$('#highest-price').text(highestPrice)
database.ref().set({
highestBidder: highestBidder,
highestPrice: highestPrice
});
} else {
alert('Your bid is too low!')
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment