Skip to content

Commit

Permalink
[BE] Adds Subscription to DB for Instant Reloading.
Browse files Browse the repository at this point in the history
  • Loading branch information
dimacuhamarc committed Jan 25, 2024
1 parent 5b313e1 commit 5ad35ea
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .firebase/hosting.cHVibGlj.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vercel.svg,1706052924543,ddb9254655f2b7733e1193fb66cb4beb95c29ad752d7adbc1765026e124c88c5
404.html,1706163261501,b6abfbdc894d37c260154e281499dc6415bb6ad76b32f01ef94dee93aa897ac4
next.svg,1706052924541,7b16665552cb47db34db342505d6d340caaf5813ca2cba1df3536ed67ea33f64
index.html,1706163261635,cb6288c3065e2c209919712849176d45f961b9241afeecb35578459ae6fe9f5a
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "campus-fw"
}
}
19 changes: 19 additions & 0 deletions .github/workflows/firebase-hosting-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on merge
'on':
push:
branches:
- main
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_CAMPUS_FW }}'
channelId: live
projectId: campus-fw
16 changes: 16 additions & 0 deletions .github/workflows/firebase-hosting-pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on PR
'on': pull_request
jobs:
build_and_preview:
if: '${{ github.event.pull_request.head.repo.full_name == github.repository }}'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_CAMPUS_FW }}'
projectId: campus-fw
10 changes: 10 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
33 changes: 33 additions & 0 deletions public/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Page Not Found</title>

<style media="screen">
body { background: #ECEFF1; color: rgba(0,0,0,0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; }
#message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px 16px; border-radius: 3px; }
#message h3 { color: #888; font-weight: normal; font-size: 16px; margin: 16px 0 12px; }
#message h2 { color: #ffa100; font-weight: bold; font-size: 16px; margin: 0 0 8px; }
#message h1 { font-size: 22px; font-weight: 300; color: rgba(0,0,0,0.6); margin: 0 0 16px;}
#message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; }
#message a { display: block; text-align: center; background: #039be5; text-transform: uppercase; text-decoration: none; color: white; padding: 16px; border-radius: 4px; }
#message, #message a { box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); }
#load { color: rgba(0,0,0,0.4); text-align: center; font-size: 13px; }
@media (max-width: 600px) {
body, #message { margin-top: 0; background: white; box-shadow: none; }
body { border-top: 16px solid #ffa100; }
}
</style>
</head>
<body>
<div id="message">
<h2>404</h2>
<h1>Page Not Found</h1>
<p>The specified file was not found on this website. Please check the URL for mistakes and try again.</p>
<h3>Why am I seeing this?</h3>
<p>This page was generated by the Firebase Command-Line Interface. To modify it, edit the <code>404.html</code> file in your project's configured <code>public</code> directory.</p>
</div>
</body>
</html>
89 changes: 89 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Welcome to Firebase Hosting</title>

<!-- update the version number as needed -->
<script defer src="/__/firebase/10.7.2/firebase-app-compat.js"></script>
<!-- include only the Firebase features as you need -->
<script defer src="/__/firebase/10.7.2/firebase-auth-compat.js"></script>
<script defer src="/__/firebase/10.7.2/firebase-database-compat.js"></script>
<script defer src="/__/firebase/10.7.2/firebase-firestore-compat.js"></script>
<script defer src="/__/firebase/10.7.2/firebase-functions-compat.js"></script>
<script defer src="/__/firebase/10.7.2/firebase-messaging-compat.js"></script>
<script defer src="/__/firebase/10.7.2/firebase-storage-compat.js"></script>
<script defer src="/__/firebase/10.7.2/firebase-analytics-compat.js"></script>
<script defer src="/__/firebase/10.7.2/firebase-remote-config-compat.js"></script>
<script defer src="/__/firebase/10.7.2/firebase-performance-compat.js"></script>
<!--
initialize the SDK after all desired features are loaded, set useEmulator to false
to avoid connecting the SDK to running emulators.
-->
<script defer src="/__/firebase/init.js?useEmulator=true"></script>

<style media="screen">
body { background: #ECEFF1; color: rgba(0,0,0,0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; }
#message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px; border-radius: 3px; }
#message h2 { color: #ffa100; font-weight: bold; font-size: 16px; margin: 0 0 8px; }
#message h1 { font-size: 22px; font-weight: 300; color: rgba(0,0,0,0.6); margin: 0 0 16px;}
#message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; }
#message a { display: block; text-align: center; background: #039be5; text-transform: uppercase; text-decoration: none; color: white; padding: 16px; border-radius: 4px; }
#message, #message a { box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); }
#load { color: rgba(0,0,0,0.4); text-align: center; font-size: 13px; }
@media (max-width: 600px) {
body, #message { margin-top: 0; background: white; box-shadow: none; }
body { border-top: 16px solid #ffa100; }
}
</style>
</head>
<body>
<div id="message">
<h2>Welcome</h2>
<h1>Firebase Hosting Setup Complete</h1>
<p>You're seeing this because you've successfully setup Firebase Hosting. Now it's time to go build something extraordinary!</p>
<a target="_blank" href="https://firebase.google.com/docs/hosting/">Open Hosting Documentation</a>
</div>
<p id="load">Firebase SDK Loading&hellip;</p>

<script>
document.addEventListener('DOMContentLoaded', function() {
const loadEl = document.querySelector('#load');
// // 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
// // The Firebase SDK is initialized and available here!
//
// firebase.auth().onAuthStateChanged(user => { });
// firebase.database().ref('/path/to/ref').on('value', snapshot => { });
// firebase.firestore().doc('/foo/bar').get().then(() => { });
// firebase.functions().httpsCallable('yourFunction')().then(() => { });
// firebase.messaging().requestPermission().then(() => { });
// firebase.storage().ref('/path/to/ref').getDownloadURL().then(() => { });
// firebase.analytics(); // call to activate
// firebase.analytics().logEvent('tutorial_completed');
// firebase.performance(); // call to activate
//
// // 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥

try {
let app = firebase.app();
let features = [
'auth',
'database',
'firestore',
'functions',
'messaging',
'storage',
'analytics',
'remoteConfig',
'performance',
].filter(feature => typeof app[feature] === 'function');
loadEl.textContent = `Firebase SDK loaded with ${features.join(', ')}`;
} catch (e) {
console.error(e);
loadEl.textContent = 'Error loading the Firebase SDK, check the console.';
}
});
</script>
</body>
</html>
5 changes: 3 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import Gallery from "@/components/Gallery";
import SubmitPost from "@/components/SubmitPost";


export default function Home() {
return (
<main className="flex min-h-screen flex-col items-center justify-start p-24">
<div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
<h1 className="text-2xl">CAMPUS FREEDOM WALL</h1>
<SubmitPost/>
<h1 className="text-2xl">The Campus Wall</h1>
<h3>Created by [email protected] for Org Week 2024</h3>
<SubmitPost />
</div>
<Gallery/>
</main>
Expand Down
58 changes: 40 additions & 18 deletions src/components/Gallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Link from 'next/link';
import { useState, useEffect } from 'react';
import { db } from '../configs/firebase';
import { getDocs, collection } from 'firebase/firestore';
import { getDocs, collection, onSnapshot } from 'firebase/firestore';

interface Post {
id: string;
Expand All @@ -22,27 +22,43 @@ export default function Gallery() {
const [posts, setPosts] = useState<Post[]>([]);

const postCollectionRef = collection(db, 'posts');
const getPosts = async () => {
try {
const data = await getDocs(postCollectionRef);
const filteredData = data.docs.map((doc) => ({
...doc.data(),
id: doc.id,
})) as Post[];
console.log(filteredData);
setPosts(filteredData);
} catch (error) {
// console.log(error);
}
};

useEffect(() => {
getPosts();
}, []);

useEffect(() => {
const getPosts = async () => {
try {
const data = await getDocs(postCollectionRef);
const filteredData = data.docs.map((doc) => ({
...doc.data(),
id: doc.id,
})) as Post[];
// console.log(filteredData);
setPosts(filteredData);
} catch (error) {
// console.log(error);
}
};
getPosts();

// Subscribe to document changes
const unsubscribe = onSnapshot(postCollectionRef, (snapshot) => {
const updatedPosts = snapshot.docs.map((doc) => ({
...doc.data(),
id: doc.id,
})) as Post[];
setPosts(updatedPosts);
});

// Clean up the subscription when the component is unmounted
return () => unsubscribe();
}, []);

return (
<div className="w-full container mx-auto my-4 p-4">
<h2 className="text-3xl font-bold mb-6">Latest Freedom Wall Posts</h2>
<h2 className="text-3xl font-bold mb-6">Latest Submissions</h2>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4">
{posts.map((post) =>
post
Expand All @@ -69,13 +85,17 @@ function Post(
) {
function formatDate(date: FirestoreTimestamp): string {
const timestamp = new Date(date.seconds * 1000 + date.nanoseconds / 1e6);
timestamp.setHours(timestamp.getHours() + 12);
if (timestamp.getHours() > 12) {
timestamp.setHours(timestamp.getHours() + 12);
}
const options: Intl.DateTimeFormatOptions = {
timeZone: 'Asia/Manila',
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: '2-digit',
minute: 'numeric',
hour12: false,
};
const formattedDate = timestamp.toLocaleDateString('en-PH', options);
return formattedDate;
Expand All @@ -84,11 +104,11 @@ function Post(
return (
<div
key={id}
className="container max-w-sm bg-white border border-gray-200 rounded-lg shadow dark:bg-gray-800 dark:border-gray-700 hover:scale-105"
className="container max-w-sm h-full bg-white border border-gray-200 rounded-lg shadow dark:bg-gray-800 dark:border-gray-700 hover:scale-105"
>
<div className="h-full p-5 flex flex-col gap-4 justify-between">
<h2 className="text-xs">{id}</h2>
<h5 className="mb-2 text-2xl font-bold tracking-tight text-pink-700 dark:text-white">
<h5 className="mb-2 text-2xl h-full font-bold tracking-tight text-pink-700 dark:text-white">
{content}
</h5>
<div className="container">
Expand Down Expand Up @@ -124,3 +144,5 @@ function Post(
</div>
);
}


Loading

0 comments on commit 5ad35ea

Please sign in to comment.