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

View File

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