diff --git a/src/App.tsx b/src/App.tsx index 54adf688..d31b6aaf 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,7 +2,7 @@ import React from 'react'; import {NavigationContainer} from '@react-navigation/native'; import {createBottomTabNavigator} from '@react-navigation/bottom-tabs'; import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; -import { Imager } from './screens/imager/Imager'; +import {Imager} from './screens/imager/Imager'; const Tab = createBottomTabNavigator(); diff --git a/src/screens/imager/Imager.jsx b/src/screens/imager/Imager.jsx index 97d55545..dc551611 100644 --- a/src/screens/imager/Imager.jsx +++ b/src/screens/imager/Imager.jsx @@ -1,57 +1,68 @@ -import { StyleSheet, Text } from 'react-native' -import { SafeAreaView } from 'react-native-safe-area-context'; -import { Camera, useCameraDevice, useCameraFormat, useCodeScanner, useCameraPermission } from 'react-native-vision-camera' -import { getProduct } from '../../service/apiCall'; +import {StyleSheet, Text} from 'react-native'; +import {SafeAreaView} from 'react-native-safe-area-context'; +import { + Camera, + useCameraDevice, + useCameraFormat, + useCodeScanner, + useCameraPermission, +} from 'react-native-vision-camera'; +import {getProduct} from '../../service/apiCall'; export const Imager = () => { const {hasPermission, requestPermission} = useCameraPermission(); - + requestPermission(); const codeScanner = useCodeScanner({ codeTypes: ['qr', 'ean-13'], - onCodeScanned: (codes) => { + onCodeScanned: codes => { codes.forEach(code => { - if(code.type == 'ean-13') getProduct(code.value).then(result => console.log(`Product found : ${result.name}`)/* call another page */); + if (code.type == 'ean-13') + getProduct(code.value).then( + result => + console.log( + `Product found : ${result.name}`, + ) /* call another page */, + ); }); - } + }, }); const device = useCameraDevice('back', { physicalDevices: [ 'ultra-wide-angle-camera', 'wide-angle-camera', - 'telephoto-camera' - ] + 'telephoto-camera', + ], }); - if(!device) return - else - { + if (!device) return ; + else { const format = useCameraFormat(device, [ - { videoAspectRatio: 3/4 }, - { fps: 120 }, - { videoStabilizationMode: 'cinematic-extended'} + {videoAspectRatio: 3 / 4}, + {fps: 120}, + {videoStabilizationMode: 'cinematic-extended'}, ]); - if(hasPermission) - { + if (hasPermission) { return ( + isActive={true} + /> ); - } - else - { - return( + } else { + return ( - No permission granted for the Camera + + No permission granted for the Camera + ); } } -}; \ No newline at end of file +}; diff --git a/src/service/apiCall.ts b/src/service/apiCall.ts index 75f94dc9..8a26b92e 100644 --- a/src/service/apiCall.ts +++ b/src/service/apiCall.ts @@ -1,13 +1,11 @@ import {logger} from 'react-native-logs'; const apiUrl = 'https://world.openfoodfacts.org/api/v2/product/'; -const log = logger.createLogger(); export async function getProduct(barcode: string) { - const response = await fetch(apiUrl + barcode); const json = await response.json(); - + let data = {}; data.code = json.code; data.name = json.product.product_name; @@ -15,7 +13,7 @@ export async function getProduct(barcode: string) { data.categories = json.product.categories; data.nutriments = json.product.nutriments; data.energy = json.product.nutriments.energy; - data.energy100g = json.product.nutriments["energy-kcal_100g"]; - + data.energy100g = json.product.nutriments['energy-kcal_100g']; + return data; }