Skip to content

Commit

Permalink
Admin features frontend (#716)
Browse files Browse the repository at this point in the history
* Node model changed. Admin node hide feature completed. Admin provider fixed.

* Profile admin features connected to the backend

* minor fix

* fix promote/demote reviewer

* added hide button to questions

* is hidden

* models updated, bug fixed

* fix state updates hide or show buttons

* small fix in provider & ban button

* fix answer question bug.

* fix admin hide/show button

* answer question bug fix

* empty auth

---------

Co-authored-by: brunettow <bengisutakkin34@hotmail.com>
  • Loading branch information
defabdullah and Brunettow authored Dec 25, 2023
1 parent c6e170d commit 3168acb
Show file tree
Hide file tree
Showing 19 changed files with 385 additions and 203 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collaborative_science_platform/providers/admin_provider.dart';
import 'package:collaborative_science_platform/providers/annotation_provider.dart';
import 'package:collaborative_science_platform/providers/auth.dart';
import 'package:collaborative_science_platform/providers/profile_data_provider.dart';
Expand Down Expand Up @@ -45,9 +46,11 @@ class MyApp extends StatelessWidget {
ChangeNotifierProvider<NodeProvider>(create: (context) => NodeProvider()),
ChangeNotifierProvider<UserProvider>(create: (context) => UserProvider()),
ChangeNotifierProvider<WorkspaceProvider>(create: (context) => WorkspaceProvider()),
ChangeNotifierProvider<QuestionAnswerProvider>(create: (context) => QuestionAnswerProvider()),
ChangeNotifierProvider<QuestionAnswerProvider>(
create: (context) => QuestionAnswerProvider()),
ChangeNotifierProvider<AnnotationProvider>(create: (context) => AnnotationProvider()),
ChangeNotifierProvider<WikiDataProvider>(create: (context) => WikiDataProvider()),
ChangeNotifierProvider<AdminProvider>(create: (context) => AdminProvider()),
ChangeNotifierProvider<SettingsProvider>(create: (context) => SettingsProvider()),
],
child: FutureBuilder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class NodeDetailed {
List<Node> citations;
bool isValid;
int noVisits;
bool isHidden;
List<Question> questions;

//List<SemanticTag> semanticTags;
Expand All @@ -37,6 +38,7 @@ class NodeDetailed {
this.isValid = true,
this.noVisits = 0,
this.questions = const [],
this.isHidden = false,
//required this.semanticTags,
//required this.wikiTags,
//required this.annotations,
Expand Down Expand Up @@ -66,6 +68,7 @@ class NodeDetailed {
citations: citations,
contributors: contributors,
isValid: jsonString['is_valid'],
isHidden: jsonString['removed_by_admin'],
nodeId: jsonString['node_id'],
nodeTitle: jsonString['node_title'],
noVisits: jsonString['num_visits'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:collaborative_science_platform/models/user.dart';

class Question {
Expand All @@ -10,6 +12,7 @@ class Question {
String? answeredAt;
int? nodeId;
bool isAnswered;
bool isHidden;
Question(
{required this.id,
required this.content,
Expand All @@ -19,7 +22,8 @@ class Question {
required this.answerer,
required this.answeredAt,
required this.nodeId,
required this.isAnswered});
required this.isAnswered,
required this.isHidden});
factory Question.fromJson(Map<String, dynamic> jsonString) {
return Question(
id: jsonString['id'] ?? -1,
Expand All @@ -33,6 +37,7 @@ class Question {
asker: User.fromJsonforNodeDetailPage(jsonString['asker']),
nodeId: jsonString['node_id'] ?? -1,
isAnswered: jsonString['answer_content'] != null,
isHidden: jsonString['removed_by_admin'] ?? false,
);
}

Expand All @@ -59,6 +64,7 @@ class Question {
answeredAt: jsonString.containsKey("answer_date") ? jsonString["answer_date"] as String : "",
nodeId: jsonString['node_id'] ?? -1,
isAnswered: jsonString['is_answered'] == 1,
isHidden: jsonString['removed_by_admin'] ?? false,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,26 @@ class ProfileData {
String surname;
String email;
String aboutMe;
String orcid;
List<Node> nodes;
List<Question> askedQuestions;
List<Question> answeredQuestions;
String userType;
bool isBanned;
List<WorkspaceSemanticTag> tags;
ProfileData(
{this.id = 0,
this.aboutMe = "",
this.email = "",
this.name = "",
this.surname = "",
this.tags = const [],
this.nodes = const [],
this.askedQuestions = const [],
this.answeredQuestions = const [],
ProfileData({
this.id = 0,
this.aboutMe = "",
this.email = "",
this.name = "",
this.surname = "",
this.orcid = "",
this.tags = const [],
this.nodes = const [],
this.askedQuestions = const [],
this.answeredQuestions = const [],
this.userType = "",
this.isBanned = false,
});

factory ProfileData.fromJson(Map<String, dynamic> jsonString) {
Expand All @@ -59,12 +65,16 @@ class ProfileData {
List<WorkspaceSemanticTag> tags = tagList.map((e) => WorkspaceSemanticTag.fromJson(e)).toList();

return ProfileData(
nodes: nodes,
id: jsonString['id'],
name: jsonString['name'],
surname: jsonString['surname'],
aboutMe: jsonString['bio'],
orcid: jsonString['orcid'] ?? "",
nodes: nodes,
askedQuestions: asked,
answeredQuestions: answered,
userType: jsonString['user_type'],
isBanned: jsonString['is_banned'] ?? false,
tags: tags,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class AdminProvider with ChangeNotifier {
Future<int> banUser(User? user, User? admin, bool isBanned) async {
Future<int> banUser(User? admin, int userId, bool isBanned) async {
final Map<String, String> header = {
"Accept": "application/json",
"content-type": "application/json",
'Authorization': admin!.token,
'Authorization': "Token ${admin!.token}",
};

final String body = json.encode({
'context': "user",
'content_id': userId,
'hide': isBanned,
});
try {
final response = await http.put(
Uri.parse("${Constants.apiUrl}/update_content_status/"),
headers: header,
body: jsonEncode(
<String, String>{
'context': "user",
'content_id': user!.id.toString(),
'hide': isBanned.toString()
},
),
body: body,
);
print(response.statusCode);
return response.statusCode;
Expand All @@ -37,20 +35,18 @@ class AdminProvider with ChangeNotifier {
final Map<String, String> header = {
"Accept": "application/json",
"content-type": "application/json",
'Authorization': admin!.token,
'Authorization': "Token ${admin!.token}",
};

final String body = json.encode({
'context': "node",
'content_id': node.nodeId,
'hide': isHidden,
});
try {
final response = await http.put(
Uri.parse("${Constants.apiUrl}/update_content_status/"),
headers: header,
body: jsonEncode(
<String, String>{
'context': "node",
'content_id': node.nodeId.toString(),
'hide': isHidden.toString()
},
),
body: body,
);
print(response.statusCode);
} catch (e) {
Expand All @@ -62,20 +58,18 @@ class AdminProvider with ChangeNotifier {
final Map<String, String> header = {
"Accept": "application/json",
"content-type": "application/json",
'Authorization': admin!.token,
'Authorization': "Token ${admin!.token}",
};

final String body = json.encode({
'context': "question",
'content_id': question.id,
'hide': isHidden,
});
try {
final response = await http.put(
Uri.parse("${Constants.apiUrl}/update_content_status/"),
headers: header,
body: jsonEncode(
<String, String>{
'context': "question",
'content_id': "-1", // TODO question!.id.toString().
'hide': isHidden.toString()
},
),
body: body,
);
print(response.statusCode);
return response.statusCode;
Expand All @@ -84,47 +78,42 @@ class AdminProvider with ChangeNotifier {
}
}

Future<void> promoteUser(User? user, User? admin) async {
Future<int> promoteUser(User? admin, int userId) async {
final Map<String, String> header = {
"Accept": "application/json",
"content-type": "application/json",
'Authorization': admin!.token,
'Authorization': "Token ${admin!.token}",
};

final String body = json.encode({'cont_id': userId});
try {
final response = await http.post(
Uri.parse("${Constants.apiUrl}/promote_contributor/"),
headers: header,
body: jsonEncode(
<String, String>{
'cont_id': user!.id.toString(), //The basic user id of the contributor
},
),
);
final response = await http.post(Uri.parse("${Constants.apiUrl}/promote_contributor/"),
headers: header, body: body);

if (response.statusCode == 200) {
print("User is promoted to reviewer.");
}
print(response.statusCode);
return response.statusCode;
} catch (e) {
rethrow;
}
}

Future<void> demoteUser(User? user, User? admin) async {
Future<int> demoteUser(User? admin, int userId) async {
final Map<String, String> header = {
"Accept": "application/json",
"content-type": "application/json",
'Authorization': admin!.token,
'Authorization': "Token ${admin!.token}",
};

try {
final response = await http.delete(
Uri.parse("${Constants.apiUrl}/demote_reviewer/?${user!.id.toString()}"),
headers: header,
body: jsonEncode(
<String, String>{
'reviewer_id': user.id.toString(), //The basic user id of the reviewer
},
),
);
Uri.parse("${Constants.apiUrl}/demote_reviewer/?reviewer_id=${userId.toString()}"),
headers: header);
if (response.statusCode == 200) {
print("User is demoted to contributor.");
}
print(response.statusCode);
return response.statusCode;
} catch (e) {
rethrow;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class NodeProvider with ChangeNotifier {
}
}

Future<void> getNode(int id) async {
Future<void> getNode(int id, String token) async {
clearAll();
Uri url = Uri.parse("${Constants.apiUrl}/get_node/");

Expand All @@ -155,7 +155,8 @@ class NodeProvider with ChangeNotifier {

final Map<String, String> headers = {
"Accept": "application/json",
"content-type": "application/json"
"content-type": "application/json",
"Authorization": "Token $token"
};
try {
final response = await http.get(url, headers: headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ProfileDataProvider with ChangeNotifier {
throw Exception("Something has happened");
}
} catch (e) {
print(e);
rethrow;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ class QuestionAnswerProvider with ChangeNotifier {
answerer: null,
answeredAt: null,
nodeId: nodeId,
isAnswered: false));
isAnswered: false,
isHidden: false));

_questions
.sort((a, b) => DateTime.parse(b.createdAt).compareTo(DateTime.parse(a.createdAt)));
notifyListeners();
} else if (response.statusCode == 401) {
throw PostQuestionError();
Expand All @@ -54,7 +58,7 @@ class QuestionAnswerProvider with ChangeNotifier {
}
}

Future<void> postAnswer(String answerText, int questionId, User user) async {
Future<void> postAnswer(String answerText, Question question, User user) async {
Uri url = Uri.parse("${Constants.apiUrl}/answer_question/");
final Map<String, String> headers = {
"Accept": "application/json",
Expand All @@ -63,7 +67,7 @@ class QuestionAnswerProvider with ChangeNotifier {
};
final Map<String, dynamic> postData = {
"answer_content": answerText,
"question_id": questionId,
"question_id": question.id,
};
try {
final response = await http.post(
Expand All @@ -72,11 +76,10 @@ class QuestionAnswerProvider with ChangeNotifier {
body: json.encode(postData),
);
if (response.statusCode == 201) {
Question answeredQuestions = questions.firstWhere((element) => element.id == questionId);
answeredQuestions.isAnswered = true;
answeredQuestions.answer = answerText;
answeredQuestions.answeredAt = DateTime.now().toString();
answeredQuestions.answerer = user;
question.isAnswered = true;
question.answer = answerText;
question.answeredAt = DateTime.now().toString();
question.answerer = user;
notifyListeners();
} else if (response.statusCode == 403) {
throw PostQuestionError();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:collaborative_science_platform/exceptions/node_details_exceptions.dart';
import 'package:collaborative_science_platform/models/node_details_page/node_detailed.dart';
import 'package:collaborative_science_platform/providers/auth.dart';
import 'package:collaborative_science_platform/providers/node_provider.dart';
import 'package:collaborative_science_platform/screens/graph_page/mobile_graph_page.dart';
import 'package:collaborative_science_platform/screens/graph_page/web_graph_page.dart';
Expand Down Expand Up @@ -55,6 +56,7 @@ class _GraphPageState extends State<GraphPage> {
void getNode() async {
try {
final nodeProvider = Provider.of<NodeProvider>(context, listen: false);
final auth = Provider.of<Auth>(context);
setState(() {
error = false;
isLoading = true;
Expand All @@ -67,7 +69,7 @@ class _GraphPageState extends State<GraphPage> {
return;
}
}
await nodeProvider.getNode(widget.nodeId);
await nodeProvider.getNode(widget.nodeId, auth.user!.token);
setState(() {
node = nodeProvider.nodeDetailed!;
});
Expand Down
Loading

0 comments on commit 3168acb

Please sign in to comment.