From 3c6c79beeb6199e9282ef261e5a057af9015e2da Mon Sep 17 00:00:00 2001 From: maxxkim Date: Wed, 20 Mar 2024 17:17:46 +0300 Subject: [PATCH] feat(diary): Moderation send network method --- .../bloc/diary_edit_bloc.dart | 53 +++++++++++++++++++ .../diary_edit_screen/diary_edit_screen.dart | 33 +++++++++--- 2 files changed, 78 insertions(+), 8 deletions(-) diff --git a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart index 03366095..4f1bcd1d 100644 --- a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart +++ b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart @@ -33,6 +33,8 @@ class DiaryEditBloc extends Bloc { on( _inputProduct); //пользователь нажимает "Добавить комментарий" on(_finishDay); //пользователь нажимает "Добавить комментарий" + on( + _sendModeration); //пользователь нажимает "Добавить комментарий" } Future _onInitialize( @@ -605,6 +607,57 @@ class DiaryEditBloc extends Bloc { } } + FutureOr _sendModeration( + SendModeration event, Emitter emit) async { + if (state is DiaryEditLoadedState) { + var currentState = state as DiaryEditLoadedState; + + final response = await _sendModerationData( + event.name, + event.energyOn100gr, + event.proteinsOn100gr, + event.fatsOn100gr, + event.carbohydratesOn100gr, + currentState.token, + ); + + print(response); + } + } + + Future _sendModerationData( + String name, + String energy_on_100gr, + String proteins_on_100gr, + String fats_on_100gr, + String carbohydrates_on_100gr, + String? token) async { + String url = + '${dotenv.env['SERVER_ROOT']!}/diary/product_nutritional_values/sendToModeration'; + final Map credentials = { + 'name': name, + 'energy_on_100gr': energy_on_100gr, + 'proteins_on_100gr': proteins_on_100gr, + 'fats_on_100gr': fats_on_100gr, + 'carbohydrates_on_100gr': carbohydrates_on_100gr, + }; + var response = await http.post( + Uri.parse(url), + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer $token', + }, + body: json.encode(credentials), + encoding: utf8, + ); + if (response.statusCode == 200) { + return response; + } else { + throw Exception( + 'Failed to upload image. Status code: ${response.statusCode}'); + } + } + FutureOr _addComment( AddComment event, Emitter emit) async { if (state is DiaryEditLoadedState) { diff --git a/lib/presentation/diary/diary_edit_screen/diary_edit_screen.dart b/lib/presentation/diary/diary_edit_screen/diary_edit_screen.dart index 2c50efb9..7b9503cb 100644 --- a/lib/presentation/diary/diary_edit_screen/diary_edit_screen.dart +++ b/lib/presentation/diary/diary_edit_screen/diary_edit_screen.dart @@ -504,19 +504,29 @@ class DiaryEditScreen extends StatelessWidget { ), ], ), + actions: [ + TextButton( + onPressed: () => popDialog, + child: Text('Отмена'), + ), + TextButton( + onPressed: () => triggerModeration( + context, + name, + energyOn100gr, + proteinsOn100gr, + fatsOn100gr, + carbohydratesOn100gr, + dialogContext), + child: Text('Отправить'), + ), + ], ); }, ); } - popDialog(BuildContext dialogContext) { - return () { - Navigator.of(dialogContext) - .pop(); // Dismiss the dialog on Cancel button press - }; - } - - sendToModeration( + Function triggerModeration( BuildContext context, String name, String energyOn100gr, @@ -536,6 +546,13 @@ class DiaryEditScreen extends StatelessWidget { }; } + popDialog(BuildContext dialogContext) { + return () { + Navigator.of(dialogContext) + .pop(); // Dismiss the dialog on Cancel button press + }; + } + changeSatiety(BuildContext context, int satiety) { return () { context.read().add(ChangeSatiety(satiety));