From 00b9c693260a58e658d7afcdac158c3c895b5c53 Mon Sep 17 00:00:00 2001 From: dasmikko Date: Fri, 24 Feb 2023 17:11:45 +0100 Subject: [PATCH] finish reporting feature --- lib/dialogs/reportPostDialog.dart | 19 +++++++++++++++-- lib/helpers/api.dart | 12 +++++++++++ lib/widgets/post/toolbar.dart | 35 +++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/lib/dialogs/reportPostDialog.dart b/lib/dialogs/reportPostDialog.dart index 9d110f8..172d394 100644 --- a/lib/dialogs/reportPostDialog.dart +++ b/lib/dialogs/reportPostDialog.dart @@ -102,9 +102,24 @@ class _ReportPostDialogState extends State { ), actions: [ TextButton( - onPressed: () => Get.back(result: false), child: Text('Cancel')), + onPressed: () => Get.back(result: null), child: Text('Cancel')), ElevatedButton( - onPressed: () => Get.back(result: true), child: Text('Submit')), + onPressed: () { + String reportReason = ''; + KnockoutRule rule = rules! + .where((element) => element.title == selectedRule) + .first; + + switch (rule.category) { + case 'Site Wide Rules': + reportReason += 'Site Wide Rule'; + } + + reportReason += + ' ${rule.id.toString()}: ${rule.title} - ${textEditingController.text}'; + Get.back(result: reportReason); + }, + child: Text('Submit')), ], ); } diff --git a/lib/helpers/api.dart b/lib/helpers/api.dart index 06b7857..9be8933 100644 --- a/lib/helpers/api.dart +++ b/lib/helpers/api.dart @@ -488,4 +488,16 @@ class KnockoutAPI { throw e; } } + + Future createReport(int postId, String reportReason) async { + try { + final response = await _request(url: 'reports', type: 'post', data: { + 'postId': postId, + 'reportReason': reportReason, + }); + } on DioError catch (e) { + onDioErrorHandler(e); + throw e; + } + } } diff --git a/lib/widgets/post/toolbar.dart b/lib/widgets/post/toolbar.dart index 8e6b2a4..5429225 100644 --- a/lib/widgets/post/toolbar.dart +++ b/lib/widgets/post/toolbar.dart @@ -4,6 +4,8 @@ import 'package:get/get.dart'; import 'package:knocky/controllers/authController.dart'; import 'package:knocky/controllers/threadController.dart'; import 'package:knocky/dialogs/reportPostDialog.dart'; +import 'package:knocky/helpers/api.dart'; +import 'package:knocky/helpers/snackbar.dart'; import 'package:timeago/timeago.dart' as timeago; import 'package:knocky/models/thread.dart'; @@ -67,15 +69,30 @@ class Toolbar extends StatelessWidget { child: Row( mainAxisAlignment: MainAxisAlignment.end, children: [ - IconButton( - iconSize: 12, - icon: FaIcon( - FontAwesomeIcons.solidFlag, - ), - onPressed: () { - Get.dialog(ReportPostDialog()); - }, - ), + (authController.isAuthenticated.value) + ? IconButton( + iconSize: 12, + icon: FaIcon( + FontAwesomeIcons.solidFlag, + ), + onPressed: () async { + String? reportReason = await Get.dialog(ReportPostDialog()); + + if (reportReason != null) { + print(reportReason); + SnackbarController snackbar = KnockySnackbar.normal( + 'Report post', 'Sending report...', + isDismissible: false, showProgressIndicator: true); + await KnockoutAPI().createReport(post!.id!, reportReason); + snackbar.close(); + KnockySnackbar.success( + 'Post was reported!', + title: 'Report post', + ).show(); + } + }, + ) + : Container(), (authController.isAuthenticated.value && post!.user!.id == authController.userId.value) ? IconButton(