Skip to content

Commit

Permalink
finish reporting feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dasmikko committed Feb 24, 2023
1 parent e276c67 commit 00b9c69
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
19 changes: 17 additions & 2 deletions lib/dialogs/reportPostDialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,24 @@ class _ReportPostDialogState extends State<ReportPostDialog> {
),
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')),
],
);
}
Expand Down
12 changes: 12 additions & 0 deletions lib/helpers/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,16 @@ class KnockoutAPI {
throw e;
}
}

Future<void> 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;
}
}
}
35 changes: 26 additions & 9 deletions lib/widgets/post/toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 00b9c69

Please sign in to comment.