Fix stuff

This commit is contained in:
Milan Paunovic
2024-12-15 17:54:01 +01:00
parent c411990312
commit 818107efe3
2 changed files with 16 additions and 22 deletions

View File

@ -31,7 +31,7 @@ interface FormattedEvent {
// Precompute time constants // Precompute time constants
const DAY_IN_MS = 24 * 60 * 60 * 1000; const DAY_IN_MS = 24 * 60 * 60 * 1000;
const PERIOD_IN_MS = 45 * DAY_IN_MS; const PERIOD_IN_MS = 5 * DAY_IN_MS;
const TIME_ZONE = Intl.DateTimeFormat().resolvedOptions().timeZone; const TIME_ZONE = Intl.DateTimeFormat().resolvedOptions().timeZone;
// Memoize date range calculations // Memoize date range calculations

View File

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