feat(diary): Moderation send network method

This commit is contained in:
2024-03-20 17:17:46 +03:00
parent 89a9918ea5
commit 3c6c79beeb
2 changed files with 78 additions and 8 deletions

View File

@@ -33,6 +33,8 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
on<ProductInput>(
_inputProduct); //пользователь нажимает "Добавить комментарий"
on<FinishDay>(_finishDay); //пользователь нажимает "Добавить комментарий"
on<SendModeration>(
_sendModeration); //пользователь нажимает "Добавить комментарий"
}
Future<void> _onInitialize(
@@ -605,6 +607,57 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
}
}
FutureOr<void> _sendModeration(
SendModeration event, Emitter<DiaryEditState> 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<http.Response> _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<String, dynamic> 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<void> _addComment(
AddComment event, Emitter<DiaryEditState> emit) async {
if (state is DiaryEditLoadedState) {

View File

@@ -504,19 +504,29 @@ class DiaryEditScreen extends StatelessWidget {
),
],
),
actions: <Widget>[
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<DiaryEditBloc>().add(ChangeSatiety(satiety));