Skip to content

Instantly share code, notes, and snippets.

@ximosa
Created April 7, 2020 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ximosa/73f0a4cdd44332336449fc7d45c5077d to your computer and use it in GitHub Desktop.
Save ximosa/73f0a4cdd44332336449fc7d45c5077d to your computer and use it in GitHub Desktop.
'use strict';
// Shortcuts to DOM Elements.
var signInButton = document.getElementById('sign-in-button');
var signOutButton = document.getElementById('sign-out-button');
var splashPage = document.getElementById('page-splash');
var recentPostsSection = document.getElementById('recent-posts-list');
var userPostsSection = document.getElementById('user-posts-list');
var topUserPostsSection = document.getElementById('top-user-posts-list');
var recentMenuButton = document.getElementById('menu-recent');
var myPostsMenuButton = document.getElementById('menu-my-posts');
var myTopPostsMenuButton = document.getElementById('menu-my-top-posts');
var listeningFirebaseRefs = [];
/**
* Writes a new comment for the given post.
*/
function createNewComment(postId, username, uid, text) {
firebase.database().ref('post-comments/' + postId).push({
text: text,
author: username,
uid: uid
});
}
/**
* Writes the user's data to the database.
*/
// [START basic_write]
function writeUserData(userId, name, email, imageUrl) {
firebase.database().ref('users/' + userId).set({
username: name,
email: email,
profile_picture : imageUrl
});
}
// [END basic_write]
/**
* Cleanups the UI and removes all Firebase listeners.
*/
function cleanupUi() {
// Stop all currently listening Firebase listeners.
listeningFirebaseRefs.forEach(function(ref) {
ref.off();
});
listeningFirebaseRefs = [];
}
/**
* The ID of the currently signed-in User. We keep track of this to detect Auth state change events that are just
* programmatic token refresh but not a User status change.
*/
var currentUID;
/**
* Triggers every time there is a change in the Firebase auth state (i.e. user signed-in or user signed out).
*/
function onAuthStateChanged(user) {
// We ignore token refresh events.
if (user && currentUID === user.uid) {
return;
}
cleanupUi();
if (user) {
currentUID = user.uid;
splashPage.style.display = 'none';
writeUserData(user.uid, user.displayName, user.email, user.photoURL);
startDatabaseQueries();
} else {
// Set currentUID to null.
currentUID = null;
// Display the splash page where you can sign-in.
splashPage.style.display = '';
}
}
/**
* Displays the given section element and changes styling of the given button.
*/
function showSection(sectionElement, buttonElement) {
recentPostsSection.style.display = 'none';
userPostsSection.style.display = 'none';
topUserPostsSection.style.display = 'none';
recentMenuButton.classList.remove('is-active');
myPostsMenuButton.classList.remove('is-active');
myTopPostsMenuButton.classList.remove('is-active');
if (sectionElement) {
sectionElement.style.display = 'block';
}
if (buttonElement) {
buttonElement.classList.add('is-active');
}
}
// Bindings on load.
window.addEventListener('load', function() {
// Bind Sign in button.
signInButton.addEventListener('click', function() {
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider);
});
// Bind Sign out button.
signOutButton.addEventListener('click', function() {
firebase.auth().signOut();
});
// Listen for auth state changes
firebase.auth().onAuthStateChanged(onAuthStateChanged);
// Bind menu buttons.
recentMenuButton.onclick = function() {
showSection(recentPostsSection, recentMenuButton);
};
myPostsMenuButton.onclick = function() {
showSection(userPostsSection, myPostsMenuButton);
};
myTopPostsMenuButton.onclick = function() {
showSection(topUserPostsSection, myTopPostsMenuButton);
};
recentMenuButton.onclick();
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment