fix(diary): Remove portion API call fixed

This commit is contained in:
2024-07-19 15:51:39 +03:00
parent be461e4183
commit 7535e82422
10 changed files with 217 additions and 195 deletions

View File

@@ -1,6 +1,7 @@
import 'dart:async';
import 'package:eatvio_app/features/diary/models/diary_model.dart';
import 'package:eatvio_app/features/diary/models/meal.dart';
import 'package:eatvio_app/features/diary/models/portion.dart';
import 'package:eatvio_app/features/diary/models/product.dart';
import 'package:eatvio_app/features/diary/repositories/diary_repository.dart';
import 'package:intl/intl.dart';
@@ -82,7 +83,7 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
selectedMood: currentMeal.mood,
selectedSatiety: currentMeal.satiety,
commentController: TextEditingController(),
productNameController: TextEditingController(),
product_nameController: TextEditingController(),
productQuantityController: TextEditingController(),
caloriesController: TextEditingController(),
fatsController: TextEditingController(),
@@ -259,15 +260,14 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
Emitter<DiaryState> emit,
) async {
if (state is DiaryStateLoaded) {
//var currentState = state as DiaryStateLoaded;
var currentState = state as DiaryStateLoaded;
print(await diaryRepository.inputProduct(event.input));
final Map<String, List<Product>> productNutritionalValues =
await diaryRepository.inputProduct(event.input);
// Create a new controller with the input text and update the autocomplete list
/*var updatedState = currentState.copyWith(
productNameController: TextEditingController(text: event.input),
autocompleteList: productNutritionalValues.map((product) => product['name']).toList(),
var updatedState = currentState.copyWith(
product_nameController: TextEditingController(text: event.input),
autocompleteList:
productNutritionalValues['product_nutritional_value'] ?? [],
);
if (event.input.length > 3) {
updatedState = updatedState.copyWith(moderationExpanded: true);
@@ -275,7 +275,7 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
updatedState = updatedState.copyWith(moderationExpanded: false);
}
emit(updatedState); */
emit(updatedState);
}
}
@@ -331,16 +331,25 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
) async {
if (state is DiaryStateLoaded) {
var currentState = state as DiaryStateLoaded;
print(currentState.diary.id);
print(currentState.currentMeal.id);
print(currentState.product_nameController.text);
print(currentState.portionController.title);
print(currentState.productQuantityController.text);
await diaryRepository.addPortion(
Map<String, Portion> data = await diaryRepository.addPortion(
currentState.diary.id,
currentState.currentMeal.id,
currentState.productNameController.text,
currentState.portionController.value,
currentState.product_nameController.text,
currentState.portionController.title,
int.parse(currentState.productQuantityController.text),
);
List<Portion> newPortions = currentState.portions;
emit(currentState.copyWith(isAdded: true));
final Portion newPortion = data['portion'] ??
new Portion(id: 1, portion: 'gr', product_name: '1', quantity: 1);
newPortions.add(newPortion);
emit(currentState.copyWith(isAdded: true, portions: newPortions));
}
}
@@ -350,45 +359,14 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
) async {
if (state is DiaryStateLoaded) {
var currentState = state as DiaryStateLoaded;
int? mealId =
currentState.currentMeal.id; // Rename id to mealId for clarity
var response = await sendRemovePortion(
event.portionId,
mealId,
await diaryRepository.removePortion(
currentState.diary.id,
currentState.currentMeal.id,
event.portionId,
);
if (response.statusCode == 204) {
await reloadScreen(event, emit);
} else {
print(
'Failed to add portion. Status Code: ${response.statusCode} ${response.body}',
);
}
}
}
Future<http.Response> sendRemovePortion(
int? portionID,
int? mealID,
int diaryID,
) async {
String url =
'${dotenv.env['SERVER_ROOT']!}/diary/$diaryID/meal/$mealID/portions/$portionID/delete';
var response = await http.delete(
Uri.parse(url),
headers: {
'Content-Type': 'application/json',
},
encoding: utf8,
);
return response;
}
FutureOr<void> _removeImage(
RemoveImageEvent event,
Emitter<DiaryState> emit,