Skip to content

Commit a917788

Browse files
committed
feat: enhance data validation and handling in FishAnalysis component
1 parent aa98df7 commit a917788

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

app/(logs)/fish-analysis.tsx

+21-5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export default function FishAnalysis() {
6262

6363
// Group data by city
6464
data.forEach(item => {
65+
if (!item.location?.details?.city || !item.location?.details?.district) return;
66+
6567
const cityKey = `${item.location.details.city} - ${item.location.details.district}`;
6668
if (!stats[cityKey]) {
6769
stats[cityKey] = {
@@ -77,11 +79,15 @@ export default function FishAnalysis() {
7779
};
7880
}
7981

80-
const timeKey = item.timestamp.time.split(':')[0];
82+
const timeKey = item.timestamp?.time?.split(':')[0] || '0';
8183
stats[cityKey].popularTimes[timeKey] = (stats[cityKey].popularTimes[timeKey] || 0) + 1;
8284
stats[cityKey].totalCatches++;
83-
stats[cityKey].averageTemp = (stats[cityKey].averageTemp * (stats[cityKey].totalCatches - 1) +
84-
item.weather.temperature) / stats[cityKey].totalCatches;
85+
86+
// Only include temperature if it exists
87+
if (item.weather?.temperature) {
88+
const currentTotal = stats[cityKey].averageTemp * (stats[cityKey].totalCatches - 1);
89+
stats[cityKey].averageTemp = (currentTotal + item.weather.temperature) / stats[cityKey].totalCatches;
90+
}
8591

8692
const uniqueUsers = new Set([...Array.from(new Set([item.userId]))]);
8793
stats[cityKey].totalUsers = uniqueUsers.size;
@@ -130,8 +136,18 @@ export default function FishAnalysis() {
130136

131137
querySnapshot.forEach((doc) => {
132138
const docData = doc.data();
133-
if (docData.location && docData.location.coordinates) {
134-
data.push(docData as FishingData);
139+
// Only include entries that have valid location data
140+
if (docData.location?.coordinates?.latitude && docData.location?.coordinates?.longitude) {
141+
// Ensure weather object exists even if empty
142+
const fishingData: FishingData = {
143+
...docData,
144+
weather: docData.weather || {
145+
temperature: 0,
146+
windSpeed: 0,
147+
windDirection: 0
148+
}
149+
} as FishingData;
150+
data.push(fishingData);
135151
}
136152
});
137153

0 commit comments

Comments
 (0)