Skip to content

Commit

Permalink
Merge pull request #5 from 392-f24/wip-cafe-posts
Browse files Browse the repository at this point in the history
Configure post/message board related backend
  • Loading branch information
alizenart authored Nov 8, 2024
2 parents 8df8a33 + 09df329 commit c0d24e9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/utilities/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const firebaseConfig = {
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const provider = new GoogleAuthProvider();
const database = getDatabase(app);


export const signInWithGoogle = () => {
Expand Down Expand Up @@ -79,4 +80,6 @@ export const useDbUpdate = (path) => {
);

return [updateData, result];
};
};

export {database};
33 changes: 33 additions & 0 deletions src/utilities/posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useState, useEffect } from 'react';
import { onValue, query, orderByChild, equalTo, ref } from 'firebase/database';
import { database } from './firebase';

export const findCafePosts = (cafeId) => {
const [data, setData] = useState(null);
const [error, setError] = useState(null);

useEffect(() => {
if (!cafeId) return

const postsRef = ref(database, `/posts`);
const postsQuery = query(postsRef, orderByChild('cafeId'), equalTo(cafeId));

const unsubscribe = onValue(
postsQuery,
(snapshot) => {
if (snapshot.exists()) {
setData(snapshot.val());
} else {
setData([]);
}
},
(error) => {
setError(error);
}
);

return () => unsubscribe();
}, [cafeId]);

return [data, error];
};

0 comments on commit c0d24e9

Please sign in to comment.