([]);
-
- useEffect(() => {
- const fetchOrganizations = async () => {
- const data = await getAllOrganizationProfiles();
- setOrganizations(data);
- };
-
- fetchOrganizations();
- }, []);
+ const { organizationProfiles } = useUser(); // Get organization profiles from context
// Filtered organizations based on search query
- const filteredOrganizations = filter(organizations, (org) => {
+ const filteredOrganizations = filter(organizationProfiles, (org) => {
const searchTerm = lowerCase(searchQuery);
return (
lowerCase(org.name).includes(searchTerm) ||
@@ -30,7 +21,7 @@ const Home = () => {
);
});
- return organizations.length > 0 ? (
+ return organizationProfiles.length > 0 ? (
{
- const { savedOrgs, toggleSavedOrg } = useSaved();
+ const { savedOrgs, updateSavedOrgs } = useSavedOrgs();
return (
@@ -15,7 +15,7 @@ const Saved = () => {
toggleSavedOrg(org)}
+ onRemove={() => updateSavedOrgs(org)}
/>
))
) : (
diff --git a/src/utils/firebase/firebaseUtils.ts b/src/utils/firebase/firebaseUtils.ts
index 513b381..f801c46 100644
--- a/src/utils/firebase/firebaseUtils.ts
+++ b/src/utils/firebase/firebaseUtils.ts
@@ -5,7 +5,7 @@ import {
getDoc,
getDocs,
setDoc,
- updateDoc,
+ onSnapshot,
} from 'firebase/firestore';
import { db } from './firebaseConfig';
@@ -29,11 +29,11 @@ const getOrCreateDocument = async >(
if (docSnap.exists()) {
return docSnap.data() as T; // Return existing document data
- } else {
- await setDoc(docRef, defaultData);
- console.info(`New ${collectionName} profile created.`);
- return defaultData; // Return the newly created document data
}
+
+ await setDoc(docRef, defaultData);
+ console.info(`New ${collectionName} profile created.`);
+ return defaultData; // Return the newly created document data
} catch (error) {
console.error(`Error in getOrCreateDocument for ${collectionName}:`, error);
return undefined; // Return undefined on failure
@@ -55,7 +55,7 @@ const updateDocument = async (
): Promise => {
try {
const docRef = doc(db, collectionName, uid);
- await updateDoc(docRef, updates);
+ await setDoc(docRef, updates, { merge: true }); // Merge updates with existing data
console.info(`Document in ${collectionName} updated successfully.`);
} catch (error) {
console.error(`Error updating document in ${collectionName}:`, error);
@@ -64,28 +64,36 @@ const updateDocument = async (
};
/**
- * Get all organization profiles from Firestore.
+ * Listen to all organization profiles in Firestore.
*
- * @returns A promise that resolves to an array of OrganizationProfile objects.
+ * @param callback A function to update the state with the latest organization profiles.
+ * @returns A function to unsubscribe from the Firestore listener.
*/
-const getAllOrganizationProfiles = async (): Promise => {
+const getAllOrganizationProfiles = (
+ callback: (profiles: OrganizationProfile[]) => void
+): (() => void) => {
try {
const organizationsCollection = collection(db, 'organizations');
- const snapshot = await getDocs(organizationsCollection);
- if (snapshot.empty) {
- console.info('No organization profiles found.');
- return [];
- }
+ // Subscribe to Firestore collection updates
+ const unsubscribe = onSnapshot(organizationsCollection, (snapshot) => {
+ if (snapshot.empty) {
+ console.info('No organization profiles found.');
+ callback([]);
+ return;
+ }
+
+ const profiles: OrganizationProfile[] = snapshot.docs.map((doc) => {
+ return { ...doc.data(), uid: doc.id } as OrganizationProfile;
+ });
- const profiles: OrganizationProfile[] = snapshot.docs.map((doc) => {
- return { ...doc.data(), uid: doc.id } as OrganizationProfile;
+ callback(profiles);
});
- return profiles;
+ return unsubscribe; // cleanup
} catch (error) {
- console.error('Error fetching organization profiles:', error);
- return [];
+ console.error('Error listening to organization profiles:', error);
+ throw error;
}
};
diff --git a/src/utils/firebase/index.ts b/src/utils/firebase/index.ts
index 9c37694..190097d 100644
--- a/src/utils/firebase/index.ts
+++ b/src/utils/firebase/index.ts
@@ -1,3 +1,3 @@
export { loginUser, logoutUser } from './auth';
export { auth } from './firebaseConfig';
-export { updateDocument } from './firebaseUtils';
+export { updateDocument, getAllOrganizationProfiles } from './firebaseUtils';
diff --git a/src/utils/firebase/userProfile.ts b/src/utils/firebase/userProfile.ts
index 3345570..297f1b0 100644
--- a/src/utils/firebase/userProfile.ts
+++ b/src/utils/firebase/userProfile.ts
@@ -52,7 +52,7 @@ const getDonorProfile = async (
saved: [],
};
- return getOrCreateDocument('users', uid, defaultProfile);
+ return getOrCreateDocument('donor', uid, defaultProfile);
};
const getOrganizationProfile = async (