Commit 1dcc4b64 authored by Sasini Perera's avatar Sasini Perera

Merge branch 'master' of http://gitlab.sliit.lk/tmp-23-068/tmp-23-068 into suggestions_chatbot

parents a128f39e 1713a20b
This diff is collapsed.
import React, { useState, useEffect } from "react"; import React, { useEffect } from 'react';
import { View, Text, StyleSheet } from "react-native"; import { View, Text, Image, ScrollView, TouchableOpacity, StyleSheet } from 'react-native';
import { ScrollView } from "react-native-gesture-handler"; import { firebase } from '../config'; // Make sure to import 'firestore' and 'firebase' correctly.
import { firebase } from "../config";
const Plan = ({ bmi }) => { export default function Plan({ route }) {
const [mealPlan, setMealPlan] = useState(null); // Extract nutritionPlan from route.params
const { nutritionPlan } = route.params;
useEffect(() => { // Function to save the nutritionPlan to Firestore
// Fetch the meal plan based on the BMI value from Firebase
const fetchMealPlan = async () => { const savePlanToDatabase = async (nutritionPlan) => {
try { try {
const mealPlanSnapshot = await firebase const timestamp = firebase.firestore.FieldValue.serverTimestamp();
.firestore() const data = {
.collection("nutrition_plan") nutritionPlan: nutritionPlan,
.where("bmiRange", "==", getBmiRange(bmi)) date: timestamp,
.get(); };
if (!mealPlanSnapshot.empty) { const docRef = await firebase.firestore().collection("mealPlans").add(data);
// Assuming there's only one matching meal plan, but you can handle multiple results if needed console.log("Meal plan saved with ID:", docRef.id);
const mealPlanData = mealPlanSnapshot.docs[0].data(); } catch (error) {
setMealPlan(mealPlanData); console.error('Error saving Nutrition Plan to Firestore: ', error);
}
} catch (error) {
console.error("Error fetching meal plan:", error);
}
};
if (bmi) {
fetchMealPlan();
}
}, [bmi]);
// Function to determine the BMI range based on value
const getBmiRange = (bmiValue) => {
// Define your BMI ranges and return the appropriate range based on the value
// Example:
if (bmiValue < 18.5) {
return "Underweight";
} else if (bmiValue >= 18.5 && bmiValue < 24.9) {
return "Normal";
} else if (bmiValue >= 24.9 && bmiValue < 29.9) {
return "Overweight";
} else {
return "Obese";
} }
}; }
return ( return (
<View style={styles.container}> <ScrollView contentContainerStyle={styles.container}>
<Text style={styles.label}>Meal Plan:</Text> {/* Image at the top */}
{mealPlan ? ( <Image
<View> source={require('../assets/bmi.jpg')}
<Text>{mealPlan.breakfast}</Text> style={styles.image}
<Text>{mealPlan.lunch}</Text> />
<Text>{mealPlan.dinner}</Text>
</View> {/* Nutrition Plan */}
) : ( <Text style={styles.title}>Nutrition Plan:</Text>
<Text>No meal plan available .</Text> <Text style={styles.nutritionPlan}>{nutritionPlan}</Text>
)}
</View> {/* Save Plan Button */}
<TouchableOpacity style={styles.button} onPress={savePlanToDatabase}>
<Text>Save Plan</Text>
</TouchableOpacity>
</ScrollView>
); );
}; }
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
flex: 1, padding: 16,
alignItems: "center", },
justifyContent: "center", image: {
width: '100%',
height: 200, // Adjust the height as needed
}, },
label: { title: {
fontSize: 24, fontSize: 20,
fontWeight: "bold", fontWeight: 'bold',
marginBottom: 10, marginTop: 16,
},
nutritionPlan: {
fontSize: 16,
marginTop: 8,
},
button: {
backgroundColor: "#FF9DD2",
padding: 12,
borderRadius: 8,
alignItems: 'center',
marginTop: 16,
},
buttonText: {
color: 'white',
}, },
}); });
export default Plan;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment