From f31f1451e10fb51b49222e3613105802bf0ffc6c Mon Sep 17 00:00:00 2001 From: Louison PARANT Date: Tue, 5 Dec 2023 11:45:00 +0100 Subject: [PATCH] Ingredient Management (Ingredient Selection) --- LeftOvers/screens/HomePage.tsx | 47 ++++++++++++-- LeftOvers/screens/IngredientSelection.tsx | 79 ++++++++++++++++++++--- 2 files changed, 110 insertions(+), 16 deletions(-) diff --git a/LeftOvers/screens/HomePage.tsx b/LeftOvers/screens/HomePage.tsx index 0b206b1..7e1880b 100644 --- a/LeftOvers/screens/HomePage.tsx +++ b/LeftOvers/screens/HomePage.tsx @@ -20,8 +20,10 @@ export default function HomePage({ navigation, props }) { const profilesHand = [ {name: "None", avatar: "logo.png", isActive: "none"} ] + const ingredientListHand = [{name: "None"}] const [profiles, setProfiles] = useState(profilesHand); + const [ingredientList, setIngredientList] = useState(ingredientListHand) const handleGetProfiles = async () => { try { @@ -33,6 +35,16 @@ export default function HomePage({ navigation, props }) { } } + const handleGetAvailableIngredient = async () => { + try { + const existingAvailableIngredient = await AsyncStorage.getItem('ingredient'); + return JSON.parse(existingAvailableIngredient) || []; + } catch (error) { + console.log(error); + return []; + } + } + const fetchProfiles = async () => { const existingProfiles = await handleGetProfiles(); if (existingProfiles.length != 0){ @@ -43,7 +55,17 @@ export default function HomePage({ navigation, props }) { } }; - const subscription = EventEmitter.addListener('profileAdded', async () => { + const fetchAvailableIngredient = async () => { + const existingAvailableIngredient = await handleGetAvailableIngredient(); + if (existingAvailableIngredient.length != 0){ + setIngredientList(existingAvailableIngredient); + } + else{ + setIngredientList(ingredientListHand) + } + }; + + const subscriptionAddProfile = EventEmitter.addListener('profileAdded', async () => { fetchProfiles(); }); @@ -56,15 +78,28 @@ export default function HomePage({ navigation, props }) { } }); + const subscriptionAddIngredient = EventEmitter.addListener('ingredientAdded', async () => { + fetchAvailableIngredient(); + }); + + const subscriptionDeleteIngredient = EventEmitter.addListener('ingredientDeleted', async () => { + if (ingredientList.length == 1){ + setIngredientList(ingredientListHand) + } + else{ + fetchAvailableIngredient(); + } + }); + useEffect(() => { + //AsyncStorage.clear() fetchProfiles(); if(profiles.length == 0){ setProfiles([{name: "None", avatar: "plus_small.png", isActive: "none"}]) } + fetchAvailableIngredient(); }, []); - const ingredientList = [{title: "Carrot"}, {title: "Potato"}, {title: "Peach"}] - const [cpt, setCpt] = useState(0); const decreaseCounter = () => { if (cpt > 0) { @@ -212,16 +247,16 @@ export default function HomePage({ navigation, props }) { - + - console.log('Chnge Selected Ingredient')}/> + navigation.navigate("IngredientSelection")}/> - console.log('Go and search for recipe')}/> + navigation.navigate("RecipeSuggestion")}/> diff --git a/LeftOvers/screens/IngredientSelection.tsx b/LeftOvers/screens/IngredientSelection.tsx index 9a0a1fe..9b9c24f 100644 --- a/LeftOvers/screens/IngredientSelection.tsx +++ b/LeftOvers/screens/IngredientSelection.tsx @@ -10,6 +10,8 @@ import IngredientService from '../Services/Ingredients/IngredientsServices'; import { LinearGradient } from 'expo-linear-gradient'; import ColorContext from '../theme/ColorContext'; import ValidateButton from '../components/ValidateButton'; +import AsyncStorage from '@react-native-async-storage/async-storage'; +import EventEmitter from './EventEmitter'; export default function IngredientSelection(props) { const alphabetArray: Array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]; @@ -57,6 +59,8 @@ const loadIngredients = async () => { useEffect(() => { loadIngredients(); + fetchAvailableIngredient(); + ChangeAvailableSize(true) }, []); const AvailableItem = ({ value }: { value: Ingredient }) => ( @@ -83,20 +87,70 @@ const loadIngredients = async () => { ); - const SelectIngredient = (newIngredient: Ingredient) => { - const exists = selectedIngredients.find((ingredient) => ingredient.id === newIngredient.id); - if (!exists) { - setSelectedIngredients([...selectedIngredients, newIngredient]); - ChangeAvailableSize(false) + const handleGetAvailableIngredient = async () => { + try { + const existingAvailableIngredient = await AsyncStorage.getItem('ingredient'); + return JSON.parse(existingAvailableIngredient) || []; + } catch (error) { + console.log(error); + return []; + } +} + +const fetchAvailableIngredient = async () => { + const existingAvailableIngredient = await handleGetAvailableIngredient(); + if (existingAvailableIngredient.length != 0){ + setSelectedIngredients(existingAvailableIngredient); + } + else{ + setSelectedIngredients([{value: "None"}]) + } +}; + + const SelectIngredient = async (newIngredient: Ingredient) => { + try{ + const exists = selectedIngredients.find((ingredient) => ingredient.id === newIngredient.id); + if (!exists) { + let existingAvailableIngredient = await AsyncStorage.getItem('ingredient'); + existingAvailableIngredient = existingAvailableIngredient ? JSON.parse(existingAvailableIngredient) : []; + const updatedAvailableIngredient = [...existingAvailableIngredient, newIngredient]; + await AsyncStorage.setItem('ingredient', JSON.stringify(updatedAvailableIngredient)); + EventEmitter.emit('ingredientAdded'); + console.log('Ingredient Added:', newIngredient); + ChangeAvailableSize(false) + } + } + catch(error){ + console.log("Error occured during the addition of Ingredient:", error) } }; - const RemoveIngredient = (idIngredient: number) => { - const updatedIngredients = selectedIngredients.filter((ingredient) => ingredient.id !== idIngredient); - setSelectedIngredients(updatedIngredients); - ChangeAvailableSize(true) + const RemoveIngredient = async (idIngredient: number) => { + try{ + const updatedIngredients = selectedIngredients.filter((ingredient) => ingredient.id !== idIngredient); + await AsyncStorage.setItem('ingredient', JSON.stringify(updatedIngredients)); + EventEmitter.emit('ingredientDeleted'); + fetchAvailableIngredient(); + setSelectedIngredients(updatedIngredients); + ChangeAvailableSize(true) + } + catch (error){ + console.log("Error occured during the suppression of Ingredient:", error) + } }; + const subscriptionAddIngredient = EventEmitter.addListener('ingredientAdded', async () => { + fetchAvailableIngredient(); + }); + const subscriptionDeleteIngredient = EventEmitter.addListener('ingredientDeleted', async () => { + if (selectedIngredients.length == 1){ + setSelectedIngredients([{title: "None"}]) + } + else{ + fetchAvailableIngredient(); + } + }); + const ChangeAvailableSize = (remove: boolean) => { if(remove){ if (selectedIngredients.length == 1){ @@ -120,7 +174,12 @@ const loadIngredients = async () => { setAvailableSize(90) } else if (selectedIngredients.length == 1){ - setAvailableSize(180) + if(selectedIngredients[0].value == "None"){ + setAvailableSize(90) + } + else{ + setAvailableSize(180) + } } else if (selectedIngredients.length == 2){ setAvailableSize(260)