@@ -299,6 +299,54 @@ function writeUserData(userId, name, email, imageUrl) {
299
299
}
300
300
// [END basic_write]
301
301
302
+ /**
303
+ * Cleanups the UI and removes all Firebase listeners.
304
+ */
305
+ function cleanupUi ( ) {
306
+ // Remove all previously displayed posts.
307
+ topUserPostsSection . getElementsByClassName ( 'posts-container' ) [ 0 ] . innerHTML = '' ;
308
+ recentPostsSection . getElementsByClassName ( 'posts-container' ) [ 0 ] . innerHTML = '' ;
309
+ userPostsSection . getElementsByClassName ( 'posts-container' ) [ 0 ] . innerHTML = '' ;
310
+
311
+ // Stop all currently listening Firebase listeners.
312
+ listeningFirebaseRefs . forEach ( function ( ref ) {
313
+ ref . off ( ) ;
314
+ } ) ;
315
+ listeningFirebaseRefs = [ ] ;
316
+ }
317
+
318
+ /**
319
+ * Triggers every time there is a change in the Firebase auth state (i.e. user signed-in or user signed out).
320
+ */
321
+ function onAuthStateChanged ( user ) {
322
+ cleanupUi ( ) ;
323
+ if ( user ) {
324
+ splashPage . style . display = 'none' ;
325
+ writeUserData ( user . uid , user . displayName , user . email , user . photoURL ) ;
326
+ startDatabaseQueries ( ) ;
327
+ } else {
328
+ // Display the splash page where you can sign-in.
329
+ splashPage . style . display = '' ;
330
+ }
331
+ }
332
+
333
+ /**
334
+ * Creates a new post for the current user.
335
+ */
336
+ function newPostForCurrentUser ( title , text ) {
337
+ // [START single_value_read]
338
+ var userId = firebase . auth ( ) . currentUser . uid ;
339
+ return firebase . database ( ) . ref ( '/users/' + userId ) . once ( 'value' ) . then ( function ( snapshot ) {
340
+ var username = snapshot . val ( ) . username ;
341
+ // [START_EXCLUDE]
342
+ return writeNewPost ( firebase . auth ( ) . currentUser . uid , username ,
343
+ firebase . auth ( ) . currentUser . photoURL ,
344
+ title , text ) ;
345
+ // [END_EXCLUDE]
346
+ } ) ;
347
+ // [END single_value_read]
348
+ }
349
+
302
350
// Bindings on load.
303
351
window . addEventListener ( 'load' , function ( ) {
304
352
// Bind Sign in button.
@@ -313,47 +361,19 @@ window.addEventListener('load', function() {
313
361
} ) ;
314
362
315
363
// Listen for auth state changes
316
- firebase . auth ( ) . onAuthStateChanged ( function ( user ) {
317
- if ( user ) {
318
- splashPage . style . display = 'none' ;
319
- writeUserData ( user . uid , user . displayName , user . email , user . photoURL ) ;
320
- startDatabaseQueries ( ) ;
321
- } else {
322
- // Display the splash page where you can sign-in.
323
- splashPage . style . display = '' ;
324
-
325
- // Remove all previously displayed posts.
326
- topUserPostsSection . getElementsByClassName ( 'posts-container' ) [ 0 ] . innerHTML = '' ;
327
- recentPostsSection . getElementsByClassName ( 'posts-container' ) [ 0 ] . innerHTML = '' ;
328
- userPostsSection . getElementsByClassName ( 'posts-container' ) [ 0 ] . innerHTML = '' ;
329
-
330
- // Stop all currently listening Firebase listeners.
331
- listeningFirebaseRefs . forEach ( function ( ref ) {
332
- ref . off ( ) ;
333
- } ) ;
334
- listeningFirebaseRefs = [ ] ;
335
- }
336
- } ) ;
364
+ firebase . auth ( ) . onAuthStateChanged ( onAuthStateChanged ) ;
337
365
338
366
// Saves message on form submit.
339
367
messageForm . onsubmit = function ( e ) {
340
368
e . preventDefault ( ) ;
341
- if ( messageInput . value && titleInput . value ) {
342
- var postText = messageInput . value ;
343
- messageInput . value = '' ;
344
- // [START single_value_read]
345
- var userId = firebase . auth ( ) . currentUser . uid ;
346
- firebase . database ( ) . ref ( '/users/' + userId ) . once ( 'value' ) . then ( function ( snapshot ) {
347
- var username = snapshot . val ( ) . username ;
348
- // [START_EXCLUDE]
349
- writeNewPost ( firebase . auth ( ) . currentUser . uid , firebase . auth ( ) . currentUser . displayName ,
350
- firebase . auth ( ) . currentUser . photoURL ,
351
- titleInput . value , postText ) . then ( function ( ) {
352
- myPostsMenuButton . click ( ) ;
353
- } ) ;
354
- // [END_EXCLUDE]
369
+ var text = messageInput . value ;
370
+ var title = titleInput . value ;
371
+ if ( text && title ) {
372
+ newPostForCurrentUser ( title , text ) . then ( function ( ) {
373
+ myPostsMenuButton . click ( ) ;
355
374
} ) ;
356
- // [END single_value_read]
375
+ messageInput . value = '' ;
376
+ titleInput . value = '' ;
357
377
}
358
378
} ;
359
379
0 commit comments