Skip to content

Commit

Permalink
imager + service -> product scan
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruiseki committed Feb 28, 2024
1 parent 790b111 commit f98c9ed
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 46 deletions.
13 changes: 3 additions & 10 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
import React, {useEffect} from 'react';
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 {useCameraPermission} from 'react-native-vision-camera';
import { Imager } from './screens/imager/Imager';

Check failure

Code scanning / ESLint

Replace ·Imager· with Imager Error

Replace ·Imager· with Imager

const Tab = createBottomTabNavigator();

const LastScanScreen = () => null;
const PlateScreen = () => null;
const QRScanScreen = () => null;
const QRScanScreen = () => Imager();
const SearchScreen = () => null;
const AccountScreen = () => null;

const App = () => {
const {hasPermission, requestPermission} = useCameraPermission();
const renderIcon =
name =>
({color, size}) =>
<Icon name={name} color={color} size={size} />;

useEffect(() => {
if (!hasPermission) {
requestPermission().then(r => console.log('requested permission', r));
}
}, [hasPermission, requestPermission]);

return (
<NavigationContainer>
<Tab.Navigator>
Expand Down
63 changes: 55 additions & 8 deletions src/screens/imager/Imager.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,57 @@
import { useCodeScanner } from 'react-native-vision-camera'
import { StyleSheet, Text } from 'react-native'

Check failure

Code scanning / ESLint

Replace ·StyleSheet,·Text·}·from·'react-native' with StyleSheet,·Text}·from·'react-native'; Error

Replace ·StyleSheet,·Text·}·from·'react-native' with StyleSheet,·Text}·from·'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';

Check failure

Code scanning / ESLint

Replace ·SafeAreaView· with SafeAreaView Error

Replace ·SafeAreaView· with SafeAreaView
import { Camera, useCameraDevice, useCameraFormat, useCodeScanner, useCameraPermission } from 'react-native-vision-camera'

Check failure

Code scanning / ESLint

Replace ·Camera,·useCameraDevice,·useCameraFormat,·useCodeScanner,·useCameraPermission·}·from·'react-native-vision-camera' with ⏎··Camera,⏎··useCameraDevice,⏎··useCameraFormat,⏎··useCodeScanner,⏎··useCameraPermission,⏎}·from·'react-native-vision-camera'; Error

Replace ·Camera,·useCameraDevice,·useCameraFormat,·useCodeScanner,·useCameraPermission·}·from·'react-native-vision-camera' with ⏎··Camera,⏎··useCameraDevice,⏎··useCameraFormat,⏎··useCodeScanner,⏎··useCameraPermission,⏎}·from·'react-native-vision-camera';
import { getProduct } from '../../service/apiCall';

Check failure

Code scanning / ESLint

Replace ·getProduct· with getProduct Error

Replace ·getProduct· with getProduct

const codeScanner = useCodeScanner({
codeTypes: ['qr', 'ean-13'],
onCodeScanned: (codes) => {
console.log(`Scanned ${codes.length} codes!`)
}
})
export const Imager = () => {
const {hasPermission, requestPermission} = useCameraPermission();

Check failure

Code scanning / ESLint

Delete ·· Error

Delete ··
requestPermission();

const codeScanner = useCodeScanner({
codeTypes: ['qr', 'ean-13'],
onCodeScanned: (codes) => {

Check failure

Code scanning / ESLint

Replace (codes) with codes Error

Replace (codes) with codes
codes.forEach(code => {
if(code.type == 'ean-13') getProduct(code.value).then(result => console.log(`Product found : ${result.name}`)/* call another page */);

Check failure

Code scanning / ESLint

Replace (code.type·==·'ean-13')·getProduct(code.value).then(result·=>·console.log(Product·found·:·${result.name})/*·call·another·page·*/ with ·(code.type·==·'ean-13')⏎··········getProduct(code.value).then(⏎············result·=>⏎··············console.log(⏎················Product·found·:·${result.name},⏎··············)·/*·call·another·page·*/,⏎·········· Error

Replace (code.type·==·'ean-13')·getProduct(code.value).then(result·=>·console.log(Product·found·:·${result.name})/\*·call·another·page·\*/ with ·(code.type·==·'ean-13')⏎··········getProduct(code.value).then(⏎············result·=>⏎··············console.log(⏎················Product·found·:·${result.name},⏎··············)·/\*·call·another·page·\*/,⏎··········

Check warning

Code scanning / ESLint

require the use of `===` and `!==` Warning

Expected '===' and instead saw '=='.
});
}

Check failure

Code scanning / ESLint

Insert , Error

Insert ,
});

const device = useCameraDevice('back', {
physicalDevices: [
'ultra-wide-angle-camera',
'wide-angle-camera',
'telephoto-camera'

Check failure

Code scanning / ESLint

Insert , Error

Insert ,
]

Check failure

Code scanning / ESLint

Insert , Error

Insert ,
});

return <Camera {...props} codeScanner={codeScanner} />
if(!device) return <NoCameraDeviceError />

Check failure

Code scanning / ESLint

Replace (!device)·return·<NoCameraDeviceError··/> with ·(!device)·return·<NoCameraDeviceError·/>; Error

Replace (!device)·return·<NoCameraDeviceError··/> with ·(!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
{

Check failure

Code scanning / ESLint

Delete ⏎· Error

Delete ⏎·
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 },

Check failure

Code scanning / ESLint

Replace ·videoAspectRatio:·3/4· with videoAspectRatio:·3·/·4 Error

Replace ·videoAspectRatio:·3/4· with videoAspectRatio:·3·/·4
{ fps: 120 },

Check failure

Code scanning / ESLint

Replace ·fps:·120· with fps:·120 Error

Replace ·fps:·120· with fps:·120
{ videoStabilizationMode: 'cinematic-extended'}

Check failure

Code scanning / ESLint

Replace ·videoStabilizationMode:·'cinematic-extended'} with videoStabilizationMode:·'cinematic-extended'}, Error

Replace ·videoStabilizationMode:·'cinematic-extended'} with videoStabilizationMode:·'cinematic-extended'},
]);

if(hasPermission)
{

Check failure

Code scanning / ESLint

Replace (hasPermission)⏎··· with ·(hasPermission) Error

Replace (hasPermission)⏎··· with ·(hasPermission)
return (
<Camera
style={StyleSheet.absoluteFill}
device={device}
format={format}
codeScanner={codeScanner}
isActive={true} />

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

Insert ⏎······· Error

Insert ⏎·······
);
}
else
{

Check failure

Code scanning / ESLint

Replace ⏎····else⏎··· with ·else Error

Replace ⏎····else⏎··· with ·else
return(

Check failure

Code scanning / ESLint

Insert · Error

Insert ·
<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>

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

Replace No·permission·granted·for·the·Camera with ⏎············No·permission·granted·for·the·Camera⏎·········· Error

Replace No·permission·granted·for·the·Camera with ⏎············No·permission·granted·for·the·Camera⏎··········
</SafeAreaView>
);
}
}
};

Check failure

Code scanning / ESLint

Insert ⏎ Error

Insert ⏎
41 changes: 13 additions & 28 deletions src/service/apiCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,18 @@ const apiUrl = 'https://world.openfoodfacts.org/api/v2/product/';
const log = logger.createLogger();

export async function getProduct(barcode: string) {
interface ProductData {
code: string;
name: string;
nutriscore: string;
categories: string;
nutriments: object; // ou un type plus spécifique si possible
}

let data: ProductData | any = null;

try {
const response = await fetch(apiUrl + barcode);
const json = await response.json();
data.code = json.code;
data.name = json.product.generic_name_fr
? json.product.generic_name_fr
: json.product.generic_name_en
? json.product.generic_name_en
: json.product.generic_name;
data.nutriscore = json.product.nutriscore_grade;
data.categories = json.product.categories;
data.nutriments = json.product.nutriments;
} catch (error: any) {
log.error('Erreur lors de la récupération du produit', {error});
// Retourner un objet d'erreur spécifique
return {error: true, message: 'Erreur lors de la récupération du produit.'};
}


const response = await fetch(apiUrl + barcode);

Check failure

Code scanning / ESLint

Delete ⏎·· Error

Delete ⏎··
const json = await response.json();

Check failure

Code scanning / ESLint

Delete ·· Error

Delete ··
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"];

Check failure

Code scanning / ESLint

Replace "energy-kcal_100g" with 'energy-kcal_100g' Error

Replace "energy-kcal\_100g" with 'energy-kcal\_100g'

Check failure

Code scanning / ESLint

Delete ·· Error

Delete ··
return data;
}

0 comments on commit f98c9ed

Please sign in to comment.