Skip to content

Commit

Permalink
add findCafePosts function
Browse files Browse the repository at this point in the history
  • Loading branch information
angelp03 committed Nov 8, 2024
1 parent d1d759b commit a1420da
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 @@ -21,6 +21,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 @@ -76,4 +77,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 a1420da

Please sign in to comment.