feat: Enhance trip creation and management features with user validation, improved error handling, and Firestore integration
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:travel_mate/models/trip.dart';
|
||||
import 'package:travel_mate/providers/user_provider.dart';
|
||||
import 'package:travel_mate/services/trip_service.dart';
|
||||
|
||||
|
||||
class CreateTripContent extends StatefulWidget {
|
||||
@@ -428,10 +432,70 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
});
|
||||
|
||||
try {
|
||||
// TODO: Implémenter la sauvegarde du voyage
|
||||
await Future.delayed(Duration(seconds: 2)); // Simulation
|
||||
final userProvider = Provider.of<UserProvider>(context, listen: false);
|
||||
final currentUser = userProvider.currentUser;
|
||||
|
||||
if (mounted) {
|
||||
if (currentUser == null) {
|
||||
throw Exception('Utilisateur non connecté');
|
||||
}
|
||||
|
||||
print('Création du voyage par: ${currentUser.id} (${currentUser.email})');
|
||||
|
||||
// Convertir les emails en IDs utilisateur
|
||||
List<String> participantIds = [];
|
||||
|
||||
// Ajouter automatiquement le créateur
|
||||
if (currentUser.id != null) {
|
||||
participantIds.add(currentUser.id!);
|
||||
}
|
||||
|
||||
// Convertir chaque email en ID utilisateur
|
||||
for (String email in _participants) {
|
||||
try {
|
||||
final userId = await userProvider.getUserIdByEmail(email);
|
||||
if (userId != null && !participantIds.contains(userId)) {
|
||||
participantIds.add(userId);
|
||||
print('Email $email converti en ID: $userId');
|
||||
} else if (userId == null) {
|
||||
print('Utilisateur non trouvé pour l\'email: $email');
|
||||
// Optionnel: afficher un warning à l'utilisateur
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Utilisateur non trouvé pour l\'email: $email'),
|
||||
backgroundColor: Colors.orange,
|
||||
duration: Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
print('Erreur lors de la recherche de l\'utilisateur $email: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Créer l'objet Trip avec les IDs des participants
|
||||
final trip = Trip(
|
||||
id: '', // Sera généré par Firebase
|
||||
title: _titleController.text.trim(),
|
||||
description: _descriptionController.text.trim(),
|
||||
location: _locationController.text.trim(),
|
||||
startDate: _startDate!,
|
||||
endDate: _endDate!,
|
||||
budget: double.tryParse(_budgetController.text) ?? 0.0,
|
||||
createdBy: currentUser.id ?? '',
|
||||
participants: participantIds, // Contient uniquement les IDs
|
||||
createdAt: DateTime.now(),
|
||||
updatedAt: DateTime.now(),
|
||||
);
|
||||
|
||||
print('Participants IDs: $participantIds');
|
||||
print('Données du voyage: ${trip.toMap()}');
|
||||
|
||||
// Sauvegarder le voyage
|
||||
final tripService = TripService();
|
||||
final success = await tripService.addTrip(trip);
|
||||
|
||||
if (success && mounted) {
|
||||
print('Voyage créé avec succès !');
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Voyage créé avec succès !'),
|
||||
@@ -439,9 +503,12 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
),
|
||||
);
|
||||
|
||||
Navigator.pop(context);
|
||||
Navigator.pop(context, true); // Retourner true pour indiquer le succès
|
||||
} else {
|
||||
throw Exception('Erreur lors de la sauvegarde');
|
||||
}
|
||||
} catch (e) {
|
||||
print('Erreur lors de la création: $e');
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
|
||||
Reference in New Issue
Block a user