import 'package:flutter/material.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import '../../constants.dart'; import '../../enumerations.dart'; import '../../services/auth/auth_service.dart'; import 'profile_category_form.dart'; class SkillsForm extends StatelessWidget { SkillsForm({super.key, required this.skillsSought}); // get instance of firestore and auth final FirebaseFirestore _firestore = FirebaseFirestore.instance; final AuthService _authService = AuthService(); final bool skillsSought; // flag to toggle offered and sought skills @override Widget build(BuildContext context) { return FutureBuilder>( future: getSkillsFromFirebase(), // Fetch skills from Firebase builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { // Show loading indicator while fetching data return const CircularProgressIndicator(); } else if (snapshot.hasError) { return Text('Error: ${snapshot.error}'); } else { List? userSkills = snapshot.data; return ProfileCategoryForm( title: 'Skills', description: skillsSought ? 'Choose up to 3 areas you are looking for in a cofounder' : 'Select up to 3 areas in which you are skilled', options: SkillOption.values.toList(), // Convert enum values to list minSelections: 1, maxSelections: 3, preSelectedOptions: userSkills ?? [], // Pass pre-selected skills to the form onSave: (selectedOptions) { // Handle saving selected options saveSkillsToFirebase(selectedOptions.cast()); // Then navigate to another screen or perform any other action??? }, ); } }, ); } Future> getSkillsFromFirebase() async { // Fetch skills from Firestore String currentUserId = _authService.getCurrentUser()!.uid; DocumentSnapshot userDoc = await _firestore .collection(Constants.dbCollectionUsers) .doc(currentUserId) .get(); if (userDoc.exists && userDoc.data() != null) { Map userData = userDoc.data()! as Map; // Explicit cast List? skills; if (skillsSought) { skills = userData[Constants.dbFieldUsersSkillsSought]; } else { skills = userData[ Constants.dbFieldUsersSkills]; //as List?; // Explicit cast } if (skills != null && skills.isNotEmpty) { // Convert skills from strings to enum values List userSkills = skills .map((skill) => SkillOption.values .firstWhere((x) => x.toString() == 'SkillOption.$skill')) .toList(); return userSkills; } } return []; } void saveSkillsToFirebase(List selectedOptions) { String currentUserId = _authService.getCurrentUser()!.uid; // Convert enum values to strings, removing leading EnumType with split List skills = selectedOptions .map((option) => option.toString().split('.').last) .toList(); // Update the corresponding 'skills' field in the user's document String keyToUpdate = skillsSought ? Constants.dbFieldUsersSkillsSought : Constants.dbFieldUsersSkills; _firestore .collection(Constants.dbCollectionUsers) .doc(currentUserId) .update({ keyToUpdate: skills, }).then((_) { print('$keyToUpdate saved to Firebase: $skills'); }).catchError((error) { print('Failed to save $keyToUpdate: $error'); }); } }