feat: Add group creation functionality and refactor trip service for improved data handling
This commit is contained in:
@@ -3,6 +3,8 @@ 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';
|
||||
import 'package:travel_mate/services/group_service.dart';
|
||||
import 'package:travel_mate/models/group.dart';
|
||||
|
||||
|
||||
class CreateTripContent extends StatefulWidget {
|
||||
@@ -44,18 +46,6 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
title: Text('Créer un voyage'),
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Colors.white,
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: _isLoading ? null : _saveTrip,
|
||||
child: Text(
|
||||
'Sauvegarder',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: EdgeInsets.all(16),
|
||||
@@ -413,6 +403,55 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
});
|
||||
}
|
||||
|
||||
Future<bool> _saveGroup() async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
final members = await _changeUserEmailById(_participants);
|
||||
|
||||
final group = Group(
|
||||
id: '',
|
||||
name: _titleController.text.trim(),
|
||||
members: members,
|
||||
);
|
||||
|
||||
final groupService = GroupService();
|
||||
bool success = await groupService.createGroup(group);
|
||||
return success;
|
||||
}
|
||||
|
||||
Future<List<String>> _changeUserEmailById(List<String> participants) async {
|
||||
final userProvider = Provider.of<UserProvider>(context, listen: false);
|
||||
List<String> ids = [];
|
||||
|
||||
for (String email in participants) {
|
||||
try {
|
||||
final id = await userProvider.getUserIdByEmail(email);
|
||||
if (id != null) {
|
||||
ids.add(id);
|
||||
} else {
|
||||
print('Utilisateur non trouvé pour l\'ID: $email');
|
||||
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 récupération de l\'utilisateur $email: $e');
|
||||
}
|
||||
}
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
Future<void> _saveTrip() async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
@@ -443,34 +482,11 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
|
||||
// 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');
|
||||
}
|
||||
}
|
||||
participantIds = await _changeUserEmailById(_participants);
|
||||
|
||||
// Créer l'objet Trip avec les IDs des participants
|
||||
final trip = Trip(
|
||||
@@ -487,14 +503,16 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
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) {
|
||||
//Créer le groupe associé au voyage
|
||||
final successGroup = await _saveGroup();
|
||||
|
||||
if (success && successGroup && mounted) {
|
||||
print('Voyage créé avec succès !');
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
@@ -525,4 +543,4 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user