cofounderella/lib/forms/skills_form.dart

143 lines
4.8 KiB
Dart
Raw Normal View History

2024-05-15 13:35:01 +02:00
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 {
2024-05-17 23:08:26 +02:00
SkillsForm({
super.key,
required this.isRegProcess,
required this.skillsSought,
});
2024-05-15 13:35:01 +02:00
// get instance of firestore and auth
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final AuthService _authService = AuthService();
2024-05-17 23:08:26 +02:00
final bool isRegProcess;
2024-05-15 13:35:01 +02:00
final bool skillsSought; // flag to toggle offered and sought skills
@override
Widget build(BuildContext context) {
return FutureBuilder<List<SkillOption>>(
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<SkillOption>? userSkills = snapshot.data;
return ProfileCategoryForm(
title: 'Skills',
2024-05-17 23:08:26 +02:00
header:
skillsSought ? 'Skills you are looking for' : 'Your own skills',
2024-05-15 13:35:01 +02:00
description: skillsSought
2024-05-17 23:08:26 +02:00
? 'Choose up to 3 areas you are looking for in a co-founder'
2024-05-15 13:35:01 +02:00
: 'Select up to 3 areas in which you are skilled',
2024-05-17 23:08:26 +02:00
saveButtonText: isRegProcess ? 'Save and continue' : 'Save',
2024-05-15 13:35:01 +02:00
options: SkillOption.values.toList(), // Convert enum values to list
minSelections: 1,
maxSelections: 3,
preSelectedOptions:
userSkills ?? [], // Pass pre-selected skills to the form
2024-05-17 23:08:26 +02:00
onSave: (selectedOptions) async {
2024-05-15 13:35:01 +02:00
// Handle saving selected options
2024-05-17 23:08:26 +02:00
bool success = await saveSkillsToFirebase(
selectedOptions.cast<SkillOption>());
2024-05-15 13:35:01 +02:00
// Then navigate to another screen or perform any other action???
2024-05-17 23:08:26 +02:00
if (context.mounted) {
if (success) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SkillsForm(
isRegProcess: isRegProcess,
skillsSought: true,
),
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Failed to save user skills.'),
),
);
}
}
2024-05-15 13:35:01 +02:00
},
);
}
},
);
}
Future<List<SkillOption>> 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<String, dynamic> userData =
userDoc.data()! as Map<String, dynamic>; // Explicit cast
List<dynamic>? skills;
if (skillsSought) {
skills = userData[Constants.dbFieldUsersSkillsSought];
} else {
skills = userData[
Constants.dbFieldUsersSkills]; //as List<dynamic>?; // Explicit cast
}
if (skills != null && skills.isNotEmpty) {
// Convert skills from strings to enum values
List<SkillOption> userSkills = skills
.map((skill) => SkillOption.values
.firstWhere((x) => x.toString() == 'SkillOption.$skill'))
.toList();
return userSkills;
}
}
return [];
}
2024-05-17 23:08:26 +02:00
Future<bool> saveSkillsToFirebase(List<SkillOption> selectedOptions) async {
try {
String currentUserId = _authService.getCurrentUser()!.uid;
2024-05-15 13:35:01 +02:00
2024-05-17 23:08:26 +02:00
// Convert enum values to strings, removing leading EnumType with split
List<String> skills = selectedOptions
.map((option) => option.toString().split('.').last)
.toList();
2024-05-15 13:35:01 +02:00
2024-05-17 23:08:26 +02:00
// Update the corresponding 'skills' field in the user's document
String keyToUpdate = skillsSought
? Constants.dbFieldUsersSkillsSought
: Constants.dbFieldUsersSkills;
2024-05-15 13:35:01 +02:00
2024-05-17 23:08:26 +02:00
_firestore
.collection(Constants.dbCollectionUsers)
.doc(currentUserId)
.update({
keyToUpdate: skills,
}).then((_) {
print('$keyToUpdate saved to Firebase: $skills');
}).catchError((error) {
print('Failed to save $keyToUpdate: $error');
});
return true;
} catch (e) {
return false;
}
2024-05-15 13:35:01 +02:00
}
}