-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.js
More file actions
177 lines (151 loc) · 5.11 KB
/
Copy pathcamera.js
File metadata and controls
177 lines (151 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import React, { useState, useEffect } from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
import { BarCodeScanner } from 'expo-barcode-scanner';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import IngredientScreen from "./IngredientScreen";
// import {initializeApp} from "@firebase/app"
import firebase from 'firebase';
import { getDatabase, ref, set } from "firebase/database";
import "firebase/database"
// function writeUserData(userId, name, email, imageUrl) {
// const db = getDatabase();
// set(ref(db, 'users/' + userId), {
// username: name,
// email: email,
// profile_picture : imageUrl
// });
// }
// function writeData(recipeNumber,foodItem,calories) {
// const db = getDatabase();
// set(ref(db, 'recipes/' + recipeNumber), {
// food: foodItem,
// calorieValue: calories,
// });
// }
// function storeIngredient(userId, score) {
// firebase
// .database()
// .ref('users/' + userId)
// .set({
// ingreident: score,
// });
// }
export default function App({navigation}) {
const [hasPermission, setHasPermission] = useState(null);
const [scanned, setScanned] = useState(false);
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState([]);
const [caloriesRoute, setCaloriesRoute] = useState(null);
const [foodItemRoute, setFoodItemRoute] = useState(null);
const [numberOfRecipe, setNumberOfRecipe] = useState(null);
// console.log(data);
function storeIngredient(recipeNumber,foodItem, calories) {
firebase
.database()
.ref('recipes/' + recipeNumber)
.push({
food: foodItem,
calorieValue: calories,
});
// firebase
// .database()
// .ref('recipes/' + recipeNumber)
// .update({
// });
console.log(calories);
}
// useEffect(() => {
// fetch('https://api.nal.usda.gov/fdc/v1/foods/search?api_key=Zv3iWDaJzMh05as8UPFgPiy10NWyAMkNiDR80hg7&query=028400097659')
// .then((response) => response.json())
// .then((json) => setData(json))
// .catch((error) => console.error(error))
// .finally(() => setLoading(false));
// }, []);
useEffect(() => {
(async () => {
const { status } = await BarCodeScanner.requestPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
const handleBarCodeScanned = ({ type, data }) => {
setScanned(true);
alert(`Bar code with type ${type} and data ${data} has been scanned!`);
if (data.length === 13){
data = data.slice(1);
}
console.log(data);
// useFetchFromAPI();
fetch('https://api.nal.usda.gov/fdc/v1/foods/search?api_key=Zv3iWDaJzMh05as8UPFgPiy10NWyAMkNiDR80hg7&query='+ data)
.then((response) => response.json())
.then((json) => {
setData(json);
//console.log(json['foods'][0]['foodNutrients'][3]["value"]);
const calories = json['foods'][0]['foodNutrients'][3]["value"];
const foodItem = json['foods'][0]['lowercaseDescription'];
setCaloriesRoute(json['foods'][0]['foodNutrients'][3]["value"]);
setFoodItemRoute(json['foods'][0]['lowercaseDescription']);
console.log('directly after' + calories)
// console.log(foodItem)
//trying to store in database
const new_num = numberOfRecipe.toString() + "recipe"; //1recipe .. 2recipe.. etc
storeIngredient(new_num,foodItem,calories);
})
.catch((error) => console.error(error))
.finally(() => setLoading(false));
};
//first method.. ended up trying another one
// const useFetchFromAPI = async() =>{
// let response = await fetch(
// 'https://api.nal.usda.gov/fdc/v1/foods/search?api_key=Zv3iWDaJzMh05as8UPFgPiy10NWyAMkNiDR80hg7&query=028400097659'
// );
// let json = await response.json();
// return json;
// }
if (hasPermission === null) {
return <Text>Requesting for camera permission</Text>;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={styles.container}>
<BarCodeScanner
onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
style={StyleSheet.absoluteFillObject}
/>
{scanned && <Button title={'Tap to Scan Again'} onPress={() => setScanned(false)} />}
<View style = {IngredientScreenButtonStyles.container}>
<Button
title="Start new recipe"
onPress={() => {
setNumberOfRecipe(numberOfRecipe + 1);
}}
/>
<Button
title="Go to IngredientScreen"
onPress={() => {
navigation.navigate('IngredientScreen',{
caloriesRoute: caloriesRoute,
foodItemRoute: foodItemRoute,
});
}}
/>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
},
});
const IngredientScreenButtonStyles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-end',
},
});