feat: CreateProfile use profile and not struct specified in CreateProfile

pull/23/head
Rémi REGNAULT 1 year ago
parent 05ffc72303
commit 24537f1b85

@ -2,5 +2,5 @@ import Profil from "../../Models/Profil";
export default interface IProfileService {
getProfiles(): Promise<Profil[]>,
addProfile(new_profile: Profil): void
addProfile(new_profile: Profil): Promise<void>
}

@ -7,8 +7,12 @@ export default class ProfileService implements IProfileService {
const existingProfiles = await AsyncStorage.getItem('profiles');
return JSON.parse(existingProfiles) || [];
}
addProfile(new_profile : Profil): void {
throw new Error("Method not implemented.");
async addProfile(new_profile : Profil): Promise<void> {
const list = [new_profile]
const key_exist = ((await AsyncStorage.getAllKeys()).includes('profiles'))
if (!key_exist) await AsyncStorage.setItem('profiles', JSON.stringify(list))
else await AsyncStorage.mergeItem('profiles', JSON.stringify(list))
}
}

@ -9,15 +9,18 @@ import ListSelect from '../components/ListSelect';
import AsyncStorage from '@react-native-async-storage/async-storage';
import EventEmitter from './EventEmitter';
import * as ImagePicker from 'expo-image-picker';
import ProfileService from '../Services/Profiles/ProfileService';
import Profil from '../Models/Profil';
export default function CreateProfile(props) {
const colors = useContext(ColorContext).colors
const all = []
const profile_service = new ProfileService()
const die = [{value: "Dairy free"}, {value: "Gluten free"}, {value: "Porkless"}, {value: "Vegan"}, {value: "Vegetarian"}, {value: "Pescatarian"}]
const [name, onChangeName] = useState();
const [avatar, setAvatar] = useState<string>('');
const [selectedDiets, setSelectedDiets] = useState([]);
const [selectedAllergies, setSelectedAllergies] = useState([])
const handleSelectedDiets = (selectedValues) => {
setSelectedDiets(selectedValues);
@ -54,22 +57,12 @@ export default function CreateProfile(props) {
const handleCreateProfile = async () => {
try {
// Ton code pour récupérer les profils existants et ajouter un nouveau profil
const newProfile = {
name: name,
avatar: avatar,
diets: selectedDiets,
allergies: all,
};
const new_profile = new Profil(name, avatar, selectedAllergies, selectedDiets)
// Mettre à jour AsyncStorage avec le nouveau profil
let existingProfiles = await AsyncStorage.getItem('profiles');
existingProfiles = existingProfiles ? JSON.parse(existingProfiles) : [];
const updatedProfiles = [...existingProfiles, newProfile];
await AsyncStorage.setItem('profiles', JSON.stringify(updatedProfiles));
await profile_service.addProfile(new_profile)
EventEmitter.emit('profileAdded');
console.log('Profil créé :', newProfile);
props.navigation.goBack();
alert('Profil créé !');
@ -173,7 +166,7 @@ export default function CreateProfile(props) {
</View>
<ListSelect title="Diets" content={die} setSelected={handleSelectedDiets}></ListSelect>
<View style={{marginTop: "6%"}}/>
<ListWithoutSelect title="Allergies" content={all}></ListWithoutSelect>
<ListWithoutSelect title="Allergies" content={selectedAllergies}></ListWithoutSelect>
<View style={{marginTop: "3%"}}/>
</View>
<View style={{marginTop: "3%"}}/>

@ -60,7 +60,8 @@ export default function Profiles({navigation, props}) {
const handleGetProfiles = async () => {
try {
return await profile_service.getProfiles()
const results = await profile_service.getProfiles()
return results
} catch (error) {
console.log("ça maaaaaaaaarche poaaaaaaaaaaaas");
return [];
@ -78,15 +79,12 @@ export default function Profiles({navigation, props}) {
useEffect(() => {
fetchProfiles();
console.log(profiles)
}, []);
const containerStyle = {
height: "75%",
width: "100%",
};
};
const styles = StyleSheet.create({
container: {
height: "100%",
@ -201,10 +199,10 @@ export default function Profiles({navigation, props}) {
const profileComponents = profiles.map((profile, index) => (
<View key={index}>
<ProfileDetails
name={profile.name}
avatar={profile.avatar}
diets={profile.diets}
allergies={profile.allergies}
name={profile._name}
avatar={profile._avatar}
diets={profile._diets}
allergies={profile._allergy}
onDeleteProfile={() => raisePopUp(index)}
/>
<Portal>

Loading…
Cancel
Save