temp build
This commit is contained in:
59
frontend/pshared/lib/provider/recipient/pmethods.dart
Normal file
59
frontend/pshared/lib/provider/recipient/pmethods.dart
Normal file
@@ -0,0 +1,59 @@
|
||||
import 'package:collection/collection.dart';
|
||||
|
||||
import 'package:pshared/data/mapper/payment/method.dart';
|
||||
import 'package:pshared/models/payment/methods/type.dart';
|
||||
import 'package:pshared/provider/organizations.dart';
|
||||
import 'package:pshared/provider/recipient/provider.dart';
|
||||
import 'package:pshared/provider/template.dart';
|
||||
import 'package:pshared/service/recipient/pmethods.dart';
|
||||
|
||||
|
||||
class PaymentMethodsProvider extends GenericProvider<PaymentMethod> {
|
||||
late OrganizationsProvider _organizations;
|
||||
late RecipientsProvider _recipients;
|
||||
|
||||
PaymentMethodsProvider() : super(service: PaymentMethodService.basicService);
|
||||
|
||||
List<PaymentMethod> get methods => List<PaymentMethod>.unmodifiable(items.toList()..sort((a, b) => a.storable.createdAt.compareTo(b.storable.createdAt)));
|
||||
|
||||
void updateProviders(OrganizationsProvider organizations, RecipientsProvider recipients) {
|
||||
_organizations = organizations;
|
||||
_recipients = recipients;
|
||||
if (_organizations.isOrganizationSet && (_recipients.currentObject != null)) {
|
||||
load(_organizations.current.id, _recipients.currentObject!.id);
|
||||
}
|
||||
}
|
||||
|
||||
// void reorderMethods(int oldIndex, int newIndex) {
|
||||
// if (newIndex > oldIndex) newIndex--;
|
||||
// final item = _methods.removeAt(oldIndex);
|
||||
// _methods.insert(newIndex, item);
|
||||
// notifyListeners();
|
||||
// }
|
||||
|
||||
PaymentMethod? get main => methods.firstWhereOrNull((m) => m.isMain);
|
||||
|
||||
Future<void> updateMethod(PaymentMethod method) async => update(method.toDTO().toJson());
|
||||
|
||||
Future<void> setArchivedMethod({
|
||||
required PaymentMethod method,
|
||||
required bool newIsArchived,
|
||||
}) async => setArchived(
|
||||
organizationRef: _organizations.current.id,
|
||||
objectRef: method.id,
|
||||
newIsArchived: newIsArchived,
|
||||
cascade: true,
|
||||
);
|
||||
|
||||
|
||||
Future<void> makeMain(PaymentMethod method) {
|
||||
// TODO: create separate backend method to manage main payment method
|
||||
final updates = <Future<void>>[];
|
||||
final currentMain = main;
|
||||
if (currentMain != null) {
|
||||
updates.add(updateMethod(currentMain.copyWith(isMain: false)));
|
||||
}
|
||||
updates.add(updateMethod(method.copyWith(isMain: true)));
|
||||
return Future.wait(updates).then((_) => null);
|
||||
}
|
||||
}
|
||||
60
frontend/pshared/lib/provider/recipient/provider.dart
Normal file
60
frontend/pshared/lib/provider/recipient/provider.dart
Normal file
@@ -0,0 +1,60 @@
|
||||
|
||||
import 'package:pshared/models/recipient/filter.dart';
|
||||
import 'package:pshared/models/recipient/recipient.dart';
|
||||
import 'package:pshared/models/recipient/status.dart';
|
||||
import 'package:pshared/provider/organizations.dart';
|
||||
import 'package:pshared/provider/template.dart';
|
||||
import 'package:pshared/service/recipient/service.dart';
|
||||
|
||||
|
||||
class RecipientsProvider extends GenericProvider<Recipient> {
|
||||
late OrganizationsProvider _organizations;
|
||||
|
||||
RecipientFilter _selectedFilter = RecipientFilter.all;
|
||||
String _query = '';
|
||||
|
||||
RecipientFilter get selectedFilter => _selectedFilter;
|
||||
String get query => _query;
|
||||
|
||||
List<Recipient> get recipients => List<Recipient>.unmodifiable(items.toList()..sort((a, b) => a.storable.createdAt.compareTo(b.storable.createdAt)));
|
||||
|
||||
RecipientsProvider() : super(service: RecipientService.basicService);
|
||||
|
||||
List<Recipient> get filteredRecipients {
|
||||
List<Recipient> filtered = recipients.where((r) {
|
||||
switch (_selectedFilter) {
|
||||
case RecipientFilter.ready:
|
||||
return r.status == RecipientStatus.ready;
|
||||
case RecipientFilter.registered:
|
||||
return r.status == RecipientStatus.registered;
|
||||
case RecipientFilter.notRegistered:
|
||||
return r.status == RecipientStatus.notRegistered;
|
||||
case RecipientFilter.all:
|
||||
return true;
|
||||
}
|
||||
}).toList();
|
||||
|
||||
if (_query.isNotEmpty) {
|
||||
filtered = filtered.where((r) => r.matchesQuery(_query)).toList();
|
||||
}
|
||||
|
||||
return filtered;
|
||||
}
|
||||
|
||||
void setFilter(RecipientFilter filter) {
|
||||
_selectedFilter = filter;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void setQuery(String query) {
|
||||
_query = query.trim().toLowerCase();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void updateProviders(OrganizationsProvider organizations) {
|
||||
_organizations = organizations;
|
||||
if (_organizations.isOrganizationSet) {
|
||||
load(_organizations.current.id, _organizations.current.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,21 +32,23 @@ List<T> mergeLists<T>({
|
||||
/// to manage state (loading, error, data) without re‑implementing service logic.
|
||||
class GenericProvider<T extends PermissionBoundStorable> extends ChangeNotifier {
|
||||
final BasicService<T> service;
|
||||
bool _isLoaded = false;
|
||||
|
||||
Resource<List<T>> _resource = Resource(data: []);
|
||||
Resource<List<T>> get resource => _resource;
|
||||
|
||||
List<T> get items => List.unmodifiable(_resource.data ?? []);
|
||||
bool get isLoading => _resource.isLoading;
|
||||
bool get isEmpty => items.isEmpty;
|
||||
Object? get error => _resource.error;
|
||||
bool get isReady => (error == null) && _isLoaded;
|
||||
|
||||
bool get isCurrentSet => _currentObjectRef != null;
|
||||
String? _currentObjectRef; // Stores the currently selected project ref
|
||||
T? get currentObject => _resource.data?.firstWhereOrNull(
|
||||
(object) => object.id == _currentObjectRef,
|
||||
);
|
||||
|
||||
T? getItemById(String id) => items.firstWhereOrNull((item) => item.id == id);
|
||||
T? getItemByRef(String id) => items.firstWhereOrNull((item) => item.id == id);
|
||||
|
||||
GenericProvider({required this.service});
|
||||
|
||||
@@ -67,11 +69,13 @@ class GenericProvider<T extends PermissionBoundStorable> extends ChangeNotifier
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> loadFuture(Future<List<T>> future) async {
|
||||
Future<List<T>> loadFuture(Future<List<T>> future) async {
|
||||
_setResource(_resource.copyWith(isLoading: true));
|
||||
try {
|
||||
final list = await future;
|
||||
_isLoaded = true;
|
||||
_setResource(Resource(data: list, isLoading: false));
|
||||
return list;
|
||||
} catch (e) {
|
||||
_setResource(
|
||||
_resource.copyWith(isLoading: false, error: toException(e)),
|
||||
@@ -80,17 +84,30 @@ class GenericProvider<T extends PermissionBoundStorable> extends ChangeNotifier
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> load(String organizationRef, String? parentRef) async {
|
||||
Future<void> load(
|
||||
String organizationRef,
|
||||
String? parentRef, {
|
||||
int? limit,
|
||||
int? offset,
|
||||
bool? Function()? fetchArchived,
|
||||
}) async {
|
||||
if (parentRef != null) {
|
||||
return loadFuture(service.list(organizationRef, parentRef));
|
||||
await loadFuture(
|
||||
service.list(
|
||||
organizationRef,
|
||||
parentRef,
|
||||
limit: limit,
|
||||
offset: offset,
|
||||
fetchArchived: fetchArchived == null ? null : fetchArchived(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadItem(String itemRef) async {
|
||||
return loadFuture((() async => [await service.get(itemRef)])());
|
||||
await loadFuture((() async => [await service.get(itemRef)])());
|
||||
}
|
||||
|
||||
|
||||
List<T> merge(List<T> rhs) => mergeLists<T>(
|
||||
lhs: items,
|
||||
rhs: rhs,
|
||||
@@ -134,11 +151,47 @@ class GenericProvider<T extends PermissionBoundStorable> extends ChangeNotifier
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> delete(String objectRef) async {
|
||||
Future<void> delete(String objectRef, {Map<String, dynamic>? request}) async {
|
||||
_setResource(_resource.copyWith(isLoading: true));
|
||||
|
||||
try {
|
||||
await service.delete(objectRef);
|
||||
await service.delete(objectRef, request: request);
|
||||
if (_currentObjectRef == objectRef) {
|
||||
_currentObjectRef = null;
|
||||
}
|
||||
|
||||
_setResource(Resource(
|
||||
data: _resource.data?.where((p) => p.id != objectRef).toList(),
|
||||
isLoading: false,
|
||||
));
|
||||
} catch (e) {
|
||||
_setResource(Resource(data: _resource.data, isLoading: false, error: toException(e)));
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> toggleArchived(T item, bool currentState, {bool? cascade}) => setArchived(
|
||||
organizationRef: item.organizationRef,
|
||||
objectRef: item.id,
|
||||
newIsArchived: !currentState,
|
||||
cascade: cascade ?? true,
|
||||
);
|
||||
|
||||
Future<void> setArchived({
|
||||
required String organizationRef,
|
||||
required String objectRef,
|
||||
required bool newIsArchived,
|
||||
bool? cascade,
|
||||
}) async {
|
||||
_setResource(_resource.copyWith(isLoading: true));
|
||||
|
||||
try {
|
||||
await service.archive(
|
||||
organizationRef: organizationRef,
|
||||
objectRef: objectRef,
|
||||
newIsArchived: newIsArchived,
|
||||
cascade: cascade,
|
||||
);
|
||||
if (_currentObjectRef == objectRef) {
|
||||
_currentObjectRef = null;
|
||||
}
|
||||
@@ -154,11 +207,17 @@ class GenericProvider<T extends PermissionBoundStorable> extends ChangeNotifier
|
||||
}
|
||||
|
||||
bool setCurrentObject(String? objectRef) {
|
||||
if (_currentObjectRef == objectRef) {
|
||||
// No change, skip notification
|
||||
return true;
|
||||
}
|
||||
|
||||
if (objectRef == null) {
|
||||
_currentObjectRef = null;
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_resource.data?.any((p) => p.id == objectRef) ?? false) {
|
||||
_currentObjectRef = objectRef;
|
||||
notifyListeners();
|
||||
@@ -167,4 +226,5 @@ class GenericProvider<T extends PermissionBoundStorable> extends ChangeNotifier
|
||||
|
||||
return false; // Object not found
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user