Skip to content

Commit

Permalink
add api call for rules, add class for knockout rules
Browse files Browse the repository at this point in the history
  • Loading branch information
dasmikko committed Feb 15, 2023
1 parent 85ab655 commit 33e562c
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
87 changes: 87 additions & 0 deletions lib/dialogs/reportPostDialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:knocky/helpers/api.dart';
import 'package:knocky/models/rule.dart';

class ReportPostDialog extends StatefulWidget {
const ReportPostDialog() : super();

@override
State<ReportPostDialog> createState() => _ReportPostDialogState();
}

const List<String> rules = <String>[];

class _ReportPostDialogState extends State<ReportPostDialog> {
String? selectedRule = null;
bool fetchingRules = true;
late List<KnockoutRule>? rules;

@override
void initState() {
super.initState();

fetchRules();
}

Future<void> fetchRules() async {
List<KnockoutRule>? rulesResult = await KnockoutAPI().rules();

setState(() {
fetchingRules = false;
rules = rulesResult;
});
}

Widget formContent(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.max,
children: [
DropdownButton(
value: selectedRule,
hint: Text('Select rule...'),
items: [
...rules!.map(
(KnockoutRule rule) => DropdownMenuItem(
child: Text(rule!.title ?? 'N/A'), value: rule!.title),
),
],
onChanged: (String? val) {
setState(() {
selectedRule = val!;
});
})
],
);
}

@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text('Report post'),
content: ConstrainedBox(
constraints: BoxConstraints(maxWidth: 400, minHeight: 100),
child: SizedBox(
width: double.maxFinite,
height: 200,
child: fetchingRules
? Center(
child: Container(
child: CircularProgressIndicator(),
width: 50,
height: 50,
),
)
: formContent(context),
),
),
actions: [
TextButton(
onPressed: () => Get.back(result: false), child: Text('Cancel')),
ElevatedButton(
onPressed: () => Get.back(result: true), child: Text('Submit')),
],
);
}
}
14 changes: 14 additions & 0 deletions lib/helpers/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:knocky/models/events.dart';
import 'package:knocky/models/forum.dart';
import 'package:knocky/models/motd.dart';
import 'package:knocky/models/notification.dart';
import 'package:knocky/models/rule.dart';
import 'package:knocky/models/significantThreads.dart';
import 'package:knocky/models/subforumv2.dart' as SubforumV2;
import 'package:knocky/models/syncData.dart';
Expand Down Expand Up @@ -474,4 +475,17 @@ class KnockoutAPI {
throw e;
}
}

Future<List<KnockoutRule>?> rules() async {
try {
final response = await _request(url: 'v2/rules', type: 'get');

return response.data
.map<KnockoutRule>((json) => KnockoutRule.fromJson(json))
.toList();
} on DioError catch (e) {
onDioErrorHandler(e);
throw e;
}
}
}
46 changes: 46 additions & 0 deletions lib/models/rule.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class KnockoutRule {
int? cardinality;
String? category;
DateTime? createdAt;
String? description;
int? id;
int? rulableId;
String? rulableType;
String? title;
DateTime? updatedAt;

KnockoutRule({
this.cardinality,
this.category,
this.createdAt,
this.description,
this.id,
this.rulableId,
this.rulableType,
this.title,
this.updatedAt,
});

factory KnockoutRule.fromJson(Map<String, dynamic> json) => KnockoutRule(
cardinality: json["cardinality"] == null ? null : json["cardinality"],
category: json["category"] == null ? null : json["category"],
createdAt: json["createdAt"] == null
? null
: DateTime.parse(json["createdAt"]),
description: json["description"] == null ? null : json["description"],
id: json["id"] == null ? null : json["id"],
title: json["title"] == null ? null : json["title"],
updatedAt: json["updatedAt"] == null
? null
: DateTime.parse(json["updatedAt"]),
);
Map<String, dynamic> toJson() => {
'cardinality': cardinality,
'category': category,
'createdAt': createdAt?.toIso8601String(),
'description': description,
'id': id,
'title': title,
'updatedAt': updatedAt?.toIso8601String(),
};
}
7 changes: 5 additions & 2 deletions lib/widgets/post/toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart';
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:timeago/timeago.dart' as timeago;
import 'package:knocky/models/thread.dart';

Expand Down Expand Up @@ -69,9 +70,11 @@ class Toolbar extends StatelessWidget {
IconButton(
iconSize: 12,
icon: FaIcon(
FontAwesomeIcons.pen,
FontAwesomeIcons.solidFlag,
),
onPressed: () => this.onToggleEditPost!.call(),
onPressed: () {
Get.dialog(ReportPostDialog());
},
),
(authController.isAuthenticated.value &&
post!.user!.id == authController.userId.value)
Expand Down

0 comments on commit 33e562c

Please sign in to comment.