Frontend first draft
This commit is contained in:
80
frontend/pshared/lib/provider/organizations.dart
Normal file
80
frontend/pshared/lib/provider/organizations.dart
Normal file
@@ -0,0 +1,80 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
|
||||
import 'package:pshared/config/constants.dart';
|
||||
import 'package:pshared/models/organization/organization.dart';
|
||||
import 'package:pshared/provider/resource.dart';
|
||||
import 'package:pshared/provider/exception.dart';
|
||||
import 'package:pshared/service/organization.dart';
|
||||
import 'package:pshared/service/secure_storage.dart';
|
||||
|
||||
|
||||
class OrganizationsProvider extends ChangeNotifier {
|
||||
Resource<List<Organization>> _resource = Resource(data: []);
|
||||
Resource<List<Organization>> get resource => _resource;
|
||||
|
||||
List<Organization> get organizations => _resource.data ?? [];
|
||||
String? _currentOrg;
|
||||
|
||||
Organization get current => isOrganizationSet ? _current! : throw StateError('Organization is not set');
|
||||
|
||||
Organization? _org(String? orgRef) => organizations.firstWhereOrNull((org) => org.id == orgRef);
|
||||
Organization? get _current => _org(_currentOrg);
|
||||
|
||||
bool get isOrganizationSet => _current != null;
|
||||
bool get isLoading => _resource.isLoading;
|
||||
Object? get error => _resource.error;
|
||||
|
||||
void _setResource(Resource<List<Organization>> newResource) {
|
||||
_resource = newResource;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<List<Organization>> load() async {
|
||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||
try {
|
||||
final orgs = await OrganizationService.list();
|
||||
// fetch stored org
|
||||
String? org = await SecureStorageService.get(Constants.currentOrgKey);
|
||||
// check stored org availability
|
||||
org = orgs.firstWhereOrNull((o) => o.id == org)?.id;
|
||||
// fallback if org is not set or not available
|
||||
org ??= orgs.first.id;
|
||||
await setCurrentOrganization(org);
|
||||
_setResource(Resource(data: orgs, isLoading: false));
|
||||
return orgs;
|
||||
} catch (e) {
|
||||
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Organization> loadByInvitation(String invitationRef) async {
|
||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||
try {
|
||||
final org = await OrganizationService.loadByInvitation(invitationRef);
|
||||
await setCurrentOrganization(org.id);
|
||||
_setResource(Resource(data: [org], isLoading: false));
|
||||
return org;
|
||||
} catch (e) {
|
||||
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
bool _setCurrentOrganization(String? orgRef) {
|
||||
final organizationRef = _org(orgRef)?.id;
|
||||
if (organizationRef == null) return false;
|
||||
|
||||
_currentOrg = organizationRef;
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<bool> setCurrentOrganization(String? orgRef) async {
|
||||
if (!_setCurrentOrganization(orgRef)) return false;
|
||||
await SecureStorageService.set(Constants.currentOrgKey, orgRef);
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user