fix: profile creation

pull/23/head
Rémi REGNAULT 1 year ago
parent 24537f1b85
commit 996d656d18

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

@ -4,15 +4,25 @@ import AsyncStorage from "@react-native-async-storage/async-storage";
export default class ProfileService implements IProfileService { export default class ProfileService implements IProfileService {
async getProfiles(): Promise<Profil[]> { async getProfiles(): Promise<Profil[]> {
const existingProfiles = await AsyncStorage.getItem('profiles'); const results = await AsyncStorage.getItem('profiles');
return JSON.parse(existingProfiles) || []; const tmp = JSON.parse(results)
let existingProfiles: Profil[] = []
for (let item of tmp) {
existingProfiles.push(new Profil(item._name, item._avatar, item._allergy, item._diets))
}
return existingProfiles;
} }
async addProfile(new_profile : Profil): Promise<void> { async addProfile(new_profile : Profil): Promise<boolean> {
const list = [new_profile] const existingProfiles = await this.getProfiles()
const key_exist = ((await AsyncStorage.getAllKeys()).includes('profiles')) for (let current_profile of existingProfiles) {
if (!key_exist) await AsyncStorage.setItem('profiles', JSON.stringify(list)) if (current_profile.name == new_profile.name) {
else await AsyncStorage.mergeItem('profiles', JSON.stringify(list)) console.log("Tried to create a profil already existing !")
return false
}
}
await AsyncStorage.setItem('profiles', JSON.stringify([...existingProfiles, new_profile]))
return true
} }
} }

@ -29,16 +29,16 @@ export default function CreateProfile(props) {
const pickImage = async () => { const pickImage = async () => {
// No permissions request is necessary for launching the image library // No permissions request is necessary for launching the image library
let result = await ImagePicker.launchImageLibraryAsync({ let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All, mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true, allowsEditing: true,
aspect: [4, 3], aspect: [4, 3],
quality: 1, quality: 1,
}); });
console.log(result); console.log(result);
if (!result.canceled) { if (!result.canceled) {
setAvatar(result.assets[0].uri); setAvatar(result.assets[0].uri);
} }
}; };
@ -56,18 +56,22 @@ export default function CreateProfile(props) {
const handleCreateProfile = async () => { const handleCreateProfile = async () => {
try { try {
// Ton code pour récupérer les profils existants et ajouter un nouveau profil // Ton code pour récupérer les profils existants et ajouter un nouveau profil
const new_profile = new Profil(name, avatar, selectedAllergies, selectedDiets) const new_profile = new Profil(name, avatar, selectedAllergies, selectedDiets)
// Mettre à jour AsyncStorage avec le nouveau profil // Mettre à jour AsyncStorage avec le nouveau profil
await profile_service.addProfile(new_profile) if (await profile_service.addProfile(new_profile)) {
EventEmitter.emit('profileAdded'); EventEmitter.emit('profileAdded');
props.navigation.goBack();
props.navigation.goBack(); alert('Profile Created !');
}
alert('Profil créé !'); else {
props.navigation.goBack()
alert('Profile already exists !')
}
} catch (error) { } catch (error) {
console.error('Erreur lors de la création du profil :', error); console.error('Erreur lors de la création du profil :', error);
} }
}; };

Loading…
Cancel
Save