diff --git a/Frontend/patient_manager/lib/components/buildFilesList.dart b/Frontend/patient_manager/lib/components/buildFilesList.dart index e4dbb84b..2aa25641 100644 --- a/Frontend/patient_manager/lib/components/buildFilesList.dart +++ b/Frontend/patient_manager/lib/components/buildFilesList.dart @@ -1,17 +1,25 @@ +import 'dart:convert'; + import 'package:flutter/material.dart'; import 'package:patient_manager/components/BuildFileView.dart'; +import 'package:patient_manager/components/mihDeleteMessage.dart'; +import 'package:patient_manager/components/myErrorMessage.dart'; +import 'package:patient_manager/components/mySuccessMessage.dart'; import 'package:patient_manager/components/mybutton.dart'; import 'package:patient_manager/env/env.dart'; import 'package:patient_manager/main.dart'; +import 'package:patient_manager/objects/appUser.dart'; import 'package:patient_manager/objects/files.dart'; -//import 'dart:js' as js; +import 'package:supertokens_flutter/http.dart' as http; import "package:universal_html/html.dart" as html; class BuildFilesList extends StatefulWidget { + final AppUser signedInUser; final List files; const BuildFilesList({ super.key, required this.files, + required this.signedInUser, }); @override @@ -20,8 +28,80 @@ class BuildFilesList extends StatefulWidget { class _BuildFilesListState extends State { int indexOn = 0; + final baseAPI = AppEnviroment.baseApiUrl; - void viewFilePopUp(String fileName, String filePath) { + Future deleteFileApiCall(String filePath, int fileID) async { + // delete file from minio + var response = await http.delete( + Uri.parse("$baseAPI/minio/delete/file/"), + headers: { + "Content-Type": "application/json; charset=UTF-8" + }, + body: jsonEncode({"file_path": filePath}), + ); + //print("Here4"); + //print(response.statusCode); + if (response.statusCode == 200) { + //SQL delete + var response2 = await http.delete( + Uri.parse("$baseAPI/files/delete/"), + headers: { + "Content-Type": "application/json; charset=UTF-8" + }, + body: jsonEncode({"idpatient_files": fileID}), + ); + if (response2.statusCode == 200) { + Navigator.of(context).pop(); + Navigator.of(context).pop(); + Navigator.of(context) + .pushNamed('/patient-profile', arguments: widget.signedInUser); + setState(() {}); + String message = + "The File has been deleted successfully. This means it will no longer be visible on your and cannot be used for future appointments."; + successPopUp(message); + } else { + internetConnectionPopUp(); + } + } else { + internetConnectionPopUp(); + } + } + + void internetConnectionPopUp() { + showDialog( + context: context, + builder: (context) { + return const MyErrorMessage(errorType: "Internet Connection"); + }, + ); + } + + void successPopUp(String message) { + showDialog( + context: context, + builder: (context) { + return MySuccessMessage( + successType: "Success", + successMessage: message, + ); + }, + ); + } + + void deleteFilePopUp(String filePath, int fileID) { + showDialog( + context: context, + barrierDismissible: false, + builder: (context) => MIHDeleteMessage( + deleteType: "File", + onTap: () { + deleteFileApiCall(filePath, fileID); + }, + ), + ); + } + + void viewFilePopUp(String fileName, String filePath, int fileID) { showDialog( context: context, barrierDismissible: false, @@ -43,16 +123,33 @@ class _BuildFilesListState extends State { child: Column( mainAxisSize: MainAxisSize.min, children: [ - Text( - fileName, - textAlign: TextAlign.center, - style: TextStyle( - color: MzanziInnovationHub.of(context)! - .theme - .secondaryColor(), - fontSize: 35.0, - fontWeight: FontWeight.bold, - ), + Row( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + Text( + fileName, + textAlign: TextAlign.center, + style: TextStyle( + color: MzanziInnovationHub.of(context)! + .theme + .secondaryColor(), + fontSize: 35.0, + fontWeight: FontWeight.bold, + ), + ), + IconButton( + onPressed: () { + deleteFilePopUp(filePath, fileID); + }, + icon: Icon( + Icons.delete, + color: MzanziInnovationHub.of(context)! + .theme + .secondaryColor(), + ), + ), + ], ), const SizedBox(height: 25.0), Expanded( @@ -138,6 +235,7 @@ class _BuildFilesListState extends State { viewFilePopUp( widget.files[index].file_name, widget.files[index].file_path, + widget.files[index].idpatient_files, ); }, ); diff --git a/Frontend/patient_manager/lib/components/patientFiles.dart b/Frontend/patient_manager/lib/components/patientFiles.dart index 0a8c604e..7d1ebfe5 100644 --- a/Frontend/patient_manager/lib/components/patientFiles.dart +++ b/Frontend/patient_manager/lib/components/patientFiles.dart @@ -169,58 +169,7 @@ class _PatientFilesState extends State { // end loading circle Navigator.of(context).pop(); String message = - "The medical certificate ${file.name.replaceAll(RegExp(r' '), '-')} has been successfully generated and added to ${widget.selectedPatient.first_name} ${widget.selectedPatient.last_name}'s record. You can now access and download it for their use."; - successPopUp(message); - } else { - internetConnectionPopUp(); - } - } else { - internetConnectionPopUp(); - } - } - - Future uploadSelectedFilev1(PlatformFile file) async { - //var strem = new http.ByteStream.fromBytes(file.bytes.) - //start loading circle - showDialog( - context: context, - builder: (context) { - return const Center( - child: CircularProgressIndicator(), - ); - }, - ); - - var request = http2.MultipartRequest( - 'POST', Uri.parse("${AppEnviroment.baseApiUrl}/files/upload/file/")); - request.fields['app_id'] = widget.selectedPatient.app_id; - request.headers['Content-Type'] = 'multipart/form-data'; - request.files.add(await http2.MultipartFile.fromBytes('file', file.bytes!, - filename: file.name.replaceAll(RegExp(r' '), '-'))); - var response1 = await request.send(); - //print(response1.statusCode); - if (response1.statusCode == 200) { - var response2 = await http.post( - Uri.parse(endpointInsertFiles), - headers: { - "Content-Type": "application/json; charset=UTF-8" - }, - body: jsonEncode({ - "file_path": file.name.replaceAll(RegExp(r' '), '-'), - "file_name": file.name.replaceAll(RegExp(r' '), '-'), - "patient_id": widget.patientIndex - }), - ); - //print(response2.statusCode); - if (response2.statusCode == 201) { - setState(() { - selectedFileController.clear(); - futueFiles = fetchFiles(); - }); - // end loading circle - Navigator.of(context).pop(); - String message = - "The medical certificate ${file.name.replaceAll(RegExp(r' '), '-')} has been successfully generated and added to ${widget.selectedPatient.first_name} ${widget.selectedPatient.last_name}'s record. You can now access and download it for their use."; + "The file ${file.name.replaceAll(RegExp(r' '), '-')} has been successfully generated and added to ${widget.selectedPatient.first_name} ${widget.selectedPatient.last_name}'s record. You can now access and download it for their use."; successPopUp(message); } else { internetConnectionPopUp(); @@ -712,7 +661,10 @@ class _PatientFilesState extends State { .secondaryColor()), ), const SizedBox(height: 10), - BuildFilesList(files: filesList), + BuildFilesList( + files: filesList, + signedInUser: widget.signedInUser, + ), ]), ), );