Files
cally/hooks/firebase/useGetHouseholdName.ts
Milan Paunovic 70db8bdc0b New calendar
2024-12-15 16:29:34 +01:00

37 lines
1.3 KiB
TypeScript

import { useQuery } from "@tanstack/react-query";
import firestore from "@react-native-firebase/firestore";
export const useGetHouseholdName = (familyId: string) => {
return useQuery(
["getHouseholdName", familyId], // Unique query key
async () => {
console.log(`Fetching household name for familyId: ${familyId}`);
try {
// Query the Households collection for the given familyId
const snapshot = await firestore()
.collection("Households")
.where("familyId", "==", familyId)
.get();
if (!snapshot.empty) {
// Extract the name from the first matching document
const householdData = snapshot.docs[0].data();
console.log("Household found:", householdData);
return householdData.name || null; // Return the name or null if missing
} else {
console.log("No household found for the given familyId.");
return null; // Return null if no household found
}
} catch (e) {
console.error("Error fetching household name:", e);
throw e; // Ensure error propagates to the query error handling
}
},
{
enabled: !!familyId, // Only fetch if familyId is provided
staleTime: 5 * 60 * 1000, // Cache the data for 5 minutes
}
);
};