fixed bugs in the comments section

This commit is contained in:
Amir Hossein Mousavi 2023-09-05 16:40:20 +03:30
parent c94f043f3f
commit 4922f517ad
2 changed files with 74 additions and 73 deletions

View File

@ -32,6 +32,7 @@ class CommentsState extends CoreProvier {
);
await service.httpGet();
if (service.isSuccess) {
comments.clear();
final messages = service.result['comments'];
for (var i = 0; i < messages.length; i++) {
comments.add(CommentData.fromJson(messages[i]));
@ -126,9 +127,6 @@ class CommentsState extends CoreProvier {
);
}
_count++;
onCommentsChanged(_count);
final body = {};
if (commentId != null) {
@ -175,36 +173,35 @@ class CommentsState extends CoreProvier {
);
}
void deleteComment(int id, int? rootId) {
void deleteComment(int id, int status, int? rootId) async {
final service = RequestService(RequestHelper.deleteComment(id));
service.delete();
if (rootId == null) {
final comment = comments.firstWhere((element) => element.id == id);
if (comment.replies.isNotEmpty) {
comments.insertAll(
comments.indexOf(comment),
comment.replies.map(
(rep) => CommentData(
id: rep.id,
text: rep.text,
createdAt: rep.createdAt,
liked: rep.liked,
disliked: rep.disliked,
feedback: rep.feedback,
user: rep.user,
replies: [],
status: 2,
),
),
);
}
_count = 0;
await getComments();
onCommentsChanged(_count);
} else {
comments.remove(comment);
if (status != 2) {
_count--;
onCommentsChanged(_count);
}
}
} else {
comments
.firstWhere((element) => element.id == rootId)
.replies
.removeWhere((element) => element.id == id);
if (status != 2) {
_count--;
onCommentsChanged(_count);
}
}
notifyListeners();
}
}

View File

@ -141,6 +141,7 @@ class CommentState extends State<Comment> {
],
),
const SizedBox(height: 8),
if (_comment.status != 2)
Row(
children: [
InkWrapper(
@ -175,7 +176,8 @@ class CommentState extends State<Comment> {
turns: _showSubComments ? 0.5 : 0,
child: Icon(
DidvanIcons.angle_down_regular,
color: Theme.of(context).colorScheme.primary,
color:
Theme.of(context).colorScheme.primary,
),
),
],
@ -187,7 +189,8 @@ class CommentState extends State<Comment> {
dislikeCount: comment.feedback.dislike,
likeValue: comment.liked,
dislikeValue: comment.disliked,
onFeedback: (like, dislike, likeCount, dislikeCount) =>
onFeedback:
(like, dislike, likeCount, dislikeCount) =>
state.feedback(
id: _comment.id,
like: like,
@ -228,6 +231,7 @@ class CommentState extends State<Comment> {
onTap: () {
state.deleteComment(
comment.id,
comment.status,
comment.runtimeType == Reply ? _comment.id : null,
);
ActionSheetUtils.pop();