Current org now sets after list gets to the state of the provider
This commit is contained in:
@@ -20,7 +20,6 @@ class OrganizationsProvider extends ChangeNotifier {
|
|||||||
Organization get current => isOrganizationSet ? _current! : throw StateError('Organization is not set');
|
Organization get current => isOrganizationSet ? _current! : throw StateError('Organization is not set');
|
||||||
|
|
||||||
Organization? _org(String? orgRef) => organizations.firstWhereOrNull((org) => org.id == orgRef);
|
Organization? _org(String? orgRef) => organizations.firstWhereOrNull((org) => org.id == orgRef);
|
||||||
Organization? _orgInList(String? orgRef, List<Organization> list) => list.firstWhereOrNull((org) => org.id == orgRef);
|
|
||||||
Organization? get _current => _org(_currentOrg);
|
Organization? get _current => _org(_currentOrg);
|
||||||
|
|
||||||
bool get isOrganizationSet => _current != null;
|
bool get isOrganizationSet => _current != null;
|
||||||
@@ -36,13 +35,15 @@ class OrganizationsProvider extends ChangeNotifier {
|
|||||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||||
try {
|
try {
|
||||||
final orgs = await OrganizationService.list();
|
final orgs = await OrganizationService.list();
|
||||||
|
// keep fetched organizations so _org can resolve against them
|
||||||
|
_setResource(Resource(data: orgs, isLoading: true));
|
||||||
// fetch stored org
|
// fetch stored org
|
||||||
String? org = await SecureStorageService.get(Constants.currentOrgKey);
|
String? org = await SecureStorageService.get(Constants.currentOrgKey);
|
||||||
// check stored org availability
|
// check stored org availability
|
||||||
org = orgs.firstWhereOrNull((o) => o.id == org)?.id;
|
org = orgs.firstWhereOrNull((o) => o.id == org)?.id;
|
||||||
// fallback if org is not set or not available
|
// fallback if org is not set or not available
|
||||||
org ??= orgs.first.id;
|
org ??= orgs.first.id;
|
||||||
await setCurrentOrganization(org, availableOrganizations: orgs);
|
await setCurrentOrganization(org);
|
||||||
_setResource(Resource(data: orgs, isLoading: false));
|
_setResource(Resource(data: orgs, isLoading: false));
|
||||||
return orgs;
|
return orgs;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -55,7 +56,8 @@ class OrganizationsProvider extends ChangeNotifier {
|
|||||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||||
try {
|
try {
|
||||||
final org = await OrganizationService.loadByInvitation(invitationRef);
|
final org = await OrganizationService.loadByInvitation(invitationRef);
|
||||||
await setCurrentOrganization(org.id, availableOrganizations: [org]);
|
_setResource(Resource(data: [org], isLoading: true));
|
||||||
|
await setCurrentOrganization(org.id);
|
||||||
_setResource(Resource(data: [org], isLoading: false));
|
_setResource(Resource(data: [org], isLoading: false));
|
||||||
return org;
|
return org;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -64,23 +66,17 @@ class OrganizationsProvider extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _setCurrentOrganization(
|
bool _setCurrentOrganization(String? orgRef) {
|
||||||
String? orgRef, {
|
final organizationRef = _org(orgRef)?.id;
|
||||||
List<Organization>? availableOrganizations,
|
|
||||||
}) {
|
|
||||||
final organizationRef = _orgInList(orgRef, availableOrganizations ?? organizations)?.id;
|
|
||||||
if (organizationRef == null) return false;
|
if (organizationRef == null) return false;
|
||||||
|
|
||||||
_currentOrg = organizationRef;
|
_currentOrg = organizationRef;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> setCurrentOrganization(
|
Future<bool> setCurrentOrganization(String? orgRef) async {
|
||||||
String? orgRef, {
|
if (!_setCurrentOrganization(orgRef)) return false;
|
||||||
List<Organization>? availableOrganizations,
|
await SecureStorageService.set(Constants.currentOrgKey, orgRef);
|
||||||
}) async {
|
|
||||||
if (!_setCurrentOrganization(orgRef, availableOrganizations: availableOrganizations)) return false;
|
|
||||||
await SecureStorageService.set(Constants.currentOrgKey, _currentOrg);
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user