* Starts listening for new posts and populates posts lists.
()
| 425 | * Starts listening for new posts and populates posts lists. |
| 426 | */ |
| 427 | function startDatabaseQueries() { |
| 428 | const myUserId = auth.currentUser!.uid; |
| 429 | const topUserPostsRef = query( |
| 430 | ref(database, 'user-posts/' + myUserId + '/starCount'), |
| 431 | orderByChild('starCount'), |
| 432 | ); |
| 433 | const recentPostsRef = query(ref(database, 'posts'), limitToLast(100)); |
| 434 | const userPostsRef = ref(database, 'user-posts/' + myUserId); |
| 435 | |
| 436 | const fetchPosts = function (postsRef: Query, sectionElement: HTMLElement) { |
| 437 | onChildAdded(postsRef, function (data) { |
| 438 | const author = data.val().author || 'Anonymous'; |
| 439 | const containerElement = |
| 440 | sectionElement.getElementsByClassName('posts-container')[0]; |
| 441 | containerElement.insertBefore( |
| 442 | createPostElement( |
| 443 | data.key!, |
| 444 | data.val().title, |
| 445 | data.val().body, |
| 446 | author, |
| 447 | data.val().uid, |
| 448 | data.val().authorPic, |
| 449 | ), |
| 450 | containerElement.firstChild, |
| 451 | ); |
| 452 | }); |
| 453 | |
| 454 | onChildChanged(postsRef, function (data) { |
| 455 | const containerElement = |
| 456 | sectionElement.getElementsByClassName('posts-container')[0]; |
| 457 | const postElement = containerElement.getElementsByClassName( |
| 458 | 'post-' + data.key, |
| 459 | )[0]; |
| 460 | const title = postElement.getElementsByClassName( |
| 461 | 'mdl-card__title-text', |
| 462 | )[0] as HTMLHeadingElement; |
| 463 | const text = postElement.getElementsByClassName( |
| 464 | 'text', |
| 465 | )[0] as HTMLDivElement; |
| 466 | const username = postElement.getElementsByClassName( |
| 467 | 'username', |
| 468 | )[0] as HTMLDivElement; |
| 469 | const starCount = postElement.getElementsByClassName( |
| 470 | 'star-count', |
| 471 | )[0] as HTMLDivElement; |
| 472 | |
| 473 | title.innerText = data.val().title; |
| 474 | username.innerText = data.val().author; |
| 475 | text.innerText = data.val().body; |
| 476 | starCount.innerText = data.val().starCount; |
| 477 | }); |
| 478 | |
| 479 | onChildRemoved(postsRef, function (data) { |
| 480 | const containerElement = |
| 481 | sectionElement.getElementsByClassName('posts-container')[0]; |
| 482 | const post = containerElement.getElementsByClassName( |
| 483 | 'post-' + data.key, |
| 484 | )[0]; |
no test coverage detected