Merge pull request #219 from krawieck/feature/edit-comment
This commit is contained in:
commit
6cea6b0162
|
@ -1,3 +1,9 @@
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Editing comments
|
||||||
|
|
||||||
## v0.4.2 - 2021-04-12
|
## v0.4.2 - 2021-04-12
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
|
@ -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) {
|
void _openMoreMenu(BuildContext context) {
|
||||||
pop() => Navigator.of(context).pop();
|
pop() => Navigator.of(context).pop();
|
||||||
|
|
||||||
|
@ -174,6 +193,12 @@ class CommentWidget extends HookWidget {
|
||||||
pop();
|
pop();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
if (isMine)
|
||||||
|
ListTile(
|
||||||
|
leading: const Icon(Icons.edit),
|
||||||
|
title: const Text('Edit'),
|
||||||
|
onTap: handleEdit,
|
||||||
|
),
|
||||||
if (isMine)
|
if (isMine)
|
||||||
ListTile(
|
ListTile(
|
||||||
leading: Icon(isDeleted.value ? Icons.restore : Icons.delete),
|
leading: Icon(isDeleted.value ? Icons.restore : Icons.delete),
|
||||||
|
|
|
@ -8,22 +8,30 @@ import '../l10n/l10n.dart';
|
||||||
import 'markdown_mode_icon.dart';
|
import 'markdown_mode_icon.dart';
|
||||||
import 'markdown_text.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]
|
/// on submit pops the navigator stack with a [CommentView]
|
||||||
/// or `null` if cancelled
|
/// or `null` if cancelled
|
||||||
class WriteComment extends HookWidget {
|
class WriteComment extends HookWidget {
|
||||||
final Post post;
|
final Post post;
|
||||||
final Comment? comment;
|
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({
|
const WriteComment.toComment({
|
||||||
required Comment this.comment,
|
required Comment this.comment,
|
||||||
required this.post,
|
required this.post,
|
||||||
});
|
}) : _isEdit = false;
|
||||||
|
const WriteComment.edit({
|
||||||
|
required Comment this.comment,
|
||||||
|
required this.post,
|
||||||
|
}) : _isEdit = true;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final controller = useTextEditingController();
|
final controller =
|
||||||
|
useTextEditingController(text: _isEdit ? comment?.content : null);
|
||||||
final showFancy = useState(false);
|
final showFancy = useState(false);
|
||||||
final delayed = useDelayedLoading();
|
final delayed = useDelayedLoading();
|
||||||
final loggedInAction = useLoggedInAction(post.instanceHost);
|
final loggedInAction = useLoggedInAction(post.instanceHost);
|
||||||
|
@ -56,12 +64,22 @@ class WriteComment extends HookWidget {
|
||||||
|
|
||||||
delayed.start();
|
delayed.start();
|
||||||
try {
|
try {
|
||||||
final res = await api.run(CreateComment(
|
final res = await () {
|
||||||
content: controller.text,
|
if (_isEdit) {
|
||||||
postId: post.id,
|
return api.run(EditComment(
|
||||||
parentId: comment?.id,
|
commentId: comment!.id,
|
||||||
auth: token.raw,
|
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);
|
Navigator.of(context).pop(res.commentView);
|
||||||
// ignore: avoid_catches_without_on_clauses
|
// ignore: avoid_catches_without_on_clauses
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -120,7 +138,9 @@ class WriteComment extends HookWidget {
|
||||||
delayed.pending ? () {} : loggedInAction(handleSubmit),
|
delayed.pending ? () {} : loggedInAction(handleSubmit),
|
||||||
child: delayed.loading
|
child: delayed.loading
|
||||||
? const CircularProgressIndicator()
|
? const CircularProgressIndicator()
|
||||||
: Text(L10n.of(context)!.post),
|
: Text(_isEdit
|
||||||
|
? L10n.of(context)!.edit
|
||||||
|
: L10n.of(context)!.post),
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in New Issue