feat(inventory): add inventory preview functionality and related models
This commit is contained in:
@@ -4,6 +4,7 @@ import '../../../core/api/api_providers.dart';
|
||||
import '../../../core/api/guarded_api_call.dart';
|
||||
import '../../../features/auth/data/auth_providers.dart';
|
||||
import '../domain/recipe.dart';
|
||||
import '../domain/inventory_preview.dart';
|
||||
import 'recipe_repository.dart';
|
||||
|
||||
final recipeRepositoryProvider = Provider<RecipeRepository>((ref) {
|
||||
@@ -26,3 +27,14 @@ final recipeDetailProvider =
|
||||
() => ref.read(recipeRepositoryProvider).fetchRecipeDetail(id, token: token),
|
||||
);
|
||||
});
|
||||
|
||||
final inventoryPreviewProvider =
|
||||
FutureProvider.family<InventoryPreview, int>((ref, id) async {
|
||||
final token = await ref.watch(authStateProvider.future);
|
||||
return guardedApiCall(
|
||||
ref,
|
||||
() => ref
|
||||
.read(recipeRepositoryProvider)
|
||||
.fetchInventoryPreview(id, token: token),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -3,6 +3,7 @@ import '../../../core/api/api_exception.dart';
|
||||
import '../../../core/api/api_paths.dart';
|
||||
import '../domain/parsed_recipe.dart';
|
||||
import '../domain/recipe.dart';
|
||||
import '../domain/inventory_preview.dart';
|
||||
|
||||
class RecipeRepository {
|
||||
final ApiClient _api;
|
||||
@@ -93,6 +94,27 @@ class RecipeRepository {
|
||||
}
|
||||
}
|
||||
|
||||
Future<InventoryPreview> fetchInventoryPreview(int id,
|
||||
{String? token}) async {
|
||||
try {
|
||||
final data = await _api.getJson(
|
||||
RecipeApiPaths.inventoryPreview(id),
|
||||
token: token,
|
||||
);
|
||||
if (data is! Map<String, dynamic>) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
|
||||
}
|
||||
return InventoryPreview.fromJson(data);
|
||||
} on ApiException {
|
||||
rethrow;
|
||||
} catch (_) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.network,
|
||||
message: 'Kunde inte hämta inventariestatus.');
|
||||
}
|
||||
}
|
||||
|
||||
Future<ParsedRecipe> parseMarkdown(String markdown,
|
||||
{String? token}) async {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user