Created
September 19, 2019 11:28
-
-
Save erinburns/59c613b32f8124c2b065672341d81fd1 to your computer and use it in GitHub Desktop.
JavaScript code sample. This is part of a project which allows users to add a new event listing to a website. Project used Node.js and Express.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// for add event page | |
// these variables are required for each route | |
var express = require('express'); | |
var router = express.Router(); | |
// GET home page | |
router.get('/add-event', function(request, response){ | |
// this refers to the add-event.ejs file in views folder | |
response.render('add-event', { | |
pageTitle: "Add Your Event", | |
pageID: 'add-event' | |
}); // end response | |
}); //end router | |
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Saves a new event to the database | |
"use strict"; | |
(function () { | |
var userEventsRef = firebase.database().ref("user-events"); | |
var eventsRef = firebase.database().ref("events"); | |
var uploadfile = document.getElementById('evimage'); | |
// listen for file selection, once file is selected save to folder under users uid | |
uploadfile.addEventListener('change', function(e) { | |
var uidforpics = firebase.auth().currentUser.uid; | |
var imgfile = $("#evimage").prop('files')[0]; | |
var imgref = firebase.storage().ref('listingpics/' + uidforpics + '/' + imgfile.name); | |
var progressbar = document.getElementById('uploader'); | |
// upload the image | |
var uploadimg = imgref.put(imgfile); | |
// update progress bar | |
uploadimg.on('state_changed', | |
function progress(snapshot) { | |
var percent = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; | |
progressbar.value = percent; | |
}, | |
function error(err) { | |
}, | |
function complete() { | |
imgref.getDownloadURL().then(function(url){ | |
var setimgurl = document.getElementById('imgurl'); | |
// set the value of form field w/URL | |
setimgurl.value = url; | |
console.log(setimgurl); | |
}); | |
} | |
); | |
}); | |
var submitEvent = function () { | |
var userid = firebase.auth().currentUser.uid; | |
// Get input values from each of the form elements | |
var title = $("#eventtitle").val(); | |
var description = $("#desc").val(); | |
var begDate = $("#startdate").val(); | |
var begTime = $("#starttime").val(); | |
var finDate = $("#enddate").val(); | |
var finTime = $("#endtime").val(); | |
var addrOne = $("#addrlnone").val(); | |
var addrTwo = $("#addrlntwo").val(); | |
var place = $("#city").val(); | |
var post = $("#postcode").val(); | |
var country = $("#country").val(); | |
var eventurl = $("#website").val(); | |
var eventcontact = $("#contactinfo").val(); | |
var cost = $("#price").val(); | |
var quantity = $("#quantity").val(); | |
var category = $("#category").val(); | |
var picurl = $("#imgurl").val(); | |
// set the data that will be saved in the db | |
var newEvent = { // items on left are db labels right are variable names. | |
"title": title, | |
"description": description, | |
"startdate": begDate, | |
"starttime": begTime, | |
"enddate": finDate, | |
"endtime": finTime, | |
"addressone": addrOne, | |
"addresstwo": addrTwo, | |
"website": eventurl, | |
"contact": eventcontact, | |
"city": place, | |
"country": country, | |
"postcode": post, | |
"price": cost, | |
"quantity": quantity, | |
"category": category, | |
"image": picurl, | |
"uid": userid | |
}; | |
// pushes data to the user-events collection in Firebase database | |
var newEventRef = userEventsRef.push(newEvent); | |
// adds data to events collection with same push key | |
eventsRef.child(newEventRef.key).set(newEvent); | |
}; // end submitEvent function | |
$(window).load(function () { | |
$("#listingform").submit(submitEvent); | |
}); // end window load | |
})(); // end file wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment