Skip to content

Commit

Permalink
frontend fix waiting status change + quiz/feedback reset + initial pa…
Browse files Browse the repository at this point in the history
…rticipants
  • Loading branch information
Fabi-02 committed Mar 14, 2024
1 parent fc472d5 commit 799139a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
35 changes: 21 additions & 14 deletions frontend/lib/pages/feedback/feedback_result_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ class _FeedbackResultPageState extends State<FeedbackResultPage> {
if (response.statusCode == 200) {
var data = jsonDecode(response.body);
var form = FeedbackForm.fromJson(data);


_form = form;
_participantCounter = data["participants"].length;
_results = getResults(data);

startWebsocket();

setState(() {
_form = form;
_results = getResults(data);
_loading = false;
_fetchResult = 'success';
});
Expand Down Expand Up @@ -137,14 +139,6 @@ class _FeedbackResultPageState extends State<FeedbackResultPage> {
Uri.parse(
"${getBackendUrl(protocol: "ws")}/course/$_courseId/feedback/form/$_formId/subscribe/$_userId/${getSession()!.jwt}"),
);
if (_socketChannel != null) {
_socketChannel!.sink.add(jsonEncode({
"action": "CHANGE_FORM_STATUS",
"formStatus": "WAITING",
"roles": _roles,
"userId": _userId,
}));
}

_socketChannel!.stream.listen((event) {
var data = jsonDecode(event);
Expand All @@ -170,6 +164,21 @@ class _FeedbackResultPageState extends State<FeedbackResultPage> {
_form.status = "ERROR";
});
});

if (_form.status == "NOT_STARTED") {
openWaitingRoom();
}
}

void openWaitingRoom() {
if (_socketChannel != null) {
_socketChannel!.sink.add(jsonEncode({
"action": "CHANGE_FORM_STATUS",
"formStatus": "WAITING",
"roles": _roles,
"userId": _userId,
}));
}
}

void startForm() {
Expand Down Expand Up @@ -203,9 +212,7 @@ class _FeedbackResultPageState extends State<FeedbackResultPage> {
"userId": _userId,
}));
}
setState(() {
_participantCounter = 0;
});
Navigator.pop(context);
}

List<Map<String, dynamic>> getResults(Map<String, dynamic> json) {
Expand Down
46 changes: 26 additions & 20 deletions frontend/lib/pages/quiz/quiz_control_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,23 @@ class _QuizControlPageState extends State<QuizControlPage> {
);
if (response.statusCode == 200) {
var data = jsonDecode(response.body);
//TODO: Print in production code not good:
print(data);
var form = QuizForm.fromJson(data);

_form = form;
_results = getResults(data);
_participantCounter = data["participants"].length;
_userNames = data["participants"]
.map((participant) => participant["userAlias"])
.toList();
if (_form.status == "FINISHED") {
_scoreboard = getScoreboard(data);
}

startWebsocket();

setState(() {
_loading = false;
_fetchResult = 'success';
_form = form;
_results = getResults(data);
if (_form.status == "FINISHED") {
_scoreboard = getScoreboard(data);
}
});
}
} on http.ClientException {
Expand Down Expand Up @@ -133,17 +136,8 @@ class _QuizControlPageState extends State<QuizControlPage> {
Uri.parse(
"${getBackendUrl(protocol: "ws")}/course/$_courseId/quiz/form/$_formId/subscribe/$_userId/${getSession()!.jwt}"),
);
if (_socketChannel != null) {
_socketChannel!.sink.add(jsonEncode({
"action": "CHANGE_FORM_STATUS",
"formStatus": "WAITING",
"roles": _roles,
"userId": _userId,
}));
}

_socketChannel!.stream.listen((event) {
print(event);
var data = jsonDecode(event);

if (data["action"] == "FORM_STATUS_CHANGED") {
Expand Down Expand Up @@ -185,6 +179,21 @@ class _QuizControlPageState extends State<QuizControlPage> {
_form.status = "ERROR";
});
});

if (_form.status == "NOT_STARTED") {
openWaitingRoom();
}
}

void openWaitingRoom() {
if (_socketChannel != null) {
_socketChannel!.sink.add(jsonEncode({
"action": "CHANGE_FORM_STATUS",
"formStatus": "WAITING",
"roles": _roles,
"userId": _userId,
}));
}
}

void startForm() {
Expand Down Expand Up @@ -218,10 +227,7 @@ class _QuizControlPageState extends State<QuizControlPage> {
"userId": _userId,
}));
}
setState(() {
_participantCounter = 0;
_userNames = _userNames = [];
});
Navigator.pop(context);
}

void next() {
Expand Down

0 comments on commit 799139a

Please sign in to comment.