From 754745fd5f04a2e6b01f633e5479a467c38c67a7 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 18 May 2019 15:05:26 +0200 Subject: [PATCH] Show newly created comment --- lib/helpers/comments_helper.dart | 12 ++++++++++++ lib/ui/tiles/post_tile.dart | 14 +++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/helpers/comments_helper.dart b/lib/helpers/comments_helper.dart index b0594f6..4b62a12 100644 --- a/lib/helpers/comments_helper.dart +++ b/lib/helpers/comments_helper.dart @@ -25,6 +25,18 @@ class CommentsHelper { return response.getObject()["commentID"]; } + /// Get a single comment from the server, specified by its [id] + Future getSingle(int id) async { + final response = await APIRequest( + uri: "comments/get_single", + needLogin: true, + args: {"commentID": id.toString()}).exec(); + + if (response.code != 200) return null; + + return apiToComment(response.getObject()); + } + /// Turn an API entry into a [Comment] object static Comment apiToComment(Map entry) { return Comment( diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index d216803..6a04f5e 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -45,6 +45,9 @@ class PostTile extends StatefulWidget { } class _PostTileState extends State { + // Helpers + final _commentsHelper = CommentsHelper(); + // Class members TextEditingController _commentController = TextEditingController(); File _commentImage; @@ -308,7 +311,7 @@ class _PostTileState extends State { Future _submitComment() async { _sendingComment = true; - final commentID = await CommentsHelper().createComment(NewComment( + final commentID = await _commentsHelper.createComment(NewComment( postID: widget.post.id, content: _commentController.text, image: _commentImage, @@ -322,5 +325,14 @@ class _PostTileState extends State { clearCommentForm(); // Get and show new comment + final newComment = await _commentsHelper.getSingle(commentID); + + if (newComment == null) + return showSimpleSnack( + context, tr("Could not retrieve created comment!")); + + setState(() { + widget.post.comments.add(newComment); + }); } }