-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
Check warning Code scanning / ESLint require the use of `===` and `!==` Warning
Expected '===' and instead saw '=='.
|
||
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 <NoCameraDeviceError /> | ||
else | ||
{ | ||
if (!device) return <NoCameraDeviceError />; | ||
Check warning Code scanning / ESLint Disallow missing React when using JSX Warning
'React' must be in scope when using JSX
Check failure Code scanning / ESLint Disallow undeclared variables in JSX Error
'NoCameraDeviceError' is not defined.
|
||
else { | ||
const format = useCameraFormat(device, [ | ||
Check failure Code scanning / ESLint enforces the Rules of Hooks Error
React Hook "useCameraFormat" is called conditionally. React Hooks must be called in the exact same order in every component render.
|
||
{ videoAspectRatio: 3/4 }, | ||
{ fps: 120 }, | ||
{ videoStabilizationMode: 'cinematic-extended'} | ||
{videoAspectRatio: 3 / 4}, | ||
{fps: 120}, | ||
{videoStabilizationMode: 'cinematic-extended'}, | ||
]); | ||
|
||
if(hasPermission) | ||
{ | ||
if (hasPermission) { | ||
return ( | ||
<Camera | ||
style={StyleSheet.absoluteFill} | ||
device={device} | ||
format={format} | ||
codeScanner={codeScanner} | ||
isActive={true} /> | ||
isActive={true} | ||
/> | ||
Check warning Code scanning / ESLint Disallow missing React when using JSX Warning
'React' must be in scope when using JSX
|
||
); | ||
} | ||
else | ||
{ | ||
return( | ||
} else { | ||
return ( | ||
<SafeAreaView> | ||
Check warning Code scanning / ESLint Disallow missing React when using JSX Warning
'React' must be in scope when using JSX
|
||
<Text style={{color: 'black'}}>No permission granted for the Camera</Text> | ||
<Text style={{color: 'black'}}> | ||
Check warning Code scanning / ESLint Disallow missing React when using JSX Warning
'React' must be in scope when using JSX
|
||
No permission granted for the Camera | ||
</Text> | ||
</SafeAreaView> | ||
); | ||
} | ||
} | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,19 @@ | ||
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; | ||
data.nutriscore = json.product.nutriscore_grade; | ||
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; | ||
} |