forked from eatvio/eatvio_app
refactor(diary): Refactor product input
This commit is contained in:
@@ -228,11 +228,34 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
selectedMood: meal.mood,
|
||||
selectedSatiety: meal.satiety,
|
||||
);
|
||||
print(updatedState.currentMeal.id);
|
||||
emit(updatedState);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _inputProduct(
|
||||
ProductInput event,
|
||||
Emitter<DiaryState> emit,
|
||||
) async {
|
||||
if (state is DiaryStateLoaded) {
|
||||
var currentState = state as DiaryStateLoaded;
|
||||
|
||||
final 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(),
|
||||
);
|
||||
if (event.input.length > 3) {
|
||||
updatedState = updatedState.copyWith(moderationExpanded: true);
|
||||
} else {
|
||||
updatedState = updatedState.copyWith(moderationExpanded: false);
|
||||
}
|
||||
|
||||
emit(updatedState); // Emit the updated state
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _clearInput(
|
||||
ClearInput event,
|
||||
Emitter<DiaryState> emit,
|
||||
@@ -332,44 +355,6 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
return response;
|
||||
}
|
||||
|
||||
Future<void> _inputProduct(
|
||||
ProductInput event,
|
||||
Emitter<DiaryState> emit,
|
||||
) async {
|
||||
if (state is DiaryStateLoaded) {
|
||||
var currentState = state as DiaryStateLoaded;
|
||||
|
||||
final Map<String, dynamic> credentials = {'name': event.input};
|
||||
final uri = Uri.parse(
|
||||
'${dotenv.env['SERVER_ROOT']!}/diary/product_nutritional_values/filter?${Uri(queryParameters: credentials).query}',
|
||||
);
|
||||
|
||||
final response = await http.get(
|
||||
uri,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
);
|
||||
|
||||
final productNutritionalValues = jsonDecode(response.body)['product_nutritional_value'];
|
||||
|
||||
if (productNutritionalValues is List<dynamic>) {
|
||||
// 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(),
|
||||
);
|
||||
if (event.input.length > 3) {
|
||||
updatedState = updatedState.copyWith(moderationExpanded: true);
|
||||
} else {
|
||||
updatedState = updatedState.copyWith(moderationExpanded: false);
|
||||
}
|
||||
|
||||
emit(updatedState); // Emit the updated state
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _removePortion(
|
||||
RemovePortionEvent event,
|
||||
Emitter<DiaryState> emit,
|
||||
|
||||
Reference in New Issue
Block a user