update add patient to take in app user and navigate to view patient when completed

This commit is contained in:
2024-07-26 14:30:18 +02:00
parent 903e07a901
commit 94067ec251

View File

@@ -13,11 +13,11 @@ import '../components/myAppBar.dart';
import 'package:supertokens_flutter/http.dart' as http; import 'package:supertokens_flutter/http.dart' as http;
class AddPatient extends StatefulWidget { class AddPatient extends StatefulWidget {
final String userEmail; final AppUser signedInUser;
const AddPatient({ const AddPatient({
super.key, super.key,
required this.userEmail, required this.signedInUser,
}); });
@override @override
@@ -38,27 +38,10 @@ class _AddPatientState extends State<AddPatient> {
final medMainMemController = TextEditingController(); final medMainMemController = TextEditingController();
final medAidCodeController = TextEditingController(); final medAidCodeController = TextEditingController();
final docOfficeIdApiUrl = "${AppEnviroment.baseApiUrl}/users/profile/"; final baseAPI = AppEnviroment.baseApiUrl;
final apiUrl = "${AppEnviroment.baseApiUrl}/patients/insert/";
late int futureDocOfficeId; late int futureDocOfficeId;
late bool medRequired; late bool medRequired;
Future getOfficeIdByUser(String endpoint) async {
final response = await http.get(Uri.parse(endpoint));
if (response.statusCode == 200) {
String body = response.body;
var decodedData = jsonDecode(body);
AppUser u = AppUser.fromJson(decodedData as Map<String, dynamic>);
setState(() {
futureDocOfficeId = u.docOffice_id;
//print(futureDocOfficeId);
});
} else {
internetConnectionPopUp();
throw Exception('failed to load patients');
}
}
bool isFieldsFilled() { bool isFieldsFilled() {
if (medRequired) { if (medRequired) {
if (idController.text.isEmpty || if (idController.text.isEmpty ||
@@ -93,10 +76,8 @@ class _AddPatientState extends State<AddPatient> {
} }
Future<void> addPatientAPICall() async { Future<void> addPatientAPICall() async {
await getOfficeIdByUser(docOfficeIdApiUrl + widget.userEmail);
print(futureDocOfficeId.toString());
var response = await http.post( var response = await http.post(
Uri.parse(apiUrl), Uri.parse("$baseAPI/patients/insert/"),
headers: <String, String>{ headers: <String, String>{
"Content-Type": "application/json; charset=UTF-8" "Content-Type": "application/json; charset=UTF-8"
}, },
@@ -113,14 +94,14 @@ class _AddPatientState extends State<AddPatient> {
"medical_aid_name": medNameController.text, "medical_aid_name": medNameController.text,
"medical_aid_scheme": medSchemeController.text, "medical_aid_scheme": medSchemeController.text,
"address": addressController.text, "address": addressController.text,
"doc_office_id": futureDocOfficeId, "app_id": widget.signedInUser.app_id,
}), }),
); );
if (response.statusCode == 201) { if (response.statusCode == 201) {
Navigator.of(context) Navigator.pushNamed(context, '/patient-manager/patient',
.pushNamed('/patient-manager', arguments: widget.userEmail); arguments: widget.signedInUser);
String message = String message =
"${fnameController.text} ${lnameController.text} has been successfully added to the Patient Manager! You can now view their details, add notes & documents, and update their information."; "${fnameController.text} ${lnameController.text} patient profiole has been successfully added!\n";
successPopUp(message); successPopUp(message);
} else { } else {
internetConnectionPopUp(); internetConnectionPopUp();
@@ -206,7 +187,7 @@ class _AddPatientState extends State<AddPatient> {
child: MyTextField( child: MyTextField(
controller: fnameController, controller: fnameController,
hintText: "First Name", hintText: "First Name",
editable: true, editable: false,
required: true, required: true,
), ),
), ),
@@ -219,7 +200,7 @@ class _AddPatientState extends State<AddPatient> {
child: MyTextField( child: MyTextField(
controller: lnameController, controller: lnameController,
hintText: "Last Name", hintText: "Last Name",
editable: true, editable: false,
required: true, required: true,
), ),
), ),
@@ -245,7 +226,7 @@ class _AddPatientState extends State<AddPatient> {
child: MyTextField( child: MyTextField(
controller: emailController, controller: emailController,
hintText: "Email", hintText: "Email",
editable: true, editable: false,
required: true, required: true,
), ),
), ),
@@ -397,6 +378,9 @@ class _AddPatientState extends State<AddPatient> {
void initState() { void initState() {
medAidController.addListener(isRequired); medAidController.addListener(isRequired);
setState(() { setState(() {
fnameController.text = widget.signedInUser.fname;
lnameController.text = widget.signedInUser.lname;
emailController.text = widget.signedInUser.email;
medAidController.text = "No"; medAidController.text = "No";
}); });
super.initState(); super.initState();