Skip to content

Commit

Permalink
fix: deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Dec 16, 2024
1 parent a25563b commit 1ed5e21
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 10 additions & 2 deletions components/cards/pastmeals/PastMealCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
TouchableOpacity,
ImageBackground,
View,
GestureResponderEvent,
Alert,
} from "react-native";
import FontAwesome from "@expo/vector-icons/FontAwesome";
import Ionicons from "@expo/vector-icons/Ionicons";
Expand Down Expand Up @@ -138,8 +140,13 @@ const PastMealCard = ({
},
});

const handleDelete = () => {
deleteMealById(meal.meal_id);
const handleDelete = async (event: GestureResponderEvent) => {
event.stopPropagation();
try {
await deleteMealById(meal.meal_id);
} catch (error) {
Alert.alert("Error", "Failed to delete meal");
}
};

const handleCardPress = () => {
Expand All @@ -161,6 +168,7 @@ const PastMealCard = ({
<TouchableOpacity
style={cardStyles.closeButton}
onPress={handleDelete}
hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
>
<FontAwesome name="close" size={24} color="#A9A9A9" />
</TouchableOpacity>
Expand Down
7 changes: 6 additions & 1 deletion shared/MealsStorageContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,16 @@ export const MealsDatabaseProvider: React.FC<ProviderProps> = ({
};

const deleteMealById = async (mealId: string) => {
// Optimistically remove from state
setDailyMeals((prev) => prev.filter((meal) => meal.meal_id !== mealId));
setWeeklyMeals((prev) => prev.filter((meal) => meal.meal_id !== mealId));

try {
await deleteMealFromDB(mealId);
await refreshMeals();
} catch (error) {
console.error("Error deleting meal:", error);
// If deletion fails, refresh meals to restore state
await refreshMeals();
throw error;
}
};
Expand Down

0 comments on commit 1ed5e21

Please sign in to comment.