From 369014dbf27028ffc5eba974cfd07409fd76befe Mon Sep 17 00:00:00 2001 From: nicholaspease18 Date: Thu, 24 Oct 2019 12:14:23 -0400 Subject: [PATCH] Added Dashboard to Mobile --- admin/admin.js | 63 +++++++++++ admin/authentication.js | 79 ++++++++++++++ admin/firebasescript.js | 95 ++++++++++++++++ admin/index.html | 233 ++++++++++++++++++++++++++++++++++++++++ index.html | 19 +++- js/authentication.js | 62 +++++++++++ 6 files changed, 550 insertions(+), 1 deletion(-) create mode 100644 admin/admin.js create mode 100644 admin/authentication.js create mode 100644 admin/firebasescript.js create mode 100644 admin/index.html create mode 100644 js/authentication.js diff --git a/admin/admin.js b/admin/admin.js new file mode 100644 index 0000000..f1e256d --- /dev/null +++ b/admin/admin.js @@ -0,0 +1,63 @@ +function deleteAdmin(email) { + console.log(email) + var path = firebase.database().ref("users/"+email) + path.remove(); + document.getElementById("emails").innerHTML = '' + pullAllEmails(); +} + +function addAdmin() { + var s = document.getElementById("email-input").value + sendData("users/"+s.replace("@","|").replace(".",","),true) + document.getElementById("emails").innerHTML = '' + pullAllEmails(); +} + +function pullAllEmails() { + database = firebase.database().ref("users"); + database.once('value', function(snapshot) { + snapshot.forEach(function(child) { + firebase.database().ref("users/" + child["key"]).on('value', function(data) { + var data1 = data.val(); + var unfiltered = data.key + var email = unfiltered.replace("|","@").replace(",",".") + var a = document.createElement("tr"); + a.innerHTML = " delete "+email+"" + document.getElementById("emails").appendChild(a) + componentHandler.upgradeAllRegistered(); + }); + }); + }); +} + + +function externalOnLoad() { + verify(); + pullAllEmails(); + timeChange(); + updatePage(); + readData("company", function(data) { + document.getElementById("alphacurrentpoints").innerHTML = data.alphapoints + document.getElementById("bravocurrentpoints").innerHTML = data.bravopoints + }); + window.setInterval(verify(),1000); +} + +// Verify if the user has access +function verify() { + firebase.auth().onAuthStateChanged(function(user) { + if (user) { + readData("users", function(array) { + if (array[firebase.auth().currentUser.email.replace("@","|").replace(".",",")]) { + document.getElementById("page-content").style.display = "block" + document.getElementById("unauthorized").style.display = "none" + } else { + document.getElementById("unauthorized").style.display = "block" + document.getElementById("page-content").style.display = "none" + }}); + } else { + document.getElementById("unauthorized").style.display = "block" + document.getElementById("page-content").style.display = "none" + } + }) +} \ No newline at end of file diff --git a/admin/authentication.js b/admin/authentication.js new file mode 100644 index 0000000..b3d0644 --- /dev/null +++ b/admin/authentication.js @@ -0,0 +1,79 @@ +// Firebase Check Log In Status +function checkLogin() { + firebase.auth().onAuthStateChanged(function(user) { + if (user) { + readData("users", function(array) { + if (array[firebase.auth().currentUser.email.replace("@","|").replace(".",",")]) { + document.getElementById("loginbutton").innerHTML = "Dashboard" + var logoutnode = document.createElement("li"); + document.getElementById("logoutbutton").style.display = "block" + } else { + document.getElementById("loginbutton").innerHTML = "Welcome, "+firebase.auth().currentUser.displayName + document.getElementById("logoutbutton").style.display = "block" + } + })} else { + document.getElementById("loginbutton").innerHTML = "Log In" + document.getElementById("logoutbutton").style.display = "none" + } + }) +} + +// Firebase Log In +function logIn() { + firebase.auth().setPersistence("local") + // No user is signed in. + var provider = new firebase.auth.GoogleAuthProvider(); + firebase.auth().signInWithPopup(provider).then(function(result) { + // This gives you a Google Access Token. You can use it to access the Google API. + var token = result.credential.accessToken; + // The signed-in user info. + var user = result.user; + readData("users", function(array) { + checkLogin(); + }); + // ... + }).catch(function(error) { + // Handle Errors here. + var errorCode = error.code; + var errorMessage = error.message; + // The email of the user's account used. + var email = error.email; + // The firebase.auth.AuthCredential type that was used. + var credential = error.credential; + console.log(errorCode) + console.log(errorMessage) +// ... + }); +} + +// Firebase Log Out +function logOut() { + firebase.auth().signOut().then(function() { + checkLogin(); +}, function(error) { + console.log("ERROR") + console.log(error) +}); +} + + +// Firebase Interaction Scripts +function readData(ref, callback) { + var nextformation = firebase.database().ref(ref); + nextformation.on("value", function(data) { + var array = data.val(); + callback(array) + }); +} +// global read data and place on page function +function setData(ref, element, read1) { + var array = readData(ref, function(array) { + document.getElementById(element).innerHTML = array[read1] + }); +} + +// Firebase Interaction Scripts +function sendData(ref, data) { + var database = firebase.database(); + firebase.database().ref(ref).set(data); +} \ No newline at end of file diff --git a/admin/firebasescript.js b/admin/firebasescript.js new file mode 100644 index 0000000..512bbe5 --- /dev/null +++ b/admin/firebasescript.js @@ -0,0 +1,95 @@ +// JROTC Firebase Interaction Script +// Bravo Company Comment Send +function bravoComment() { + sendData("company/bravocomments",document.getElementById("bravocomments").value) + document.getElementById('bravocomments').value = ''; +} +// Alpha Company Comment Send +function alphaComment() { + sendData("company/alphacomments",document.getElementById("alphacomments").value) + document.getElementById('alphacomments').value = ''; +} +// Commanders Cup Point Send +function alphaSend() { + sendData("company/alphapoints", document.getElementById("alpha").value); + document.getElementById('alpha').value = ''; +} + +// Bravo Point Send +function bravoSend() { + sendData("company/bravopoints", document.getElementById("bravo").value); + document.getElementById('bravo').value = ''; +} + + +// Time Change Function (Global) +function timeConvert(timestring) { + var year = timestring.substring(0,4) + var month = timestring.substring(5,7) + var day = timestring.substring(8,10) + var fulldate = month+"-"+day+"-"+year + return fulldate +} + +// Specific Webpage Scripts +function timeChange() { + var data = document.getElementById("time").value + if (data == "RTI") { + document.getElementById("customtimebox").style.display = 'none' + } else { + document.getElementById('customtimebox').style.display = 'block' + } +} + +function updatePage() { + var data = document.getElementById("event").value + if (data == "UNI") { + document.getElementById("event_type").innerHTML = "Uniform Day" + document.getElementById("locationbox").style.display = 'none' + document.getElementById("timebox").style.display = 'none' + document.getElementById("messagebox").style.display = 'none' + document.getElementById("datebox").style.display = 'block' + document.getElementById("uniformbox").style.display = 'block' + } else if (data == "FOR") { + document.getElementById("event_type").innerHTML = "Formation" + document.getElementById("locationbox").style.display = 'block' + document.getElementById("timebox").style.display = 'block' + document.getElementById("messagebox").style.display = 'none' + document.getElementById("datebox").style.display = 'block' + document.getElementById("uniformbox").style.display = 'block' + } else if (data == "CUS") { + document.getElementById("locationbox").style.display = 'none' + document.getElementById("timebox").style.display = 'none' + document.getElementById("datebox").style.display = 'none' + document.getElementById("uniformbox").style.display = 'none' + document.getElementById("messagebox").style.display = 'block' + } + document.getElementById("event_uniform").innerHTML = document.getElementById("uniformtype").value + document.getElementById("event_date").innerHTML = timeConvert(document.getElementById("date").value) + if (document.getElementById("time").value === "RTI") { + document.getElementById("event_time").innerHTML = "RTI" + } else { + document.getElementById("event_time").innerHTML = document.getElementById("customtime").value + } + document.getElementById("event_place").innerHTML = document.getElementById("location").value +} + +// Key handle sending functions + +function eventSend() { + var array = { + event: document.getElementById("event").value, + date: timeConvert(document.getElementById("date").value), + uniform: document.getElementById("uniformtype").value, + location: document.getElementById('location').value, + time: document.getElementById('time').value, + custom: document.getElementById('custommessage').value + } + sendData("nextevent/", array); + document.getElementById('date').value = ''; + document.getElementById('uniformtype').value = ''; + document.getElementById('time').value = ''; + document.getElementById('location').value = ''; + //document.getElementById('event').value = ''; + document.getElementById('custommessage').value = ''; +} \ No newline at end of file diff --git a/admin/index.html b/admin/index.html new file mode 100644 index 0000000..60e69bf --- /dev/null +++ b/admin/index.html @@ -0,0 +1,233 @@ + + + + + Nokomis JROTC - Admin + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+ + Admin Dashboard + +
+ + +
+
+
+
+

+ You do not have permission to be on this page +

+
+ +
+
+ + + \ No newline at end of file diff --git a/index.html b/index.html index c9e22cd..c1d01a1 100644 --- a/index.html +++ b/index.html @@ -1,3 +1,4 @@ + @@ -11,6 +12,7 @@ + @@ -88,7 +90,7 @@ -
+
@@ -98,6 +100,21 @@
info
+
+ Nokomis JROTC + +
diff --git a/js/authentication.js b/js/authentication.js new file mode 100644 index 0000000..61a4503 --- /dev/null +++ b/js/authentication.js @@ -0,0 +1,62 @@ +// Redirect to Dashboard +function dashboard() { + location.href = "admin/index.html" +} + +// Firebase Check Log In Status +function checkLogin() { + firebase.auth().onAuthStateChanged(function(user) { + if (user) { + readData("users", function(array) { + if (array[firebase.auth().currentUser.email.replace("@","|").replace(".",",")]) { + document.getElementById("loginbutton").innerHTML = ""; + document.getElementById("logoutbutton").style.display = "block" + componentHandler.upgradeDom(); + } else { + document.getElementById("loginbutton").innerHTML = "Welcome, "+firebase.auth().currentUser.displayName + document.getElementById("logoutbutton").style.display = "block" + } + })} else { + document.getElementById("loginbutton").innerHTML = "" + document.getElementById("logoutbutton").style.display = "none" + } + }) +} + +// Firebase Log In +function logIn() { + firebase.auth().setPersistence("local") + // No user is signed in. + var provider = new firebase.auth.GoogleAuthProvider(); + firebase.auth().signInWithPopup(provider).then(function(result) { + // This gives you a Google Access Token. You can use it to access the Google API. + var token = result.credential.accessToken; + // The signed-in user info. + var user = result.user; + readData("users", function(array) { + checkLogin(); + }); + // ... + }).catch(function(error) { + // Handle Errors here. + var errorCode = error.code; + var errorMessage = error.message; + // The email of the user's account used. + var email = error.email; + // The firebase.auth.AuthCredential type that was used. + var credential = error.credential; + console.log(errorCode) + console.log(errorMessage) +// ... + }); +} + +// Firebase Log Out +function logOut() { + firebase.auth().signOut().then(function() { + checkLogin(); +}, function(error) { + console.log("ERROR") + console.log(error) +}); +} \ No newline at end of file