You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.0 KiB
80 lines
2.0 KiB
2 years ago
|
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';
|
||
|
|
||
|
final mitraAddProvider = StateNotifierProvider<MitraAddNotifier, MitraAddState>(
|
||
|
(ref) => MitraAddNotifier(ref: ref),
|
||
|
);
|
||
|
|
||
|
class MitraAddNotifier extends StateNotifier<MitraAddState> {
|
||
|
final Ref ref;
|
||
|
CancelToken? cancelToken;
|
||
|
MitraAddNotifier({
|
||
|
required this.ref,
|
||
|
}) : super(MitraAddStateInit());
|
||
|
|
||
|
void reset() {
|
||
|
state = MitraAddStateInit();
|
||
|
}
|
||
|
|
||
|
void add({
|
||
|
required String companyID,
|
||
|
required List<String> mouID,
|
||
|
required String doctorID,
|
||
|
required String doctorAddressID,
|
||
|
required String login,
|
||
|
}) 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,
|
||
|
);
|
||
|
state = MitraAddStateDone();
|
||
|
} 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<Object?> 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();
|
||
|
}
|