From 8e14883221a2cc4de64f71f67f13694b49a44d25 Mon Sep 17 00:00:00 2001 From: Rayhan Hassou Date: Tue, 5 Dec 2023 08:06:33 +0100 Subject: [PATCH] start to bind modify profile --- LeftOvers/navigation/ProfileStackScreen.tsx | 2 +- LeftOvers/screens/ModifyProfile.tsx | 35 +++++++++++++++++++-- LeftOvers/screens/Profiles.tsx | 6 ++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/LeftOvers/navigation/ProfileStackScreen.tsx b/LeftOvers/navigation/ProfileStackScreen.tsx index b1c1e4e..8a725c5 100644 --- a/LeftOvers/navigation/ProfileStackScreen.tsx +++ b/LeftOvers/navigation/ProfileStackScreen.tsx @@ -37,7 +37,7 @@ export default function ProfilesStackScreen({ navigation }) { const _handleSearch = () => { console.log('Searching'); } - const _handleHeaderAdd = () => navigation.navigate('ProfileCreation'); + const _handleHeaderAdd = () => navigation.navigate('ProfileCreation', { name: String }); return ( diff --git a/LeftOvers/screens/ModifyProfile.tsx b/LeftOvers/screens/ModifyProfile.tsx index 49043ff..de25e37 100644 --- a/LeftOvers/screens/ModifyProfile.tsx +++ b/LeftOvers/screens/ModifyProfile.tsx @@ -1,4 +1,4 @@ -import React, {useContext} from 'react'; +import React, {useContext, useEffect, useState} from 'react'; import {StyleSheet, View, ScrollView, useWindowDimensions} from 'react-native'; import { LinearGradient } from 'expo-linear-gradient'; import { SafeAreaProvider } from 'react-native-safe-area-context'; @@ -7,17 +7,48 @@ import ProfileModification from '../components/ProfileModification'; import ValidateButton from '../components/ValidateButton'; import ColorContext from '../theme/ColorContext' +import { useRoute } from '@react-navigation/native'; +import AsyncStorage from '@react-native-async-storage/async-storage'; export default function ModifyProfile(props) { const {colors} = useContext(ColorContext); const all = [{value: "Mussels"}, {value: "Skimmed Milk"}, {value: "Nuts"}] const die = [{value: "Dairy free"}, {value: "Gluten free"}, {value: "Porkless"}, {value: "Vegan"}, {value: "Vegetarian"}, {value: "Pescatarian"}] + const [profile, setProfile] = useState(null); + const route = useRoute(); + + + + + const handleGetProfileByName = async (profileName) => { + try { + const existingProfiles = await AsyncStorage.getItem('profiles'); + const profiles = JSON.parse(existingProfiles) || []; + + const matchedProfile = profiles.find(profile => profile.name === profileName); + console.log("Le profil choisit : " + matchedProfile); + return matchedProfile || null; + } catch (error) { + console.log("Erreur lors de la récupération du profil :", error); + return null; + } + }; + + const fetchProfiles = async () => { + const selectedProfil = await handleGetProfileByName(route.params); + setProfile(selectedProfil); +}; + +useEffect(() => { + fetchProfiles(); +}, []); + return ( - + (console.log("Profile Modified"))}> diff --git a/LeftOvers/screens/Profiles.tsx b/LeftOvers/screens/Profiles.tsx index e4580f7..e7e0e3c 100644 --- a/LeftOvers/screens/Profiles.tsx +++ b/LeftOvers/screens/Profiles.tsx @@ -23,6 +23,9 @@ export default function Profiles({navigation, props}) { const [profiles, setProfiles] = useState([]); const [selectedProfileIndex, setSelectedProfileIndex] = useState(null); + const goDetails = (name: string) => navigation.navigate('ProfileCreation', name); + + const raisePopUp = (index) => { setSelectedProfileIndex(index) setVisible(true) @@ -122,7 +125,6 @@ export default function Profiles({navigation, props}) { width: "100%", flex: 1, }, - profileValidation: { width: "100%", alignItems: "center", @@ -205,6 +207,7 @@ export default function Profiles({navigation, props}) { diets={profile.diets} allergies={profile.allergies} onDeleteProfile={() => raisePopUp(index)} + onModification={() => goDetails(profile.name)} /> @@ -255,7 +258,6 @@ export default function Profiles({navigation, props}) { - );