This defines the structure of each nutrient, with a title for its name, a value for its numerical data, and a unit to specify the measurement unit (e.g., grams, calories).
We create a state variable, foodNutrients, using React's useState hook to hold an array of FoodNutrient objects:
We use useEffect to trigger the extraction of nutrient information when the passioFoodItem changes. The PassioSDK.getNutrientsOfPassioFoodItem() method is used to get the nutrients based on the food item and its weight:
Next, we build a UI component to display macro-nutrients like calories, carbohydrates, protein, and fat. In the renderMacro function, we map through a predefined array of macro-nutrients, search for the corresponding nutrient in the foodNutrients array, and display its value:
The renderFoodDetailCard function is a React Native component that generates a card UI displaying detailed information about a food item. It features an icon, the food item's name, and additional information based on the food item's attributes.
Icon Display:
The icon of the food item is displayed within a circular View. The PassioIconView component is used to render the icon based on the iconId and iconSize properties.
Food Name:
The food item's name is displayed in a Text component, styled for clarity with padding, font size, weight, and text transformation.
Additional Information:
The renderMacro() function is called to render macronutrient information.
If passioFoodItem.isOpenFood is true, the renderOpenFood() function is called to display additional details relevant to open food items.
React Native component that generates a user interface for editing serving sizes of a food item. It displays the current serving size, provides an input field for updating the quantity, and offers selectable serving units.
The structure of passioFoodItem is determined by the properties of prop.passioFoodItem, which could include fields such as:
name: The name of the food item.
iconId: An identifier for the food item's icon.
amount: An object containing serving sizes and units.
isOpenFood: A boolean indicating whether the food item is an open food.
The onServingQuantityChange function is a callback that handles changes to the serving quantity input. It updates the state of the input and recalculates the weight based on the quantity entered by the user. This function is optimized with useCallback to prevent unnecessary re-renders.
Purpose
To manage the input for serving quantities, ensuring that the food item's state is updated dynamically based on user input. It calculates the corresponding weight based on the selected serving unit and updates the passioFoodItem state accordingly.
A Text component displays the serving sizes, formatted to show the calculated weight and its unit. The text is styled for emphasis.
Serving Quantity Input:
A TextInput component allows users to input the serving quantity. It is styled with borders, padding, and a placeholder that indicates the expected input.
Selectable Serving Units:
A FlatList displays the available serving units horizontally. Each item in the list is rendered as a Pressable, allowing users to select a unit.
The background color of the selected unit changes to blue, and the text color changes to white for clarity.
The renderIngredient function is a React Native component that displays a list of ingredients for a food item. It checks if the passioFoodItem has ingredients and renders a list of them, including each ingredient's name, amount, and icon.
FlatList:
A FlatList is used to display each ingredient.
Each ingredient is represented by a row, consisting of:
An icon (PassioIconView) displaying the ingredient’s iconId.
The name of the ingredient.
The selected unit or amount of the ingredient.
Render Item:
For each ingredient, a row is rendered, showing:
The ingredient’s icon.
The ingredient’s name.
The selected unit of the ingredient, such as its amount.