[results feed] When adding a comment/approval, show the new comment.

Previously, the new comment was only added to the database, and not
also added to the list of comments shown in the current view.

Change-Id: Iffb988d00e042cf66d9be6f16f7c469fd84b6d83
Reviewed-on: https://dart-review.googlesource.com/c/dart_ci/+/128140
Reviewed-by: Alexander Thomas <athom@google.com>
diff --git a/results_feed/lib/src/components/commit_component.dart b/results_feed/lib/src/components/commit_component.dart
index 44bcdb2..1d74b54 100644
--- a/results_feed/lib/src/components/commit_component.dart
+++ b/results_feed/lib/src/components/commit_component.dart
@@ -13,6 +13,7 @@
 import 'blamelist_component.dart';
 import 'blamelist_picker.dart';
 import '../model/commit.dart';
+import '../model/comment.dart';
 import '../services/firestore_service.dart';
 import '../services/filter_service.dart';
 import 'results_panel.dart';
@@ -68,12 +69,16 @@
     ]).then((_) => window.location.reload());
   }
 
-  Future approve(bool approval) {
+  Future<void> approve(bool approval) async {
     approving = false;
     for (Change result in selected) {
       result.approved = approval ?? result.approved;
     }
-    return firestoreService.saveApproval(
+
+    await firestoreService.saveApprovals(
+        approve: approval,
+        resultIds: [for (Change result in selected) result.id]);
+    final comment = Comment.fromDocument(await firestoreService.saveComment(
         approval,
         commentText,
         changeGroup.comments.isEmpty
@@ -82,6 +87,12 @@
                 changeGroup.comments.last.id,
         resultIds: [for (Change result in selected) result.id],
         blamelistStart: changeGroup.range.start,
-        blamelistEnd: changeGroup.range.end);
+        blamelistEnd: changeGroup.range.end));
+    // The list changeGroup.comments is the actual list object stored in the
+    // applications-level map of all comments, AppComponent.comments.
+    // So changing it here changes it everywhere.
+    changeGroup.comments
+      ..add(comment)
+      ..sort();
   }
 }
diff --git a/results_feed/lib/src/components/try_results_component.dart b/results_feed/lib/src/components/try_results_component.dart
index 09de21d..d1b49a5 100644
--- a/results_feed/lib/src/components/try_results_component.dart
+++ b/results_feed/lib/src/components/try_results_component.dart
@@ -70,11 +70,11 @@
     }
   }
 
-  Future approve(bool approval) {
+  Future<void> approve(bool approval) async {
     for (Change result in selected) {
       result.approved = approval ?? result.approved;
     }
-    return _tryDataService.saveApproval(
+    final comment = await _tryDataService.saveApproval(
         approval,
         commentText,
         changeGroup.comments.isEmpty
@@ -83,6 +83,10 @@
                 changeGroup.comments.last.id,
         [for (Change result in selected) result.id],
         changeInfo.review);
+    comments
+      ..add(comment)
+      ..sort();
+    _approving = false;
   }
 
   void tryUpdate() async {
diff --git a/results_feed/lib/src/services/firestore_service.dart b/results_feed/lib/src/services/firestore_service.dart
index 609c7c0..bc1c6c1 100644
--- a/results_feed/lib/src/services/firestore_service.dart
+++ b/results_feed/lib/src/services/firestore_service.dart
@@ -136,13 +136,14 @@
     }
   }
 
-  Future saveApproval(bool approve, String comment, String baseComment,
+  Future<firestore.DocumentSnapshot> saveComment(
+      bool approve, String comment, String baseComment,
       {List<String> resultIds,
       List<String> tryResultIds,
       int blamelistStart,
       int blamelistEnd,
       int review}) async {
-    await app.firestore().collection('comments').add({
+    final reference = await app.firestore().collection('comments').add({
       'author': app.auth().currentUser.email,
       if (comment != null) 'comment': comment,
       'created': DateTime.now(),
@@ -153,6 +154,11 @@
       if (blamelistStart != null) 'blamelist_end_index': blamelistEnd,
       if (review != null) 'review': review
     });
+    return reference.get();
+  }
+
+  Future<void> saveApprovals(
+      {bool approve, List<String> resultIds, List<String> tryResultIds}) async {
     if (approve == null) return;
     // Update approved field in results documents.
     Future<void> approveResults(List<String> ids, String collectionName) async {
diff --git a/results_feed/lib/src/services/try_data_service.dart b/results_feed/lib/src/services/try_data_service.dart
index e74c1fa..539ddc3 100644
--- a/results_feed/lib/src/services/try_data_service.dart
+++ b/results_feed/lib/src/services/try_data_service.dart
@@ -6,9 +6,11 @@
 
 class TryDataService {
   final FirestoreService _firestoreService;
+
   TryDataService(this._firestoreService);
 
   Future logIn() => _firestoreService.logIn();
+
   bool get isLoggedIn => _firestoreService.isLoggedIn;
 
   Future<List<Change>> changes(ReviewInfo changeInfo, int patch) async {
@@ -43,10 +45,14 @@
     return [for (final doc in docs) Comment.fromDocument(doc)];
   }
 
-  Future saveApproval(bool approve, String comment, String baseComment,
-          Iterable<String> resultIds, int review) =>
-      _firestoreService.saveApproval(approve, comment, baseComment,
-          tryResultIds: resultIds, review: review);
+  Future<Comment> saveApproval(bool approve, String comment, String baseComment,
+      Iterable<String> resultIds, int review) async {
+    await _firestoreService.saveApprovals(
+        approve: approve, tryResultIds: resultIds);
+    return Comment.fromDocument(await _firestoreService.saveComment(
+        approve, comment, baseComment,
+        tryResultIds: resultIds, review: review));
+  }
 }
 
 class ReviewInfo {