From c1f854bc689a054305b04d040d43c8dff786c415 Mon Sep 17 00:00:00 2001 From: padmanto Date: Sun, 12 Jun 2022 12:23:50 +0700 Subject: [PATCH] Delete Mitra --- lib/repository/mitra_repository.dart | 15 ++++++ lib/widget/fx_data_mitra.dart | 49 +++++++++++++++-- lib/widget/provider/mitra_delete_provider.dart | 75 ++++++++++++++++++++++++++ php-api/mitra/Md.php | 41 ++++++++++++++ 4 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 lib/widget/provider/mitra_delete_provider.dart diff --git a/lib/repository/mitra_repository.dart b/lib/repository/mitra_repository.dart index 8c1c583..9adf7d5 100644 --- a/lib/repository/mitra_repository.dart +++ b/lib/repository/mitra_repository.dart @@ -10,6 +10,21 @@ import 'base_repository.dart'; class MitraRepository extends BaseRepository { MitraRepository({required super.dio}); + Future delete({ + required String token, + required String mitraID, + CancelToken? cancelToken, + }) async { + final param = { + "token": token, + "mitraID": mitraID, + }; + + final service = "${Constants.baseUrl}md/delete"; + await post(service: service, jsonParam: param, cancelToken: cancelToken); + return true; + } + Future add({ required String token, required String companyID, diff --git a/lib/widget/fx_data_mitra.dart b/lib/widget/fx_data_mitra.dart index 4208f37..6ff1c61 100644 --- a/lib/widget/fx_data_mitra.dart +++ b/lib/widget/fx_data_mitra.dart @@ -5,11 +5,10 @@ import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:onemd/model/ac_company_response_model.dart'; import 'package:onemd/model/ac_doctor_model.dart'; +import 'package:onemd/widget/provider/mitra_delete_provider.dart'; import '../model/ac_mou_response_model.dart'; import '../model/mitra_response_model.dart'; -import '../provider/dio_provider.dart'; -import '../repository/mitra_repository.dart'; import '../screen/md_lab_mitra/mitra_lookup_mou_provider.dart'; import '../screen/md_lab_mitra/mitra_search_provider.dart'; import 'fx_error_text.dart'; @@ -89,6 +88,44 @@ class FxDataMitra extends HookConsumerWidget { totalRow: list.value.length, list: list.value, pageWidth: pageWidth, + onDelete: (model) async { + await showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: const Text("Delete Mitra"), + content: Text( + "Confirm delete ${model.mitraUsername} , ${model.mCompanyName} ?"), + actions: [ + TextButton( + onPressed: () { + ref + .read(mitraDeleteProvider.notifier) + .delete(mitraID: model.mitraID, query: ""); + Navigator.of(context).pop(); + }, + style: ButtonStyle( + backgroundColor: MaterialStateColor.resolveWith( + (state) => Colors.red), + ), + child: const Text("Delete", + style: TextStyle(color: Colors.white)), + ), + TextButton( + style: ButtonStyle( + backgroundColor: MaterialStateColor.resolveWith( + (state) => Colors.green), + ), + onPressed: () { + Navigator.of(context).pop(); + }, + child: const Text("Cancel", + style: TextStyle(color: Colors.white)), + ) + ], + ); + }); + }, onEdit: (model) async { ref.read(selectedMouProvider.notifier).state = model.aggrementID .map((id) => AcMouResponseModel( @@ -179,11 +216,13 @@ class _MitraDataSource extends DataTableSource { final int totalRow; final double pageWidth; final void Function(MitraResponseModel)? onEdit; + final void Function(MitraResponseModel)? onDelete; _MitraDataSource({ required this.list, required this.totalRow, required this.pageWidth, this.onEdit, + this.onDelete, }); @override DataRow? getRow(int index) { @@ -223,7 +262,11 @@ class _MitraDataSource extends DataTableSource { ), ), InkWell( - onTap: () {}, + onTap: () async { + if (onDelete != null) { + onDelete!(model); + } + }, child: Icon( Icons.delete_rounded, size: 24, diff --git a/lib/widget/provider/mitra_delete_provider.dart b/lib/widget/provider/mitra_delete_provider.dart new file mode 100644 index 0000000..ebb5d1b --- /dev/null +++ b/lib/widget/provider/mitra_delete_provider.dart @@ -0,0 +1,75 @@ +import 'package:dio/dio.dart'; +import 'package:equatable/equatable.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../../provider/dio_provider.dart'; +import '../../provider/local_auth_provider.dart'; +import '../../repository/base_repository.dart'; +import '../../repository/mitra_repository.dart'; +import '../../screen/md_lab_mitra/mitra_search_provider.dart'; + +final mitraDeleteProvider = + StateNotifierProvider( + (ref) => MitraDeleteNotifier(ref: ref), +); + +class MitraDeleteNotifier extends StateNotifier { + final Ref ref; + CancelToken? cancelToken; + MitraDeleteNotifier({ + required this.ref, + }) : super(MitraDeleteStateInit()); + + void reset() { + state = MitraDeleteStateInit(); + } + + void delete({ + required String mitraID, + required String query, + }) async { + try { + state = MitraDeleteStateLoading(); + final dio = ref.read(dioProvider); + final localAuth = ref.read(localAuthProvider); + if (localAuth?.token == null) { + throw BaseRepositoryException(message: "Invalid Token"); + } + await MitraRepository(dio: dio).delete( + mitraID: mitraID, + token: localAuth!.token!, + ); + state = MitraDeleteStateDone(); + ref.read(mitraSearchProvider.notifier).search(query: query); + } catch (e) { + if (e is BaseRepositoryException) { + state = MitraDeleteStateError(message: e.message); + } else { + state = MitraDeleteStateError(message: "Unknown Error "); + } + } + } +} + +abstract class MitraDeleteState extends Equatable { + final DateTime date; + MitraDeleteState() : date = DateTime.now(); + @override + List get props => throw [date]; +} + +class MitraDeleteStateInit extends MitraDeleteState {} + +class MitraDeleteStateLoading extends MitraDeleteState {} + +class MitraDeleteStateError extends MitraDeleteState { + final String message; + + MitraDeleteStateError({ + required this.message, + }); +} + +class MitraDeleteStateDone extends MitraDeleteState { + MitraDeleteStateDone(); +} diff --git a/php-api/mitra/Md.php b/php-api/mitra/Md.php index 891ec41..163f049 100644 --- a/php-api/mitra/Md.php +++ b/php-api/mitra/Md.php @@ -12,6 +12,47 @@ class Md extends MY_Controller { echo "Mitra:MD:API"; } + function delete() + { + $param = $this->sys_input; + $user = $this->sys_user; + $userID = $user["M_UserID"]; + + $this->db->trans_begin(); + + $sql = "update mitra + set MitraIsActive = 'Y', + MitraM_UserID=? + where MitraID = ?"; + $qry = $this->db->query($sql, [ + $userID, $param["mitraID"] + ]); + if (!$qry) { + echo json_encode([ + "status" => "ERR", + "message" => $this->db->error()["message"], + ]); + exit(); + } + $mitraID = $param["mitraID"]; + $sql = "update mitra_mou set MitraMouIsActive ='N', + MitraMouM_UserID = ? + where + MitraMouMitraID = ? "; + $qry = $this->db->query($sql, [$userID, $param["mitraID"]]); + + if (!$qry) { + echo json_encode([ + "status" => "ERR", + "message" => $this->db->error()["message"], + ]); + $this->db->trans_rollback(); + exit(); + } + + $this->db->trans_commit(); + echo json_encode(["status" => "OK"]); + } function edit() { $param = $this->sys_input;