Getting nutrition value for a FoodItem

In this we get nutritional value form PassioID and PassioFoodItem ?

/* You can utilize the getMacroNutrientsFormPassioFoodItem method, 
  declaring it as shown below, to retrieve specific
  macro or micronutrient information from a recipe.
*/
  
  useEffect(() => {
    // Fetch Passio Food Item details when component mounts
    const init = async () => {
      const result = await PassioSDK.getAttributesForPassioID(passioID);
      if (result && result.foodItem) {
        // Set macro and micronutrients based on Passio Food Item
        const macrosNutrients = getMacroNutrientsFormPassioFoodItem(result.foodItem);
        const microsNutrients = getMicroNutrientsFormPassioFoodItem(result.foodItem);
      }
    };

    init();
  }, [passioID]);


 const micros: NutrientsTypes[] = [
  'saturatedFat',
  'transFat',
  'polyunsaturatedFat',
  'sodium',
  'fiber',
  'sugar',
  'sugarAdded',
  'vitaminD',
  'iron',
  'potassium',
  'vitaminA',
  'alcohol',
  'sugarAlcohol',
  'vitaminB12',
  'vitaminB12Added',
  'vitaminB6',
  'vitaminE',
  'vitaminEAdded',
  'phosphorus',
  'iodine',
  'cholesterol',
];

 const macros: NutrientsTypes[] = ['calories', 'carbs', 'protein', 'fat'];

Methods

NutrientsTypes, Nutrients

// Define the types of nutrients
export type NutrientsTypes =
  | 'calories'
  | 'protein'
  | 'carbs'
  | 'fat'
  | 'saturatedFat'
  | 'transFat'
  | 'polyunsaturatedFat'
  | 'sodium'
  | 'fiber'
  | 'sugar'
  | 'sugarAdded'
  | 'vitaminD'
  | 'iron'
  | 'potassium'
  | 'vitaminA'
  | 'vitaminC'
  | 'alcohol'
  | 'sugarAlcohol'
  | 'vitaminB12'
  | 'vitaminB12Added'
  | 'vitaminB6'
  | 'vitaminE'
  | 'vitaminEAdded'
  | 'phosphorus'
  | 'iodine'
  | 'cholesterol';

// Define the structure of individual nutrients
export interface Nutrients {
  unitMass?: UnitMass;
  name: NutrientsTypes;
}

getNutrient method

// Get a single nutrient from Passio Food Item
export const getNutrient = (
  foodItem: PassioFoodItem,
  name: NutrientsTypes
): Nutrients | null => {
  if (foodItem[name]) {
    return {
      name,
      unitMass: foodItem[name] ?? undefined,
    } as Nutrients;
  } else {
    return null;
  }
};

getNutrients method

// Get multiple nutrients from Passio FoodItem
const getNutrients = (
  foodItem: PassioFoodItem,
  nutrientNames: NutrientsTypes[]
): Nutrients[] => {
  return nutrientNames
    .map((name) => {
      // General case for other nutrients
      return getNutrient(foodItem, name);
    })
    .filter((item): item is Nutrients => !!item);
};

Example

MacroInfo

import React, { useState, useEffect } from 'react';
import { StyleSheet, View } from 'react-native';
import {
  PassioSDK,
  type PassioID,
} from '@passiolife/nutritionai-react-native-sdk-v2';
import { Text } from 'react-native';
import { FlatList } from 'react-native';
import {
  formatNutrient,
  getMacroNutrientsFormPassioFoodItem,
  getMicroNutrientsFormPassioFoodItem,
  type Nutrients,
} from './MacroUtils';

export const MacroInfo = ({ passioID }: { passioID: PassioID }) => {
  const [macroNutrients, setMacroNutrients] = useState<Nutrients[]>();
  const [microNutrients, setMicroNutrients] = useState<Nutrients[]>();

  useEffect(() => {
    // Fetch Passio Food Item details when component mounts
    const init = async () => {
      const result = await PassioSDK.getAttributesForPassioID(passioID);
      if (result && result.foodItem) {
        // Set macro and micro nutrients based on Passio Food Item
        setMacroNutrients(getMacroNutrientsFormPassioFoodItem(result.foodItem));
        setMicroNutrients(getMicroNutrientsFormPassioFoodItem(result.foodItem));
      }
    };

    init();
  }, [passioID]);

  // Function to render individual nutrient information
  const renderInformation = ({ item }: { item: Nutrients }) => {
    const { name, unitMass } = item;
    return (
      <View style={styles.infoRow}>
        <Text style={[styles.infoText, styles.infoTitle]}>{name}</Text>
        <Text style={[styles.infoText, styles.infoValue]}>
          {formatNutrient(unitMass?.value ?? 0)}
        </Text>
      </View>
    );
  };

  return (
    <View style={styles.container}>
      {/* Display Macro Nutrients */}
      <Text style={styles.heading}>Macro Nutrients</Text>
      {macroNutrients && (
        <FlatList data={macroNutrients} renderItem={renderInformation} />
      )}

      {/* Display Micro Nutrients */}
      <Text style={styles.heading}>Micro Nutrients</Text>
      {microNutrients && (
        <FlatList data={microNutrients} renderItem={renderInformation} />
      )}
    </View>
  );
};

// Styles for the component
export const styles = StyleSheet.create({
  container: { flex: 1, paddingHorizontal: 16 },
  heading: { fontSize: 18, fontWeight: '600', marginVertical: 16 },
  infoRow: { flexDirection: 'row', marginVertical: 4 },
  infoText: { fontSize: 16 },
  infoTitle: { flex: 1 },
  infoValue: {},
});

Utility Method

import {
  type PassioFoodItem,
  type UnitMass,
} from '@passiolife/nutritionai-react-native-sdk-v2';


// Get micro nutrients from Passio FoodItem
export const getMicroNutrientsFormPassioFoodItem = (
  foodItem: PassioFoodItem
): Nutrients[] => {
  const microNutrientNames: NutrientsTypes[] = [
    'saturatedFat',
    'transFat',
    'polyunsaturatedFat',
    'sodium',
    'fiber',
    'sugar',
    'sugarAdded',
    'vitaminD',
    'iron',
    'potassium',
    'vitaminA',
    'alcohol',
    'sugarAlcohol',
    'vitaminB12',
    'vitaminB12Added',
    'vitaminB6',
    'vitaminE',
    'vitaminEAdded',
    'phosphorus',
    'iodine',
    'cholesterol',
  ];
  return getNutrients(foodItem, microNutrientNames);
};

// Get micro nutrients from Passio FoodItem
export const getMacroNutrientsFormPassioFoodItem = (
  foodItem: PassioFoodItem
): Nutrients[] => {
  const macros: NutrientsTypes[] = ['calories', 'carbs', 'protein', 'fat'];
  return getNutrients(foodItem, macros);
};

// Function to format nutrient value
export const formatNutrient = (value?: number): number => {
  return Number((value ?? 0).toFixed(2));
};

Result

Last updated