cofounderella/lib/pages/user_vision_page.dart

218 lines
6.6 KiB
Dart
Raw Normal View History

2024-05-22 02:46:46 +02:00
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import '../components/my_elevated_button.dart';
2024-06-17 17:57:32 +02:00
import '../components/text_bold.dart';
2024-05-22 02:46:46 +02:00
import '../constants.dart';
import '../enumerations.dart';
import '../forms/risks_form.dart';
2024-05-22 02:46:46 +02:00
import '../services/auth/auth_service.dart';
2024-06-14 14:28:59 +02:00
import '../utils/helper_dialogs.dart';
2024-05-21 14:01:44 +02:00
class UserVisionPage extends StatefulWidget {
2024-05-31 23:16:15 +02:00
const UserVisionPage(
{super.key, required this.isRegProcess, required this.isEditMode});
2024-05-21 14:01:44 +02:00
final bool isRegProcess;
2024-05-31 23:16:15 +02:00
final bool isEditMode;
2024-05-21 14:01:44 +02:00
@override
UserVisionPageState createState() => UserVisionPageState();
2024-05-21 14:01:44 +02:00
}
class UserVisionPageState extends State<UserVisionPage> {
2024-05-21 14:01:44 +02:00
Map<VisionOption, bool> selectedVisionOptions = {
VisionOption.marketLeader: false,
VisionOption.sustainableBusiness: false,
VisionOption.innovativeProduct: false,
VisionOption.exitStrategy: false,
};
CommunicationPreference? communicationPreference;
2024-05-21 14:01:44 +02:00
final AuthService _authService = AuthService();
@override
void initState() {
super.initState();
_loadDataFromFirebase();
}
Future<void> _loadDataFromFirebase() async {
final userDoc = FirebaseFirestore.instance
.collection(Constants.dbCollectionUsers)
.doc(_authService.getCurrentUser()!.uid);
final snapshot = await userDoc.get();
if (snapshot.exists) {
final data = snapshot.data();
setState(() {
if (data != null) {
// Load Communication Preference
if (data[Constants.dbFieldUsersCommunication] != null) {
communicationPreference = CommunicationPreference.values
.firstWhereOrNull((x) =>
x.toString() == data[Constants.dbFieldUsersCommunication]);
}
2024-06-07 02:18:20 +02:00
// Load Vision options
if (data[Constants.dbFieldUsersVisions] != null) {
2024-06-07 02:18:20 +02:00
for (var option in VisionOption.values) {
selectedVisionOptions[option] =
data[Constants.dbFieldUsersVisions]
2024-06-07 02:18:20 +02:00
.contains(option.toString());
}
2024-05-21 14:01:44 +02:00
}
}
});
2024-05-21 14:01:44 +02:00
}
}
2024-05-31 23:16:15 +02:00
Future<bool> _saveDataToFirebase() async {
try {
final userDoc = FirebaseFirestore.instance
.collection(Constants.dbCollectionUsers)
.doc(_authService.getCurrentUser()!.uid);
2024-05-21 14:01:44 +02:00
2024-05-31 23:16:15 +02:00
await userDoc.set(
{
Constants.dbFieldUsersCommunication:
communicationPreference?.toString(),
2024-05-31 23:16:15 +02:00
Constants.dbFieldUsersVisions: selectedVisionOptions.entries
.where((entry) => entry.value)
.map((entry) => entry.key.toString())
.toList(),
},
SetOptions(merge: true), // avoid overwriting existing data
);
return true;
} catch (e) {
_showSnackBar(e.toString());
return false;
}
}
void _showSnackBar(String message) {
2024-06-10 17:38:47 +02:00
showErrorSnackBar(context, message);
2024-05-21 14:01:44 +02:00
}
bool isVisionSelected() {
return selectedVisionOptions.containsValue(true);
}
2024-05-31 23:16:15 +02:00
Future<void> handleSubmit() async {
if (communicationPreference == null) {
showErrorSnackBar(context, 'Please select a communication preference.');
2024-05-21 14:01:44 +02:00
return;
}
if (!isVisionSelected()) {
_showSnackBar('Please select at least one long-term vision.');
2024-05-21 14:01:44 +02:00
return;
}
2024-05-21 14:01:44 +02:00
// Handle the form submission logic here
2024-05-31 23:16:15 +02:00
bool success = await _saveDataToFirebase();
if (success) {
2024-06-07 02:18:20 +02:00
_navigate();
2024-05-31 23:16:15 +02:00
} else {
2024-06-07 02:18:20 +02:00
_showSnackBar('Failed to save data.');
}
}
void _navigate() {
if (widget.isRegProcess) {
Navigator.push(
context,
MaterialPageRoute(
//
// set following registration page HERE
// this Page (UserVisionPage) is optional as of 22.06.24
// and therefore not called anymore during RegProcess
//
2024-06-07 02:18:20 +02:00
//
builder: (context) => RisksFormPage(
2024-06-07 02:18:20 +02:00
isRegProcess: widget.isRegProcess,
isEditMode: false,
),
2024-05-21 19:37:00 +02:00
),
2024-05-31 23:16:15 +02:00
);
2024-06-07 02:18:20 +02:00
} else {
if (widget.isEditMode == true) {
// pass selectedOptions data back to caller
Navigator.pop(context, {
Constants.dbFieldUsersCommunication: communicationPreference,
2024-06-07 02:18:20 +02:00
Constants.dbFieldUsersVisions: selectedVisionOptions.entries
.where((entry) => entry.value)
.map((entry) => entry.key)
.toList(),
});
} else {
Navigator.pop(context);
}
2024-05-31 23:16:15 +02:00
}
2024-05-21 14:01:44 +02:00
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Personal preferences'),
actions: [
if (widget.isEditMode && !widget.isRegProcess)
IconButton(
onPressed: handleSubmit,
icon: const Icon(Icons.save),
)
],
2024-05-21 14:01:44 +02:00
),
body: Padding(
padding: const EdgeInsets.all(16.0),
2024-05-21 19:37:00 +02:00
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const TextBold(text: 'Communication preference'),
const Text('How do you prefer to communicate in a team?'),
...CommunicationPreference.values.map((option) {
2024-05-21 19:37:00 +02:00
return RadioListTile(
title: Text(option.displayName),
value: option,
groupValue: communicationPreference,
onChanged: (CommunicationPreference? value) {
2024-05-21 19:37:00 +02:00
setState(() {
communicationPreference = value;
2024-05-21 19:37:00 +02:00
});
},
);
}),
2024-06-14 14:28:59 +02:00
const SizedBox(height: 40),
2024-06-17 17:57:32 +02:00
const TextBold(text: 'Vision and goals'),
2024-06-14 14:28:59 +02:00
const Text('What is your long-term vision for a startup?'),
...VisionOption.values.map((option) {
return CheckboxListTile(
title: Text(option.displayName),
value: selectedVisionOptions[option],
controlAffinity: ListTileControlAffinity.platform,
onChanged: (bool? value) {
if (value != null) {
setState(() {
selectedVisionOptions[option] = value;
});
}
},
);
}),
2024-05-21 19:37:00 +02:00
const SizedBox(height: 20),
Center(
child: MyElevatedButton(
2024-05-21 19:37:00 +02:00
onPressed: handleSubmit,
child:
Text(widget.isRegProcess ? 'Save and continue' : 'Save'),
),
2024-05-21 14:01:44 +02:00
),
2024-05-21 19:37:00 +02:00
],
),
2024-05-21 14:01:44 +02:00
),
),
);
}
}