From d19b6ec29cfed83f1651db775db2257218e3f7ac Mon Sep 17 00:00:00 2001 From: nicholaspease18 Date: Wed, 11 Sep 2019 10:41:59 -0400 Subject: [PATCH] Added Dashboard --- admin/admin.js | 25 ++++++ admin/authentication.js | 79 +++++++++++++++++ admin/firebasescript.js | 82 +++++++++++++++++ admin/index.html | 143 ++++++++++++++++++++++++++++++ css/style.css | 4 + index.html | 191 ++++++++++++++++++++++------------------ js/authentication.js | 62 +++++++++++++ js/script.js | 1 + sw.js | 7 +- 9 files changed, 501 insertions(+), 93 deletions(-) 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..b7547d3 --- /dev/null +++ b/admin/admin.js @@ -0,0 +1,25 @@ +function externalOnLoad() { + verify(); + timeChange(); + updatePage(); + 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.uid]) { + 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..ad34dcf --- /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.uid]) { + 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..121e5bd --- /dev/null +++ b/admin/firebasescript.js @@ -0,0 +1,82 @@ +// JROTC Firebase Interaction Script +// 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 = ''; +} + +function cupSend() { + var array = { + Alpha: document.getElementById("alpha").value, + Bravo: document.getElementById("bravo").value + } + sendData("commanderscup/", array); + document.getElementById('alpha').value = ''; + document.getElementById('bravo').value = ''; +} \ No newline at end of file diff --git a/admin/index.html b/admin/index.html new file mode 100644 index 0000000..f51a549 --- /dev/null +++ b/admin/index.html @@ -0,0 +1,143 @@ + + + + + Nokomis JROTC - Admin + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+ + Admin Dashboard + +
+
+
+
+
+

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

+
+ +
+
+ + + \ No newline at end of file diff --git a/css/style.css b/css/style.css index 41463f1..53c15a5 100644 --- a/css/style.css +++ b/css/style.css @@ -1,3 +1,7 @@ +.accountbutton { + display: inline-block; +} + .centere { width: 100%; text-align: center diff --git a/index.html b/index.html index 06922fa..b39a817 100644 --- a/index.html +++ b/index.html @@ -1,3 +1,4 @@ + @@ -7,6 +8,7 @@ + @@ -88,7 +90,7 @@ -
+ +
- + \ No newline at end of file diff --git a/js/authentication.js b/js/authentication.js new file mode 100644 index 0000000..9a4a246 --- /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.uid]) { + 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 diff --git a/js/script.js b/js/script.js index 9e1f1d6..9062edb 100644 --- a/js/script.js +++ b/js/script.js @@ -35,6 +35,7 @@ function externalOnLoad() { document.getElementById("event_place").innerHTML = "N/A" } }) + checkLogin(); } // Firebase Scripts diff --git a/sw.js b/sw.js index 502f191..6ec9ac1 100644 --- a/sw.js +++ b/sw.js @@ -1,14 +1,9 @@ // Offline SW const filesToCache = [ - 'index.html', - 'manifest.json', - 'sw.js', - 'https://lax18.github.io/WebJROTC/css/style.css', 'https://lax18.github.io/WebJROTC/css/font.css', 'https://lax18.github.io/WebJROTC/css/material.indigo-red.min.css', 'https://lax18.github.io/WebJROTC/js/firebase.js', - 'https://lax18.github.io/WebJROTC/js/material.min.js', - 'https://lax18.github.io/WebJROTC/js/script.js' + 'https://lax18.github.io/WebJROTC/js/material.min.js' ]; const staticCacheName = 'NokomisJROTC';