Merge pull request #69 from krawieck/remove-comment

This commit is contained in:
Filip Krawczyk 2020-10-06 01:09:37 +02:00 committed by GitHub
commit dd4b663c28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 7 deletions

View File

@ -98,6 +98,8 @@ class Comment extends HookWidget {
} }
bool get isOP => commentTree.comment.creatorId == postCreatorId; bool get isOP => commentTree.comment.creatorId == postCreatorId;
bool get isMine =>
commentTree.comment.creatorId == commentTree.comment.userId;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -106,12 +108,32 @@ class Comment extends HookWidget {
final showRaw = useState(false); final showRaw = useState(false);
final collapsed = useState(false); final collapsed = useState(false);
final myVote = useState(commentTree.comment.myVote ?? VoteType.none); final myVote = useState(commentTree.comment.myVote ?? VoteType.none);
final isDeleted = useState(commentTree.comment.deleted);
final delayedVoting = useDelayedLoading(); final delayedVoting = useDelayedLoading();
final delayedDeletion = useDelayedLoading();
final loggedInAction = useLoggedInAction(commentTree.comment.instanceUrl); final loggedInAction = useLoggedInAction(commentTree.comment.instanceUrl);
final newReplies = useState(const <CommentTree>[]); final newReplies = useState(const <CommentTree>[]);
final comment = commentTree.comment; final comment = commentTree.comment;
handleDelete(Jwt token) async {
final api = LemmyApi(token.payload.iss).v1;
delayedDeletion.start();
Navigator.of(context).pop();
try {
final res = await api.deleteComment(
editId: comment.id, deleted: !isDeleted.value, auth: token.raw);
isDeleted.value = res.deleted;
// ignore: avoid_catches_without_on_clauses
} catch (e) {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text('Failed to delete/restore comment')));
return;
}
delayedDeletion.cancel();
}
void _openMoreMenu(BuildContext context) { void _openMoreMenu(BuildContext context) {
pop() => Navigator.of(context).pop(); pop() => Navigator.of(context).pop();
@ -160,6 +182,12 @@ class Comment extends HookWidget {
pop(); pop();
}, },
), ),
if (isMine)
ListTile(
leading: Icon(isDeleted.value ? Icons.restore : Icons.delete),
title: Text(isDeleted.value ? 'Restore' : 'Delete'),
onTap: loggedInAction(handleDelete),
),
ListTile( ListTile(
leading: Icon(Icons.info_outline), leading: Icon(Icons.info_outline),
title: Text('Nerd stuff'), title: Text('Nerd stuff'),
@ -209,7 +237,7 @@ class Comment extends HookWidget {
}(); }();
final body = () { final body = () {
if (comment.deleted) { if (isDeleted.value) {
return Flexible( return Flexible(
child: Text( child: Text(
'comment deleted by creator', 'comment deleted by creator',
@ -254,7 +282,7 @@ class Comment extends HookWidget {
final actions = collapsed.value final actions = collapsed.value
? SizedBox.shrink() ? SizedBox.shrink()
: Row(children: [ : Row(children: [
if (selectable.value && !comment.deleted && !comment.removed) if (selectable.value && !isDeleted.value && !comment.removed)
_CommentAction( _CommentAction(
icon: Icons.content_copy, icon: Icons.content_copy,
tooltip: 'copy', tooltip: 'copy',
@ -268,14 +296,16 @@ class Comment extends HookWidget {
_CommentAction( _CommentAction(
icon: Icons.more_horiz, icon: Icons.more_horiz,
onPressed: () => _openMoreMenu(context), onPressed: () => _openMoreMenu(context),
loading: delayedDeletion.loading,
tooltip: 'more', tooltip: 'more',
), ),
_SaveComment(commentTree.comment), _SaveComment(commentTree.comment),
_CommentAction( if (!isDeleted.value && !comment.removed)
icon: Icons.reply, _CommentAction(
onPressed: loggedInAction((_) => reply()), icon: Icons.reply,
tooltip: 'reply', onPressed: loggedInAction((_) => reply()),
), tooltip: 'reply',
),
_CommentAction( _CommentAction(
icon: Icons.arrow_upward, icon: Icons.arrow_upward,
iconColor: myVote.value == VoteType.up ? theme.accentColor : null, iconColor: myVote.value == VoteType.up ? theme.accentColor : null,