Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Force switch household on request forbidden #591

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ class HouseholdUpdateCubit
}

Future<void> refresh() async {
Future<Household?> fHousehold =
ApiService.getInstance().getHousehold(this.household);
Future<Household?> fHousehold = ApiService.getInstance()
.getHousehold(this.household)
.then((res) => res.household);

Future<List<ShoppingList>?> shoppingLists =
ApiService.getInstance().getShoppingLists(this.household);
Expand Down
6 changes: 5 additions & 1 deletion kitchenowl/lib/cubits/household_member_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:kitchenowl/models/household.dart';
import 'package:kitchenowl/models/member.dart';
import 'package:kitchenowl/services/api/api_service.dart';
import 'package:kitchenowl/services/transaction_handler.dart';
import 'package:kitchenowl/services/transactions/household.dart';

class HouseholdMemberCubit extends Cubit<HouseholdMemberState> {
final Household household;
Expand All @@ -16,7 +18,9 @@ class HouseholdMemberCubit extends Cubit<HouseholdMemberState> {

Future<void> refresh() async {
Future<Household?> fHousehold =
ApiService.getInstance().getHousehold(this.household);
TransactionHandler.getInstance().runTransaction(
TransactionHouseholdGet(household: this.household),
);

Household household = await fHousehold ?? this.household;

Expand Down
11 changes: 8 additions & 3 deletions kitchenowl/lib/services/api/household.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ extension HouseholdApi on ApiService {
return body.map((e) => Household.fromJson(e)).toList();
}

Future<Household?> getHousehold(Household household) async {
Future<({Household? household, int statusCode})> getHousehold(
Household household) async {
final res = await get("$baseRoute/${household.id}");
if (res.statusCode != 200) return null;
if (res.statusCode != 200)
return (household: null, statusCode: res.statusCode);

return Household.fromJson(jsonDecode(res.body));
return (
household: Household.fromJson(jsonDecode(res.body)),
statusCode: 200,
);
}

Future<bool> updateHousehold(Household household) async {
Expand Down
18 changes: 12 additions & 6 deletions kitchenowl/lib/services/transactions/household.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,25 @@ class TransactionHouseholdGet extends Transaction<Household?> {

@override
Future<Household?> runOnline() async {
final household =
await ApiService.getInstance().getHousehold(this.household);
if (household != null) {
final res = await ApiService.getInstance().getHousehold(this.household);
if (res.household != null) {
final households = await MemStorage.getInstance().readHouseholds() ?? [];
final i = households.indexWhere((e) => e.id == household.id);
if (i >= 0) {
households[i] = household;
households[i] = res.household!;
} else {
households.add(household);
households.add(res.household!);
}
MemStorage.getInstance().writeHouseholds(households);
} else if (res.statusCode == 403) {
final households = await MemStorage.getInstance().readHouseholds() ?? [];
final i = households.indexWhere((e) => e.id == household.id);
if (i >= 0) {
households.removeAt(i);
MemStorage.getInstance().writeHouseholds(households);
}
}

return household;
return res.household;
}
}
Loading