cofounderella/lib/forms/risks_form.dart

192 lines
5.7 KiB
Dart
Raw Permalink Normal View History

2024-05-22 02:46:46 +02:00
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import '../components/my_elevated_button.dart';
import '../components/text_bold.dart';
2024-05-22 02:46:46 +02:00
import '../constants.dart';
import '../enumerations.dart';
import '../pages/registration_complete_page.dart';
import '../services/auth/auth_service.dart';
import '../utils/helper_dialogs.dart';
2024-05-22 02:46:46 +02:00
class RisksFormPage extends StatefulWidget {
2024-05-31 23:55:33 +02:00
const RisksFormPage(
{super.key, required this.isRegProcess, required this.isEditMode});
2024-05-22 02:46:46 +02:00
final bool isRegProcess;
2024-05-31 23:55:33 +02:00
final bool isEditMode;
2024-05-22 02:46:46 +02:00
@override
RisksFormPageState createState() => RisksFormPageState();
}
class RisksFormPageState extends State<RisksFormPage> {
AvailabilityOption? availability;
2024-05-31 23:55:33 +02:00
RiskTolerance? riskPreference;
2024-05-22 02:46:46 +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 Availability option
if (data[Constants.dbFieldUsersAvailability] != null) {
availability = AvailabilityOption.values.firstWhereOrNull(
(x) => x.toString() == data[Constants.dbFieldUsersAvailability],
);
2024-05-22 02:46:46 +02:00
}
// Load Risk Tolerance
if (data[Constants.dbFieldUsersRiskTolerance] != null) {
2024-05-31 23:55:33 +02:00
riskPreference = RiskTolerance.values.firstWhereOrNull((x) =>
2024-05-22 02:46:46 +02:00
x.toString() == data[Constants.dbFieldUsersRiskTolerance]);
}
}
});
}
}
2024-05-31 23:55:33 +02:00
Future<bool> _saveDataToFirebase() async {
try {
final userDoc = FirebaseFirestore.instance
.collection(Constants.dbCollectionUsers)
.doc(_authService.getCurrentUser()!.uid);
2024-05-22 02:46:46 +02:00
2024-05-31 23:55:33 +02:00
await userDoc.set(
{
Constants.dbFieldUsersAvailability: availability.toString(),
2024-05-31 23:55:33 +02:00
Constants.dbFieldUsersRiskTolerance: riskPreference?.toString(),
},
SetOptions(merge: true), // avoid overwriting existing data
);
return true;
} catch (e) {
_showSnackBar(e.toString());
return false;
}
}
void _showSnackBar(String message) {
showErrorSnackBar(context, message);
2024-05-22 02:46:46 +02:00
}
2024-05-31 23:55:33 +02:00
Future<void> handleSubmit() async {
if (availability == null) {
_showSnackBar('Please select an availability option.');
2024-05-22 02:46:46 +02:00
return;
}
2024-05-31 23:55:33 +02:00
if (riskPreference == null) {
showErrorSnackBar(context, 'Please select a willingness to take risks.');
2024-05-22 02:46:46 +02:00
return;
}
// Handle the form submission logic here
2024-05-31 23:55:33 +02:00
bool success = await _saveDataToFirebase();
if (success) {
2024-06-07 02:18:20 +02:00
_navigate();
2024-05-31 23:55:33 +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(
//
// TODO set following registration page HERE
//
builder: (context) => const RegistrationCompletePage(),
2024-05-31 23:55:33 +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.dbFieldUsersAvailability: availability,
2024-06-07 02:18:20 +02:00
Constants.dbFieldUsersRiskTolerance: riskPreference,
});
} else {
Navigator.pop(context);
}
2024-05-31 23:55:33 +02:00
}
2024-05-22 02:46:46 +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-22 02:46:46 +02:00
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const TextBold(text: 'Availability and commitment'),
2024-05-22 02:46:46 +02:00
const Text(
'How much time can you devote to the startup each week?',
2024-05-22 02:46:46 +02:00
),
...AvailabilityOption.values.map((option) {
2024-05-22 02:46:46 +02:00
return RadioListTile(
title: Text(option.displayName),
value: option,
groupValue: availability,
onChanged: (AvailabilityOption? value) {
2024-05-22 02:46:46 +02:00
setState(() {
availability = value;
2024-05-22 02:46:46 +02:00
});
},
);
}),
const SizedBox(height: 40),
const TextBold(text: 'Risk tolerance'),
2024-05-22 02:46:46 +02:00
const Text('How do you deal with uncertainty and risk?'),
...RiskTolerance.values.map((option) {
return RadioListTile(
title: Text(option.displayName),
value: option,
2024-05-31 23:55:33 +02:00
groupValue: riskPreference,
2024-05-22 02:46:46 +02:00
onChanged: (RiskTolerance? value) {
setState(() {
2024-05-31 23:55:33 +02:00
riskPreference = value;
2024-05-22 02:46:46 +02:00
});
},
);
}),
const SizedBox(height: 20),
Center(
child: MyElevatedButton(
2024-05-22 02:46:46 +02:00
onPressed: handleSubmit,
child:
Text(widget.isRegProcess ? 'Save and continue' : 'Save'),
),
),
],
),
),
),
);
}
}