Skip to content

Commit

Permalink
Utilize network status to gray out create button
Browse files Browse the repository at this point in the history
  • Loading branch information
dbadrian committed Jan 3, 2025
1 parent 582ff5d commit b8f224c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
4 changes: 4 additions & 0 deletions frontend/lib/api/api_status_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class ApiStatus extends _$ApiStatus {
Future<void> _updateStatus() async {
state = await AsyncValue.guard(() => _checkBackendStatus());
}

Future<void> updateStatus(bool isOnline) async {
state = AsyncValue.data(isOnline);
}
}

// @riverpod
Expand Down
8 changes: 7 additions & 1 deletion frontend/lib/recipes/controller/edit_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import 'package:go_router/go_router.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:sqflite/sqflite.dart';
import 'package:zest/api/api_service.dart';
import 'package:zest/api/api_status_provider.dart';
import 'package:zest/main.dart';
import 'package:zest/recipes/controller/draft_controller.dart';
import 'package:zest/recipes/screens/recipe_search.dart';
import 'package:zest/settings/settings_provider.dart';
import 'package:zest/utils/utils.dart';

Expand Down Expand Up @@ -217,7 +219,11 @@ class RecipeEditController extends _$RecipeEditController {
// TODO: onconfirm
);
} else if (categories.error is ServerNotReachableException) {
openServerNotAvailableDialog();
openServerNotAvailableDialog(onPressed: () {
ref.read(apiStatusProvider.notifier).updateStatus(false);
shellNavigatorKey.currentState!.overlay!.context
.goNamed(RecipeSearchPage.routeName);
});
}
}
// express the list as a map
Expand Down
10 changes: 6 additions & 4 deletions frontend/lib/ui/main_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class MainScaffold extends ConsumerWidget {
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.primary,
title: backendStatus.value ?? false
? const Text("Online")
? Container()
: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expand All @@ -71,9 +71,11 @@ class MainScaffold extends ConsumerWidget {
IconButton(
icon: const Icon(
key: Key("appbar_addrecipe_icon"), Icons.add_card_rounded),
onPressed: () {
context.goNamed(RecipeEditPage.routeNameCreate);
},
onPressed: (backendStatus.value ?? false)
? () {
context.goNamed(RecipeEditPage.routeNameCreate);
}
: null,
),
if (isAuthenticated)
IconButton(
Expand Down
2 changes: 1 addition & 1 deletion frontend/lib/utils/networking.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class ResourceNotFoundInterceptor extends InterceptorContract {
case 400:
throw BadRequestException();
case 500:
throw Exception("Server error");
throw ServerNotReachableException(); // "Internal Server Error [500]"
}
return response;
}
Expand Down

0 comments on commit b8f224c

Please sign in to comment.