2024-06-20 22:47:54 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../enumerations.dart';
|
|
|
|
import '../../services/auth/auth_service.dart';
|
|
|
|
import '../../services/user_service.dart';
|
|
|
|
import '../constants.dart';
|
|
|
|
import '../pages/user_vision_page.dart';
|
|
|
|
import 'profile_category_form.dart';
|
|
|
|
|
|
|
|
class SectorsForm extends StatelessWidget {
|
|
|
|
SectorsForm({
|
|
|
|
super.key,
|
|
|
|
required this.isRegProcess,
|
|
|
|
required this.isEditMode,
|
|
|
|
});
|
|
|
|
|
|
|
|
final AuthService _authService = AuthService();
|
|
|
|
|
|
|
|
final bool isRegProcess;
|
|
|
|
final bool isEditMode;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return FutureBuilder<List<SectorOption>>(
|
|
|
|
future: UserService.getSectorsFromFirebase(
|
|
|
|
_authService.getCurrentUser()!.uid,
|
|
|
|
),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
|
|
return const CircularProgressIndicator();
|
|
|
|
} else if (snapshot.hasError) {
|
|
|
|
return Text('Error: ${snapshot.error}');
|
|
|
|
} else {
|
|
|
|
List<SectorOption>? userSectors = snapshot.data;
|
|
|
|
return ProfileCategoryForm(
|
|
|
|
title: 'Sectors of interest',
|
|
|
|
header: 'Sectors of interest',
|
2024-06-22 21:56:45 +02:00
|
|
|
description: 'Select up to ${Constants.maxSectors} sectors that match your interests.',
|
2024-06-20 22:47:54 +02:00
|
|
|
saveButtonText: isRegProcess ? 'Save and continue' : 'Save',
|
|
|
|
options: SectorOption.values.toList(), // Convert enum to list
|
|
|
|
minSelections: 1,
|
2024-06-22 21:56:45 +02:00
|
|
|
maxSelections: Constants.maxSectors,
|
2024-06-20 22:47:54 +02:00
|
|
|
preSelectedOptions: userSectors ?? [],
|
|
|
|
onSave: (selectedOptions) async {
|
|
|
|
// Handle saving selected options
|
|
|
|
bool success = await UserService.saveSectorsToFirebase(
|
|
|
|
selectedOptions.cast<SectorOption>(),
|
|
|
|
_authService.getCurrentUser()!.uid,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (context.mounted) {
|
|
|
|
if (success) {
|
|
|
|
if (isRegProcess) {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
// set following registration page HERE
|
|
|
|
builder: (context) => UserVisionPage(
|
|
|
|
isRegProcess: isRegProcess,
|
|
|
|
isEditMode: false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if (isEditMode == true) {
|
|
|
|
// pass selectedOptions data back to caller
|
|
|
|
Navigator.pop(context, {
|
|
|
|
Constants.dbFieldUsersSectors: selectedOptions,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Navigate back after saving
|
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(
|
|
|
|
content: Text('Failed to save sectors of interest.'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|