cofounderella/lib/pages/user_vision_page.dart

215 lines
6.4 KiB
Dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import '../components/my_elevated_button.dart';
import '../components/text_bold.dart';
import '../constants.dart';
import '../enumerations.dart';
import '../forms/corporate_culture_form.dart';
import '../services/auth/auth_service.dart';
import '../utils/helper_dialogs.dart';
class UserVisionPage extends StatefulWidget {
const UserVisionPage(
{super.key, required this.isRegProcess, required this.isEditMode});
final bool isRegProcess;
final bool isEditMode;
@override
UserVisionPageState createState() => UserVisionPageState();
}
class UserVisionPageState extends State<UserVisionPage> {
Map<VisionOption, bool> selectedVisionOptions = {
VisionOption.marketLeader: false,
VisionOption.sustainableBusiness: false,
VisionOption.innovativeProduct: false,
VisionOption.exitStrategy: false,
};
AvailabilityOption? availability;
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(
() {
// Load Vision options
if (data?[Constants.dbFieldUsersVisions] != null) {
for (var option in VisionOption.values) {
selectedVisionOptions[option] =
data?[Constants.dbFieldUsersVisions]
.contains(option.toString());
}
}
// Load Availability option
if (data?[Constants.dbFieldUsersAvailability] != null) {
availability = AvailabilityOption.values.firstWhereOrNull(
(e) => e.toString() == data?[Constants.dbFieldUsersAvailability],
);
}
},
);
}
}
Future<bool> _saveDataToFirebase() async {
try {
final userDoc = FirebaseFirestore.instance
.collection(Constants.dbCollectionUsers)
.doc(_authService.getCurrentUser()!.uid);
await userDoc.set(
{
Constants.dbFieldUsersVisions: selectedVisionOptions.entries
.where((entry) => entry.value)
.map((entry) => entry.key.toString())
.toList(),
Constants.dbFieldUsersAvailability: availability?.toString(),
},
SetOptions(merge: true), // avoid overwriting existing data
);
return true;
} catch (e) {
_showSnackBar(e.toString());
return false;
}
}
void _showSnackBar(String message) {
showErrorSnackBar(context, message);
}
bool isVisionSelected() {
return selectedVisionOptions.containsValue(true);
}
Future<void> handleSubmit() async {
if (!isVisionSelected()) {
_showSnackBar('Please select at least one long-term vision.');
return;
}
if (availability == null) {
_showSnackBar('Please select an availability option.');
return;
}
// Handle the form submission logic here
bool success = await _saveDataToFirebase();
if (success) {
_navigate();
} else {
_showSnackBar('Failed to save data.');
}
}
void _navigate() {
if (widget.isRegProcess) {
Navigator.push(
context,
MaterialPageRoute(
//
// set following registration page HERE
//
builder: (context) => CultureValuesFormPage(
isRegProcess: widget.isRegProcess,
isEditMode: false,
),
),
);
} else {
if (widget.isEditMode == true) {
// pass selectedOptions data back to caller
Navigator.pop(context, {
Constants.dbFieldUsersVisions: selectedVisionOptions.entries
.where((entry) => entry.value)
.map((entry) => entry.key)
.toList(),
Constants.dbFieldUsersAvailability: availability,
});
} else {
Navigator.pop(context);
}
}
}
@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),
)
],
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const TextBold(text: 'Availability and commitment'),
const Text(
'How much time can you devote to the startup each week?',
),
...AvailabilityOption.values.map((option) {
return RadioListTile(
title: Text(option.displayName),
value: option,
groupValue: availability,
onChanged: (AvailabilityOption? value) {
setState(() {
availability = value;
});
},
);
}),
const SizedBox(height: 40),
const TextBold(text: 'Vision and goals'),
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;
});
}
},
);
}),
const SizedBox(height: 20),
Center(
child: MyElevatedButton(
onPressed: handleSubmit,
child:
Text(widget.isRegProcess ? 'Save and continue' : 'Save'),
),
),
],
),
),
),
);
}
}