Skip to content

Commit c13efa7

Browse files
committed
Resolve Scan
1 parent 431ca47 commit c13efa7

File tree

6 files changed

+48
-45
lines changed

6 files changed

+48
-45
lines changed

ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ SPEC CHECKSUMS:
14241424
RNVectorIcons: 73ab573085f65a572d3b6233e68996d4707fd505
14251425
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
14261426
VisionCamera: 7a5c87805b13adaaa557132b44879ed1e1e39786
1427-
Yoga: a716eea57d0d3430219c0a5a233e1e93ee931eb7
1427+
Yoga: 9e6a04eacbd94f97d94577017e9f23b3ab41cf6c
14281428

14291429
PODFILE CHECKSUM: 0324d2f1bead3c67e34ad5270355479295c48a2e
14301430

src/screens/carrot/CarrotScreen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ const CarrotScreen = () => {
3434
<TouchableOpacity onPress={() => handlePress(product)}>
3535
<ProductItem
3636
title={product.item.name ?? product.name}
37-
subtitle="Blablatest"
38-
badgeText="4/100 Mauvais"
37+
subtitle={product.categories}
38+
badgeText={product.nutriscore_score}
3939
defects={
4040
product.item.negative_nutrients ?? product.negative_nutrients
4141
}

src/screens/carrot/components/NutritionInfo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const NutritionInfo = ({data}) => {
1818
{item ? (
1919
<View style={styles.container}>
2020
<View style={styles.upperPartContainer}>
21-
<Image style={styles.image} source={{uri: image}} />
21+
<Image style={styles.image} source={{uri: item.image}} />
2222
<Text style={styles.title}>{item.name}</Text>
2323
</View>
2424
<View style={styles.subtitle}>

src/screens/imager/Imager.tsx

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import {useNavigation} from '@react-navigation/native';
1111
import {useDispatch, useSelector} from 'react-redux';
1212
import {
1313
addProductToList,
14+
removeProductFromList,
1415
setProduct,
1516
} from '../../service/redux/slices/productSlice';
1617
import {TAB_BAR_NAVIGATOR_ROUTES} from '../../components/navigators/TabBarNavigation/TabNavigator.interfaces.ts';
1718
import AsyncStorage from '@react-native-async-storage/async-storage';
1819
import {getProductByBarcode} from '../../service/apiCall';
19-
import { fakeBaseQuery } from '@reduxjs/toolkit/query';
2020

2121
export const Imager = () => {
2222
var busy = false;
@@ -29,31 +29,34 @@ export const Imager = () => {
2929
const navigation = useNavigation();
3030

3131
const {productList} = useSelector(state => state.product);
32-
let productListToSave = productList;
3332

3433
const codeScanner = useCodeScanner({
3534
codeTypes: ['qr', 'ean-13'],
3635
onCodeScanned: codes => {
3736
if (codes[0].type === 'ean-13') {
38-
// setIsCameraActive(false);
39-
if (!busy)
40-
{
37+
if (!busy) {
4138
busy = true;
4239
console.log(`Searching barcode ${codes[0].value}...`);
43-
44-
getProductByBarcode(codes[0].value).then(async result => {
45-
if(!result) return;
40+
41+
getProductByBarcode(codes[0].value).then(result => {
42+
if (!result) return;
4643
console.log(`Product found : ${result.name}`);
4744
dispatch(setProduct(result));
48-
// productListToSave.push(result);
45+
if (productList.find(product => product.name === result.name)) {
46+
console.log('Product already in list');
47+
48+
// Remove product from list if already in list and add it again on top
49+
dispatch(removeProductFromList(result));
50+
} else {
51+
AsyncStorage.setItem('productList', JSON.stringify(productList));
52+
}
53+
54+
// Add product to list if not already in list
4955
dispatch(addProductToList(result));
50-
await AsyncStorage.setItem(
51-
'productList',
52-
JSON.stringify(productList),
53-
);
56+
5457
// @ts-ignore
55-
busy = false;
5658
navigation.navigate(TAB_BAR_NAVIGATOR_ROUTES.CARROT);
59+
busy = false;
5760
});
5861
}
5962
}

src/screens/register/RegisterScreen.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ import {useNavigation} from '@react-navigation/native';
1212
import {ACCOUNT_NAVIGATOR_ROUTES} from '../../components/navigators/AccountNavigator/AccountNavigator.interfaces.ts';
1313
import {logger} from 'react-native-logs';
1414
import AsyncStorage from '@react-native-async-storage/async-storage';
15-
import {TAB_BAR_NAVIGATOR_ROUTES} from '../../components/navigators/TabBarNavigation/TabNavigator.interfaces.ts';
1615
import {
1716
validateEmail,
1817
validateName,
1918
validatePassword,
2019
validateSurname,
2120
} from '../../utils/validationUtils.ts';
21+
import {login} from '../../service/redux/slices/userSlice.ts';
22+
import {useDispatch} from 'react-redux';
2223

2324
const RegisterScreen = () => {
2425
const log = logger.createLogger();
2526
const navigation = useNavigation();
27+
const dispatch = useDispatch();
2628
const [name, setName] = useState('');
2729
const [surname, setSurname] = useState('');
2830
const [email, setEmail] = useState('');
@@ -60,9 +62,14 @@ const RegisterScreen = () => {
6062
return;
6163
}
6264

63-
AsyncStorage.setItem('user', email).then(() => {
64-
navigation.navigate(TAB_BAR_NAVIGATOR_ROUTES.CARROT);
65-
});
65+
AsyncStorage.setItem('user', email)
66+
.then(() => {
67+
log.info('User set to async storage');
68+
dispatch(login({email, password}));
69+
})
70+
.catch(error => {
71+
log.error('Error while setting user to async storage', error);
72+
});
6673
}
6774

6875
return (

src/service/redux/slices/productSlice.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,15 @@ import {createSlice} from '@reduxjs/toolkit';
33
export const productSlice = createSlice({
44
name: 'product',
55
initialState: {
6-
productList: [
7-
{
8-
name: 'Carotte',
9-
categories: ['Légume', 'Bio'],
10-
ingredients_text: 'Carotte, eau',
11-
nutriments: {
12-
carbohydrates: 54,
13-
proteins: 7,
14-
fat: 30,
15-
sugars: 100,
16-
salt: 0.2,
17-
},
18-
image_url:
19-
'https://images.openfoodfacts.net/images/products/301/762/042/2003/front_en.610.100.jpg',
20-
nutriscore_score: 3,
21-
nutriscore_grade: 'c',
22-
positive_nutrients: ['vitamin C', 'fiber'],
23-
negative_nutrients: ['sugar'],
24-
},
25-
],
6+
productList: [],
267
product: {},
278
productSearch: {},
289
},
2910
reducers: {
3011
addProductToList: (state, action) => {
31-
state.productList.push(action.payload);
12+
// state.productList.push(action.payload);
13+
// Push on the top of the list
14+
state.productList = [action.payload, ...state.productList];
3215
},
3316
setProductList: (state, action) => {
3417
state.productList = action.payload;
@@ -39,10 +22,20 @@ export const productSlice = createSlice({
3922
setProductSearch: (state, action) => {
4023
state.productSearch = action.payload;
4124
},
25+
removeProductFromList: (state, action) => {
26+
state.productList = state.productList.filter(
27+
product => product.name !== action.payload.name,
28+
);
29+
},
4230
},
4331
});
4432

45-
export const {addProductToList, setProduct, setProductList, setProductSearch} =
46-
productSlice.actions;
33+
export const {
34+
removeProductFromList,
35+
addProductToList,
36+
setProduct,
37+
setProductList,
38+
setProductSearch,
39+
} = productSlice.actions;
4740

4841
export default productSlice.reducer;

0 commit comments

Comments
 (0)