Add comment editing

This commit is contained in:
shilangyu 2021-04-17 22:45:18 +02:00
parent 4f6d03c5cb
commit 7a627bbb42
2 changed files with 56 additions and 11 deletions

View File

@ -130,6 +130,25 @@ class CommentWidget extends HookWidget {
);
}
handleEdit() async {
final editedComment = await showCupertinoModalPopup<CommentView>(
context: context,
builder: (_) => WriteComment.edit(
comment: comment.comment,
post: comment.post,
),
);
if (editedComment != null) {
commentTree.comment = editedComment;
// TODO: workaround to force this widget to rebuild
// TODO: These problems should go away once we move to an actual state management lib
// TODO: work being done here should also help: https://github.com/krawieck/lemmur/tree/fix/better-local-updates
(context as Element).markNeedsBuild();
Navigator.of(context).pop();
}
}
void _openMoreMenu(BuildContext context) {
pop() => Navigator.of(context).pop();
@ -174,6 +193,12 @@ class CommentWidget extends HookWidget {
pop();
},
),
if (isMine)
ListTile(
leading: const Icon(Icons.edit),
title: const Text('Edit'),
onTap: handleEdit,
),
if (isMine)
ListTile(
leading: Icon(isDeleted.value ? Icons.restore : Icons.delete),

View File

@ -8,22 +8,30 @@ import '../l10n/l10n.dart';
import 'markdown_mode_icon.dart';
import 'markdown_text.dart';
/// Modal for writing a comment to a given post/comment (aka reply)
/// Modal for writing/editing a comment to a given post/comment (aka reply)
/// on submit pops the navigator stack with a [CommentView]
/// or `null` if cancelled
class WriteComment extends HookWidget {
final Post post;
final Comment? comment;
final bool _isEdit;
const WriteComment.toPost(this.post) : comment = null;
const WriteComment.toPost(this.post)
: comment = null,
_isEdit = false;
const WriteComment.toComment({
required Comment this.comment,
required this.post,
});
}) : _isEdit = false;
const WriteComment.edit({
required Comment this.comment,
required this.post,
}) : _isEdit = true;
@override
Widget build(BuildContext context) {
final controller = useTextEditingController();
final controller =
useTextEditingController(text: _isEdit ? comment?.content : null);
final showFancy = useState(false);
final delayed = useDelayedLoading();
final loggedInAction = useLoggedInAction(post.instanceHost);
@ -56,12 +64,22 @@ class WriteComment extends HookWidget {
delayed.start();
try {
final res = await api.run(CreateComment(
content: controller.text,
postId: post.id,
parentId: comment?.id,
auth: token.raw,
));
final res = await () {
if (_isEdit) {
return api.run(EditComment(
commentId: comment!.id,
content: controller.text,
auth: token.raw,
));
} else {
return api.run(CreateComment(
content: controller.text,
postId: post.id,
parentId: comment?.id,
auth: token.raw,
));
}
}();
Navigator.of(context).pop(res.commentView);
// ignore: avoid_catches_without_on_clauses
} catch (e) {
@ -120,7 +138,9 @@ class WriteComment extends HookWidget {
delayed.pending ? () {} : loggedInAction(handleSubmit),
child: delayed.loading
? const CircularProgressIndicator()
: Text(L10n.of(context)!.post),
: Text(_isEdit
? L10n.of(context)!.edit
: L10n.of(context)!.post),
)
],
),