padmanto
2 years ago
9 changed files with 537 additions and 32 deletions
@ -0,0 +1,202 @@
|
||||
import 'dart:async'; |
||||
|
||||
import 'package:flutter/material.dart'; |
||||
import 'package:flutter_hooks/flutter_hooks.dart'; |
||||
import 'package:hooks_riverpod/hooks_riverpod.dart'; |
||||
import 'package:onemd/widget/fx_doctor_address.dart'; |
||||
|
||||
import 'fx_company_lookup.dart'; |
||||
import 'fx_doctor_lookup.dart'; |
||||
import 'fx_mitra_mou.dart'; |
||||
import 'fx_text_field.dart'; |
||||
import 'provider/mitra_add_provider.dart'; |
||||
import 'provider/mitra_edit_provider.dart'; |
||||
import 'provider/selectedCompanyProvider.dart'; |
||||
import 'provider/selectedDoctorProvider.dart'; |
||||
|
||||
class FxMitraEditDialog extends HookConsumerWidget { |
||||
final String login; |
||||
final String idNo; |
||||
final String mitraID; |
||||
|
||||
const FxMitraEditDialog({ |
||||
Key? key, |
||||
required this.login, |
||||
required this.idNo, |
||||
required this.mitraID, |
||||
}) : super(key: key); |
||||
|
||||
@override |
||||
Widget build(BuildContext context, WidgetRef ref) { |
||||
final fcLogin = FocusNode(); |
||||
final company = ref.watch(selectedAcCompanyProvider); |
||||
final doctor = ref.watch(selectedAcDoctorProvider); |
||||
final doctorAddress = ref.watch(selectedAcDoctorAddressProvider); |
||||
final mou = ref.watch(selectedMouProvider); |
||||
|
||||
final errorCompany = useState<String?>(null); |
||||
final errorDoctor = useState<String?>(null); |
||||
final errorDoctorAddress = useState<String?>(null); |
||||
final errorMou = useState<String?>(null); |
||||
final errorLogin = useState<String?>(null); |
||||
|
||||
final fcPassword = FocusNode(); |
||||
final fcRePassword = FocusNode(); |
||||
final errorPassword = useState<String?>(null); |
||||
final errorRePassword = useState<String?>(null); |
||||
|
||||
final ctrlLogin = useTextEditingController(text: login); |
||||
final ctrlPassword = useTextEditingController(text: ""); |
||||
final ctrlRePassword = useTextEditingController(text: ""); |
||||
|
||||
bool Function() validationError; |
||||
validationError = () { |
||||
bool haveError = false; |
||||
if (ctrlRePassword.text != ctrlPassword.text) { |
||||
errorRePassword.value = "Password confirmation error"; |
||||
haveError = true; |
||||
} |
||||
if (company == null) { |
||||
errorCompany.value = "Company is mandatory"; |
||||
haveError = true; |
||||
} |
||||
if (mou.isEmpty) { |
||||
errorMou.value = "Mou is mandatory"; |
||||
haveError = true; |
||||
} |
||||
if (doctor == null) { |
||||
errorDoctor.value = "Doctor is mandatory"; |
||||
haveError = true; |
||||
} |
||||
if (doctorAddress == null) { |
||||
errorDoctorAddress.value = "Doctor Address is mandatory"; |
||||
haveError = true; |
||||
} |
||||
if (ctrlLogin.text == "") { |
||||
errorLogin.value = "Login is mandatory"; |
||||
haveError = true; |
||||
} |
||||
Timer(const Duration(seconds: 3), () { |
||||
errorCompany.value = null; |
||||
errorMou.value = null; |
||||
errorDoctor.value = null; |
||||
errorDoctorAddress.value = null; |
||||
errorLogin.value = null; |
||||
}); |
||||
return haveError; |
||||
}; |
||||
return Dialog( |
||||
shape: RoundedRectangleBorder( |
||||
side: const BorderSide(), |
||||
borderRadius: BorderRadius.circular(10), |
||||
), |
||||
child: SizedBox( |
||||
width: 800, |
||||
height: 600, |
||||
child: SingleChildScrollView( |
||||
child: Padding( |
||||
padding: const EdgeInsets.all(20.0), |
||||
child: Column( |
||||
crossAxisAlignment: CrossAxisAlignment.start, |
||||
children: [ |
||||
FxAcCompany( |
||||
readOnly: true, |
||||
errorValidation: errorCompany.value, |
||||
), |
||||
const SizedBox(height: 10), |
||||
FxMitraMou( |
||||
errorValidation: errorMou.value, |
||||
), |
||||
const SizedBox(height: 10), |
||||
FxAcDoctor( |
||||
errorValidation: errorDoctor.value, |
||||
), |
||||
const SizedBox(height: 10), |
||||
FxDoctorAddress( |
||||
errorValidation: errorDoctorAddress.value, |
||||
), |
||||
const SizedBox(height: 10), |
||||
FxTextField( |
||||
ctrl: ctrlLogin, |
||||
fc: fcLogin, |
||||
hint: "Login", |
||||
label: "Login", |
||||
errorMessage: errorLogin.value, |
||||
), |
||||
const SizedBox(height: 10), |
||||
FxTextField( |
||||
ctrl: ctrlPassword, |
||||
fc: fcPassword, |
||||
hint: "Password", |
||||
label: "Password", |
||||
obscureText: true, |
||||
errorMessage: errorPassword.value, |
||||
), |
||||
const SizedBox(height: 10), |
||||
FxTextField( |
||||
ctrl: ctrlRePassword, |
||||
fc: fcRePassword, |
||||
hint: "Retype Password", |
||||
label: "Retype Password", |
||||
obscureText: true, |
||||
errorMessage: errorRePassword.value, |
||||
), |
||||
const SizedBox(height: 10), |
||||
FxTextField( |
||||
ctrl: useTextEditingController(text: idNo), |
||||
hint: "ID", |
||||
label: "ID", |
||||
isReadOnly: true, |
||||
isEnabled: false, |
||||
suffixText: "Auto Generated", |
||||
), |
||||
const SizedBox(height: 10), |
||||
Row( |
||||
mainAxisAlignment: MainAxisAlignment.end, |
||||
mainAxisSize: MainAxisSize.max, |
||||
children: [ |
||||
ElevatedButton( |
||||
style: ButtonStyle( |
||||
backgroundColor: MaterialStateColor.resolveWith( |
||||
(st) => Colors.green, |
||||
), |
||||
), |
||||
onPressed: () { |
||||
if (!validationError()) { |
||||
ref.read(mitraEditProvider.notifier).edit( |
||||
mitraID: mitraID, |
||||
companyID: company!.mCompanyID, |
||||
mouID: mou.map((e) => e.mMouID).toList(), |
||||
doctorID: doctor!.mDoctorID, |
||||
doctorAddressID: |
||||
doctorAddress!.mDoctorAddressID, |
||||
login: ctrlLogin.text, |
||||
password: ctrlPassword.text, |
||||
query: "", |
||||
); |
||||
Navigator.of(context).pop(); |
||||
} |
||||
}, |
||||
child: const Text("Save"), |
||||
), |
||||
const SizedBox(width: 20), |
||||
ElevatedButton( |
||||
style: ButtonStyle( |
||||
backgroundColor: MaterialStateColor.resolveWith( |
||||
(st) => Colors.red, |
||||
), |
||||
), |
||||
onPressed: () { |
||||
Navigator.of(context).pop(); |
||||
}, |
||||
child: const Text("Cancel"), |
||||
), |
||||
], |
||||
), |
||||
], |
||||
), |
||||
), |
||||
)), |
||||
); |
||||
} |
||||
} |
@ -0,0 +1,87 @@
|
||||
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 mitraEditProvider = |
||||
StateNotifierProvider<MitraEditNotifier, MitraEditState>( |
||||
(ref) => MitraEditNotifier(ref: ref), |
||||
); |
||||
|
||||
class MitraEditNotifier extends StateNotifier<MitraEditState> { |
||||
final Ref ref; |
||||
CancelToken? cancelToken; |
||||
MitraEditNotifier({ |
||||
required this.ref, |
||||
}) : super(MitraEditStateInit()); |
||||
|
||||
void reset() { |
||||
state = MitraEditStateInit(); |
||||
} |
||||
|
||||
void edit({ |
||||
required String mitraID, |
||||
required String companyID, |
||||
required List<String> mouID, |
||||
required String doctorID, |
||||
required String doctorAddressID, |
||||
required String login, |
||||
required String password, |
||||
required String query, |
||||
}) async { |
||||
try { |
||||
state = MitraEditStateLoading(); |
||||
final dio = ref.read(dioProvider); |
||||
final localAuth = ref.read(localAuthProvider); |
||||
if (localAuth?.token == null) { |
||||
throw BaseRepositoryException(message: "Invalid Token"); |
||||
} |
||||
await MitraRepository(dio: dio).edit( |
||||
mitraID: mitraID, |
||||
token: localAuth!.token!, |
||||
mouID: mouID, |
||||
companyID: companyID, |
||||
doctorID: doctorID, |
||||
doctorAddressID: doctorAddressID, |
||||
login: login, |
||||
password: password, |
||||
); |
||||
state = MitraEditStateDone(); |
||||
ref.read(mitraSearchProvider.notifier).search(query: query); |
||||
} catch (e) { |
||||
if (e is BaseRepositoryException) { |
||||
state = MitraEditStateError(message: e.message); |
||||
} else { |
||||
state = MitraEditStateError(message: "Unknown Error "); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
abstract class MitraEditState extends Equatable { |
||||
final DateTime date; |
||||
MitraEditState() : date = DateTime.now(); |
||||
@override |
||||
List<Object?> get props => throw [date]; |
||||
} |
||||
|
||||
class MitraEditStateInit extends MitraEditState {} |
||||
|
||||
class MitraEditStateLoading extends MitraEditState {} |
||||
|
||||
class MitraEditStateError extends MitraEditState { |
||||
final String message; |
||||
|
||||
MitraEditStateError({ |
||||
required this.message, |
||||
}); |
||||
} |
||||
|
||||
class MitraEditStateDone extends MitraEditState { |
||||
MitraEditStateDone(); |
||||
} |
Loading…
Reference in new issue