@ -10,6 +10,8 @@ import IngredientService from '../Services/Ingredients/IngredientsServices';
import { LinearGradient } from 'expo-linear-gradient' ;
import { LinearGradient } from 'expo-linear-gradient' ;
import ColorContext from '../theme/ColorContext' ;
import ColorContext from '../theme/ColorContext' ;
import ValidateButton from '../components/ValidateButton' ;
import ValidateButton from '../components/ValidateButton' ;
import AsyncStorage from '@react-native-async-storage/async-storage' ;
import EventEmitter from './EventEmitter' ;
export default function IngredientSelection ( props ) {
export default function IngredientSelection ( props ) {
const alphabetArray : Array < string > = [ "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" ] ;
const alphabetArray : Array < string > = [ "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 ( ( ) = > {
useEffect ( ( ) = > {
loadIngredients ( ) ;
loadIngredients ( ) ;
fetchAvailableIngredient ( ) ;
ChangeAvailableSize ( true )
} , [ ] ) ;
} , [ ] ) ;
const AvailableItem = ( { value } : { value : Ingredient } ) = > (
const AvailableItem = ( { value } : { value : Ingredient } ) = > (
@ -83,20 +87,70 @@ const loadIngredients = async () => {
< / >
< / >
) ;
) ;
const SelectIngredient = ( newIngredient : Ingredient ) = > {
const handleGetAvailableIngredient = async ( ) = > {
const exists = selectedIngredients . find ( ( ingredient ) = > ingredient . id === newIngredient . id ) ;
try {
if ( ! exists ) {
const existingAvailableIngredient = await AsyncStorage . getItem ( 'ingredient' ) ;
setSelectedIngredients ( [ . . . selectedIngredients , newIngredient ] ) ;
return JSON . parse ( existingAvailableIngredient ) || [ ] ;
ChangeAvailableSize ( false )
} 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 RemoveIngredient = async ( idIngredient : number ) = > {
const updatedIngredients = selectedIngredients . filter ( ( ingredient ) = > ingredient . id !== idIngredient ) ;
try {
setSelectedIngredients ( updatedIngredients ) ;
const updatedIngredients = selectedIngredients . filter ( ( ingredient ) = > ingredient . id !== idIngredient ) ;
ChangeAvailableSize ( true )
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 ) = > {
const ChangeAvailableSize = ( remove : boolean ) = > {
if ( remove ) {
if ( remove ) {
if ( selectedIngredients . length == 1 ) {
if ( selectedIngredients . length == 1 ) {
@ -120,7 +174,12 @@ const loadIngredients = async () => {
setAvailableSize ( 90 )
setAvailableSize ( 90 )
}
}
else if ( selectedIngredients . length == 1 ) {
else if ( selectedIngredients . length == 1 ) {
setAvailableSize ( 180 )
if ( selectedIngredients [ 0 ] . value == "None" ) {
setAvailableSize ( 90 )
}
else {
setAvailableSize ( 180 )
}
}
}
else if ( selectedIngredients . length == 2 ) {
else if ( selectedIngredients . length == 2 ) {
setAvailableSize ( 260 )
setAvailableSize ( 260 )