-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (120 loc) · 4.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Import stylesheets
import './style.css';
// Firebase App (the core Firebase SDK) is always required and must be listed first
import * as firebase from "firebase/app";
// Add the Firebase products that you want to use
import "firebase/auth";
import "firebase/firestore";
import * as firebaseui from 'firebaseui';
// Document elements
const startRsvpButton = document.getElementById('startRsvp');
const guestbookContainer = document.getElementById('guestbook-container');
const form = document.getElementById('leave-message');
const input = document.getElementById('message');
const guestbook = document.getElementById('guestbook');
const numberAttending = document.getElementById('number-attending');
const rsvpYes = document.getElementById('rsvp-yes');
const rsvpNo = document.getElementById('rsvp-no');
var rsvpListener = null;
var guestbookListener = null;
// Add Firebase project configuration object here
var firebaseConfig = {
apiKey: "AIzaSyAYeLA_27uS-_NcV9thwF95IYTrh5wGYp0",
authDomain: "calgary-u-of-c-dinos.firebaseapp.com",
databaseURL: "https://calgary-u-of-c-dinos.firebaseio.com",
projectId: "calgary-u-of-c-dinos",
storageBucket: "calgary-u-of-c-dinos.appspot.com",
messagingSenderId: "656661211811",
appId: "1:656661211811:web:799f905fcaf7df56da6d16"
};
firebase.initializeApp(firebaseConfig);
// FirebaseUI config
const uiConfig = {
credentialHelper: firebaseui.auth.CredentialHelper.NONE,
signInOptions: [
// Email / Password Provider.
firebase.auth.EmailAuthProvider.PROVIDER_ID
],
callbacks: {
signInSuccessWithAuthResult: function(authResult, redirectUrl){
// Handle sign-in.
// Return false to avoid redirect.
return false;
}
}
};
const ui = new firebaseui.auth.AuthUI(firebase.auth());
startRsvpButton.addEventListener("click",
() => {
if (firebase.auth().currentUser) {
// User is signed in; allows user to sign out
firebase.auth().signOut();
} else {
// No user is signed in; allows user to sign in
ui.start("#firebaseui-auth-container", uiConfig);
}
});
firebase.auth().onAuthStateChanged((user)=> {
if (user) {
startRsvpButton.textContent = "LOGOUT"
// Show guestbook to logged-in users
guestbookContainer.style.display = "block";
// Subscribe to the guestbook collection
subscribeGuestbook();
}
else {
startRsvpButton.textContent = "RSVP"
// Hide guestbook for non-logged-in users
guestbookContainer.style.display = "none";
// Unsubscribe from the guestbook collection
unsubscribeGuestbook();
}
});
form.addEventListener("submit", (e) => {
// Prevent the default form redirect
e.preventDefault();
// Write a new message to the database collection "guestbook"
firebase.firestore().collection("guestbook").add({
text: input.value,
timestamp: Date.now(),
name: firebase.auth().currentUser.displayName,
userId: firebase.auth().currentUser.uid
})
// clear message input field
input.value = "";
// Return false to avoid redirect
return false;
});
// rsvpNo.onclick = () => {
// // Get a reference to the user's document in the attendees collection
// const userDoc = firebase.firestore().collection('attendees').doc(firebase.auth().currentUser.uid);
// // If they RSVP'd no, save a document with attending: false
// userDoc.set({
// attending: false
// }).catch(console.error)
// }
// Listen to guestbook updates
function subscribeGuestbook(){
// Create query for messages
guestbookListener = firebase.firestore().collection("guestbook")
.orderBy("timestamp","desc")
.onSnapshot((snaps) => {
// Reset page
guestbook.innerHTML = "";
// Loop through documents in database
snaps.forEach((doc) => {
// Create an HTML entry for each document and add it to the chat
const entry = document.createElement("p");
entry.textContent = doc.data().name + ": " + doc.data().text;
guestbook.appendChild(entry);
});
});
};
// Unsubscribe from guestbook updates
function unsubscribeGuestbook(){
if (guestbookListener != null)
{
guestbookListener();
guestbookListener = null;
}
};