forked from yaso_meth/mih-project
enable API call from patient manager
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:patient_manager/objects/patients.dart';
|
||||
|
||||
class BuildPatientsList extends StatefulWidget {
|
||||
final List<Patient> patients;
|
||||
const BuildPatientsList({
|
||||
super.key,
|
||||
required this.patients,
|
||||
});
|
||||
|
||||
@override
|
||||
State<BuildPatientsList> createState() => _BuildPatientsListState();
|
||||
}
|
||||
|
||||
class _BuildPatientsListState extends State<BuildPatientsList> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView.builder(
|
||||
itemCount: widget.patients.length,
|
||||
itemBuilder: (context, index) {
|
||||
final patient = widget.patients[index];
|
||||
return ListTile(
|
||||
title: Text(patient.first_name + " " + patient.last_name),
|
||||
subtitle: Text(patient.id_no),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,12 @@ class _MyAppDrawerState extends State<MyAppDrawer> {
|
||||
color: Colors.blueAccent,
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
title: Text("Home"),
|
||||
onTap: () {
|
||||
Navigator.of(context).pushNamed('/home');
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
59
Frontend/patient_manager/lib/objects/patients.dart
Normal file
59
Frontend/patient_manager/lib/objects/patients.dart
Normal file
@@ -0,0 +1,59 @@
|
||||
class Patient {
|
||||
final int idpatients;
|
||||
final String id_no;
|
||||
final String first_name;
|
||||
final String last_name;
|
||||
final String email;
|
||||
final String cell_no;
|
||||
final String medical_aid_name;
|
||||
final String medical_aid_no;
|
||||
final String medical_aid_scheme;
|
||||
final String address;
|
||||
final int doc_office_id;
|
||||
|
||||
const Patient({
|
||||
required this.idpatients,
|
||||
required this.id_no,
|
||||
required this.first_name,
|
||||
required this.last_name,
|
||||
required this.email,
|
||||
required this.cell_no,
|
||||
required this.medical_aid_name,
|
||||
required this.medical_aid_no,
|
||||
required this.medical_aid_scheme,
|
||||
required this.address,
|
||||
required this.doc_office_id,
|
||||
});
|
||||
|
||||
factory Patient.fromJson(Map<String, dynamic> json) {
|
||||
return switch (json) {
|
||||
{
|
||||
"idpatients": int idpatients,
|
||||
'id_no': String id_no,
|
||||
'first_name': String first_name,
|
||||
'last_name': String last_name,
|
||||
'email': String email,
|
||||
'cell_no': String cell_no,
|
||||
'medical_aid_name': String medical_aid_name,
|
||||
'medical_aid_no': String medical_aid_no,
|
||||
'medical_aid_scheme': String medical_aid_scheme,
|
||||
'address': String address,
|
||||
'doc_office_id': int doc_office_id,
|
||||
} =>
|
||||
Patient(
|
||||
idpatients: idpatients,
|
||||
id_no: id_no,
|
||||
first_name: first_name,
|
||||
last_name: last_name,
|
||||
email: email,
|
||||
cell_no: cell_no,
|
||||
medical_aid_name: medical_aid_name,
|
||||
medical_aid_no: medical_aid_no,
|
||||
medical_aid_scheme: medical_aid_scheme,
|
||||
address: address,
|
||||
doc_office_id: doc_office_id,
|
||||
),
|
||||
_ => throw const FormatException('Failed to load album.'),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,33 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:patient_manager/components/buildPatientList.dart';
|
||||
import 'package:patient_manager/components/myAppBar.dart';
|
||||
import 'package:patient_manager/components/myAppDrawer.dart';
|
||||
import 'package:patient_manager/main.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:mysql_client/mysql_client.dart';
|
||||
import 'package:patient_manager/objects/patients.dart';
|
||||
|
||||
Future<List<Patient>> fetchPatients() async {
|
||||
print("fetch patients");
|
||||
final response = await http.get(Uri.parse('http://localhost:80/patients/'));
|
||||
//print("Status Code: " + response.statusCode.toString());
|
||||
if (response.statusCode == 200) {
|
||||
Iterable l = jsonDecode(response.body);
|
||||
List<Patient> patients =
|
||||
List<Patient>.from(l.map((model) => Patient.fromJson(model)));
|
||||
print("convert response to json");
|
||||
//print(response.body);
|
||||
// final patientsData =
|
||||
// Patient.fromJson(jsonDecode(response.body)[0] as Map<String, dynamic>);
|
||||
print(patients);
|
||||
return patients;
|
||||
} else {
|
||||
throw Exception('failed to load patients');
|
||||
}
|
||||
}
|
||||
|
||||
class PatientManager extends StatefulWidget {
|
||||
const PatientManager({super.key});
|
||||
@@ -12,35 +38,77 @@ class PatientManager extends StatefulWidget {
|
||||
|
||||
class _PatientManagerState extends State<PatientManager> {
|
||||
String useremail = "";
|
||||
String endpoint = "http://localhost:80/patients/";
|
||||
late MySQLConnection conn;
|
||||
String resultsofDB = "";
|
||||
late Future<List<Patient>> futurePatients;
|
||||
|
||||
Future<void> getUserEmail() async {
|
||||
Future<void> getuserEmail() async {
|
||||
final res = await client.auth.getUser();
|
||||
//final response = await http.get(Uri.parse(endpoint));
|
||||
//print(json.decode(response.body));
|
||||
if (res.user!.email != null) {
|
||||
//print("emai not null");
|
||||
print("User: " + res.user!.email.toString());
|
||||
useremail = res.user!.email!;
|
||||
//print(useremail);
|
||||
print(useremail);
|
||||
}
|
||||
}
|
||||
|
||||
String getEmail() {
|
||||
return useremail;
|
||||
@override
|
||||
void initState() {
|
||||
print("init now");
|
||||
getuserEmail();
|
||||
futurePatients = fetchPatients();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder(
|
||||
future: getUserEmail(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.done) {
|
||||
return Scaffold(
|
||||
appBar: const MyAppBar(barTitle: "Patient Manager"),
|
||||
body: Center(child: Text(useremail)),
|
||||
drawer: MyAppDrawer(drawerTitle: useremail),
|
||||
);
|
||||
} else {
|
||||
return Center(child: CircularProgressIndicator());
|
||||
}
|
||||
},
|
||||
return Scaffold(
|
||||
appBar: const MyAppBar(barTitle: "Patient Manager"),
|
||||
drawer: FutureBuilder(
|
||||
future: getuserEmail(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return MyAppDrawer(drawerTitle: useremail);
|
||||
} else {
|
||||
return const MyAppDrawer(drawerTitle: "Error pulling email");
|
||||
}
|
||||
},
|
||||
),
|
||||
body: FutureBuilder(
|
||||
future: futurePatients,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const CircularProgressIndicator();
|
||||
} else if (snapshot.hasData) {
|
||||
final patientsList = snapshot.data!;
|
||||
return BuildPatientsList(
|
||||
patients: patientsList,
|
||||
);
|
||||
} else {
|
||||
return const MyAppDrawer(drawerTitle: "Error pulling email");
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
// FutureBuilder<Patient>(
|
||||
// future: futurePatients,
|
||||
// builder: (context, snapshot) {
|
||||
// if (snapshot.hasData) {
|
||||
// return Scaffold(
|
||||
// appBar: const MyAppBar(barTitle: "Patient Manager"),
|
||||
// body: Center(child: Text(snapshot.data.toString())),
|
||||
// drawer: MyAppDrawer(drawerTitle: useremail),
|
||||
// );
|
||||
// } else if (snapshot.hasError) {
|
||||
// print('${snapshot.error}');
|
||||
// return Text('${snapshot.error}');
|
||||
// } else {
|
||||
// return Center(child: CircularProgressIndicator());
|
||||
// }
|
||||
// },
|
||||
// );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,8 @@ dependencies:
|
||||
cupertino_icons: ^1.0.2
|
||||
supabase_auth_ui: ^0.4.1
|
||||
supabase_flutter: ^2.4.0
|
||||
http: ^1.2.0
|
||||
mysql_client: ^0.0.27
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
||||
Reference in New Issue
Block a user