Skip to content

Commit 86ffd6b

Browse files
committed
use type
1 parent cc204da commit 86ffd6b

File tree

6 files changed

+65
-116
lines changed

6 files changed

+65
-116
lines changed

components/modals/FoodDetailsModal.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ export function FoodDetailsModal({
6161

6262
// Extract nutritional data from API response
6363
const getNutritionalData = () => {
64-
if (isSuccessFoodProductState(productDetails)) {
65-
const nutrients = productDetails.product.nutrition.aggregated_set?.nutrients;
64+
if (
65+
isSuccessFoodProductState(productDetails) &&
66+
productDetails.product.nutrition?.aggregated_set?.nutrients
67+
) {
68+
const nutrients = productDetails.product.nutrition.aggregated_set.nutrients;
6669
return {
6770
calories: nutrients?.['energy-kcal']?.value || 0,
6871
protein: nutrients?.proteins?.value || 0,

components/modals/FoodSearchModal.tsx

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { theme, addOpacityToHex } from '../../theme';
1515
import { FullScreenModal } from './FullScreenModal';
1616
import { FoodDetailsModal } from './FoodDetailsModal';
1717
import { useFoodSearch, useProductDetails } from '../../hooks/useSearchFood';
18-
import { type FoodProduct } from '../../types/openFoodFacts';
18+
import { type SearchResultProduct } from '../../types/openFoodFacts';
1919

2020
type FoodItem = {
2121
id: string;
@@ -323,36 +323,34 @@ export function FoodSearchModal({
323323
{ id: 'recipes', label: t('foodSearch.filters.recipes') },
324324
];
325325

326-
const handleFoodClick = (food: FoodProduct) => {
327-
setSelectedBarcode(food.id);
326+
const handleFoodClick = (food: SearchResultProduct) => {
327+
setSelectedBarcode(food.code || null);
328328

329-
// Convert FoodProduct to FoodItem format
329+
// Convert SearchResultProduct to FoodItem format
330330
const foodItem: FoodItem = {
331-
id: food.id,
332-
name: food.name,
333-
description: `${food.brand || 'Generic'}${formatCalories(food)}`,
334-
brand: food.brand,
331+
id: food.code || String(Math.random()),
332+
name: food.product_name || 'Unknown Product',
333+
description: `${food.brands || food.generic_name || 'Generic'}${formatCalories(food)}`,
334+
brand: food.brands,
335335
imageUrl: food.image_url,
336336
serving_size: food.serving_size,
337337
nutriments: food.nutriments,
338-
_raw: food._raw,
338+
_raw: food,
339339
};
340340

341341
setSelectedFood(foodItem);
342342
setIsFoodDetailsVisible(true);
343343
};
344344

345-
const formatCalories = useCallback((food: FoodProduct) => {
346-
const kcalPer100g = food.nutriments?.['energy-kcal_100g'];
347-
const kcalServing = food.nutriments?.['energy-kcal_serving'];
345+
const formatCalories = useCallback((food: SearchResultProduct) => {
346+
const kcal = food.nutriments?.['energy-kcal'];
348347

349-
if (kcalPer100g) {
350-
return `${Math.round(kcalPer100g)} kcal / 100g`;
348+
if (kcal) {
349+
return `${Math.round(kcal)} kcal`;
351350
}
352-
if (kcalServing && food.serving_size) {
353-
return `${Math.round(kcalServing)} kcal / ${food.serving_size}`;
354-
}
355-
return 'Nutrition info unavailable';
351+
352+
// TODO: calculate using macro values
353+
return 'N/A';
356354
}, []);
357355

358356
const headerRight = (
@@ -464,16 +462,16 @@ export function FoodSearchModal({
464462
{!isLoading && !error && foods.length > 0
465463
? foods.map((food) => (
466464
<FoodItemCard
467-
key={food.id}
465+
key={food.code}
468466
food={{
469-
id: food.id,
470-
name: food.name,
471-
description: `${food.brand || 'Generic'}${formatCalories(food)}`,
472-
brand: food.brand,
467+
id: food.code || String(Math.random()),
468+
name: food.product_name || 'Unknown Product',
469+
description: `${food.brands || food.generic_name || 'Generic'}${formatCalories(food)}`,
470+
brand: food.brands,
473471
imageUrl: food.image_url,
474472
serving_size: food.serving_size,
475473
nutriments: food.nutriments,
476-
_raw: food._raw,
474+
_raw: food,
477475
}}
478476
onAddPress={() => handleFoodClick(food)}
479477
/>

hooks/useSearchFood.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useQuery } from '@tanstack/react-query';
22
import { OpenFoodFacts } from '@openfoodfacts/openfoodfacts-nodejs';
33
import { fetch } from 'expo/fetch';
4-
import { FoodProduct } from '../types/openFoodFacts';
4+
import { SearchResultProduct } from '../types/openFoodFacts';
55

66
const getClient = () => {
77
return new OpenFoodFacts(fetch as any);
@@ -31,20 +31,9 @@ export function useFoodSearch(searchTerm: string) {
3131
return [];
3232
}
3333

34-
const products: FoodProduct[] = result.products
35-
.filter((product: FoodProduct) => product.product_name)
36-
.map((product: FoodProduct) => ({
37-
id: product.code || String(Math.random()),
38-
name: product.product_name || 'Unknown Product',
39-
brand: product.brands || product.generic_name || 'Generic',
40-
nutriments: product.nutriments,
41-
serving_size: product.serving_size,
42-
product_name: product.product_name,
43-
brands: product.brands,
44-
categories: product.categories,
45-
image_url: product.image_small_url || product.image_url,
46-
_raw: product,
47-
}));
34+
const products: SearchResultProduct[] = result.products.filter(
35+
(product: SearchResultProduct) => product.product_name
36+
);
4837

4938
return products;
5039
},

ios/musclog.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
13B07F8E1A680F5B00A75B9A /* Resources */,
107107
00DD1BFF1BD5951E006B06BC /* Bundle React Native code and images */,
108108
800E24972A6A228C8D4807E9 /* [CP] Copy Pods Resources */,
109-
BF47C61C6ED44BF0BE2FB64A /* Upload Debug Symbols to Sentry */,
109+
D92FBD4930904514894FFC43 /* Upload Debug Symbols to Sentry */,
110110
);
111111
buildRules = (
112112
);
@@ -223,7 +223,7 @@
223223
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-musclog/Pods-musclog-resources.sh\"\n";
224224
showEnvVarsInLog = 0;
225225
};
226-
BF47C61C6ED44BF0BE2FB64A /* Upload Debug Symbols to Sentry */ = {
226+
D92FBD4930904514894FFC43 /* Upload Debug Symbols to Sentry */ = {
227227
isa = PBXShellScriptBuildPhase;
228228
buildActionMask = 2147483647;
229229
files = (

types/guards/openFoodFacts.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { SuccessFoodProductState } from '../openFoodFacts';
22

33
// Type guard function to check if productDetails is a success state with food product and nutrition data
4-
export const isSuccessFoodProductState = (
5-
state: any
6-
): state is SuccessFoodProductState => {
4+
export const isSuccessFoodProductState = (state: any): state is SuccessFoodProductState => {
75
return (
86
state?.status === 'success' &&
97
state?.product?.product_type === 'food' &&

types/openFoodFacts.ts

Lines changed: 31 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,37 @@
1-
import type { Product, ProductStateV3 } from '@openfoodfacts/openfoodfacts-nodejs';
1+
// Import types from Open Food Facts library
2+
import type {
3+
Product as ProductV3,
4+
ProductStateV3,
5+
Product as ProductV2,
6+
SearchResult as V2SearchResult,
7+
} from '@openfoodfacts/openfoodfacts-nodejs';
28

3-
export type { Product, ProductStateV3 as ProductState };
9+
// Re-export the main types we need from the library
10+
export type { ProductV3, ProductStateV3 as ProductState, ProductV2, V2SearchResult };
411

5-
// Create a more specific type for food products with nutrition data
6-
export interface FoodProductWithNutrition extends Product {
7-
product_type: 'food';
8-
nutrition: {
9-
aggregated_set?: {
10-
nutrients?: {
11-
'energy-kcal'?: { value?: number };
12-
proteins?: { value?: number };
13-
carbohydrates?: { value?: number };
14-
fat?: { value?: number };
15-
fiber?: { value?: number };
16-
sugars?: { value?: number };
17-
'saturated-fat'?: { value?: number };
18-
sodium?: { value?: number };
19-
salt?: { value?: number };
20-
[key: string]: { value?: number } | undefined;
21-
};
22-
};
23-
};
24-
}
12+
// Type for search results (using V2 types from the library)
13+
export type SearchResultProduct = ProductV2;
2514

26-
// Type for successful product state with nutrition data
15+
// Type guard for successful food product state with nutrition data (V3 API)
2716
export interface SuccessFoodProductState {
2817
status: 'success';
29-
product: FoodProductWithNutrition;
30-
}
31-
32-
// Helper type for the nutrients we commonly use
33-
export interface NutrientValues {
34-
calories?: number;
35-
protein?: number;
36-
carbs?: number;
37-
fat?: number;
38-
fiber?: number;
39-
sugars?: number;
40-
saturatedFat?: number;
41-
sodium?: number;
42-
salt?: number;
43-
}
44-
45-
// Legacy types for backward compatibility during migration
46-
export interface Nutriments {
47-
'energy-kcal'?: number;
48-
'energy-kcal_100g'?: number;
49-
proteins?: number;
50-
proteins_100g?: number;
51-
carbohydrates?: number;
52-
carbohydrates_100g?: number;
53-
fat?: number;
54-
fat_100g?: number;
55-
fiber?: number;
56-
fiber_100g?: number;
57-
sugars?: number;
58-
sugars_100g?: number;
59-
'saturated-fat'?: number;
60-
'saturated-fat_100g'?: number;
61-
sodium?: number;
62-
sodium_100g?: number;
63-
salt?: number;
64-
salt_100g?: number;
65-
[key: string]: number | undefined;
66-
}
67-
68-
export interface FoodProduct {
69-
product_type: 'food' | 'beauty' | 'petfood' | 'product';
70-
nutriments?: Nutriments;
71-
product_name?: string;
72-
brands?: string;
73-
categories?: string;
74-
serving_size?: string;
75-
[key: string]: any;
18+
product: ProductV3 & {
19+
product_type: 'food';
20+
nutrition?: {
21+
aggregated_set?: {
22+
nutrients?: {
23+
'energy-kcal'?: { value?: number };
24+
proteins?: { value?: number };
25+
carbohydrates?: { value?: number };
26+
fat?: { value?: number };
27+
fiber?: { value?: number };
28+
sugars?: { value?: number };
29+
'saturated-fat'?: { value?: number };
30+
sodium?: { value?: number };
31+
salt?: { value?: number };
32+
[key: string]: { value?: number } | undefined;
33+
};
34+
};
35+
};
36+
};
7637
}

0 commit comments

Comments
 (0)