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( (ref) => MitraEditNotifier(ref: ref), ); class MitraEditNotifier extends StateNotifier { 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 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 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(); }