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 mitraAddProvider = StateNotifierProvider( (ref) => MitraAddNotifier(ref: ref), ); class MitraAddNotifier extends StateNotifier { final Ref ref; CancelToken? cancelToken; MitraAddNotifier({ required this.ref, }) : super(MitraAddStateInit()); void reset() { state = MitraAddStateInit(); } void add({ required String companyID, required List mouID, required String doctorID, required String doctorAddressID, required String login, required String password, required String query, }) async { try { state = MitraAddStateLoading(); final dio = ref.read(dioProvider); final localAuth = ref.read(localAuthProvider); if (localAuth?.token == null) { throw BaseRepositoryException(message: "Invalid Token"); } await MitraRepository(dio: dio).add( token: localAuth!.token!, mouID: mouID, companyID: companyID, doctorID: doctorID, doctorAddressID: doctorAddressID, login: login, password: password, ); state = MitraAddStateDone(); ref.read(mitraSearchProvider.notifier).search(query: query); } catch (e) { if (e is BaseRepositoryException) { state = MitraAddStateError(message: e.message); } else { state = MitraAddStateError(message: "Unknown Error "); } } } } abstract class MitraAddState extends Equatable { final DateTime date; MitraAddState() : date = DateTime.now(); @override List get props => throw [date]; } class MitraAddStateInit extends MitraAddState {} class MitraAddStateLoading extends MitraAddState {} class MitraAddStateError extends MitraAddState { final String message; MitraAddStateError({ required this.message, }); } class MitraAddStateDone extends MitraAddState { MitraAddStateDone(); }