fix: delete reaction uses reaction instead of id

This commit is contained in:
Tim 2023-12-22 10:25:15 +01:00 committed by Freek van de Ven
parent d60244fa3e
commit c8cc325a95
3 changed files with 39 additions and 18 deletions

View file

@ -13,7 +13,7 @@ environment:
dependencies:
flutter:
sdk: flutter
go_router: ^12.1.1
go_router: any
flutter_timeline_view:
git:
url: https://github.com/Iconica-Development/flutter_timeline

View file

@ -65,23 +65,30 @@ class FirebaseTimelineService with ChangeNotifier implements TimelineService {
TimelinePost post,
String reactionId,
) async {
var updatedPost = post.copyWith(
reaction: post.reaction - 1,
reactions: (post.reactions ?? [])
..removeWhere((element) => element.id == reactionId),
);
_posts = _posts
.map(
(p) => p.id == post.id ? updatedPost : p,
)
.toList();
var postRef = _db.collection(_options.timelineCollectionName).doc(post.id);
await postRef.update({
'reaction': FieldValue.increment(-1),
'reactions': FieldValue.arrayRemove([reactionId]),
});
notifyListeners();
return updatedPost;
if (post.reactions != null && post.reactions!.isNotEmpty) {
var reaction =
post.reactions!.firstWhere((element) => element.id == reactionId);
var updatedPost = post.copyWith(
reaction: post.reaction - 1,
reactions: (post.reactions ?? [])..remove(reaction),
);
_posts = _posts
.map(
(p) => p.id == post.id ? updatedPost : p,
)
.toList();
var postRef =
_db.collection(_options.timelineCollectionName).doc(post.id);
await postRef.update({
'reaction': FieldValue.increment(-1),
'reactions': FieldValue.arrayRemove(
[reaction.toJsonWithMicroseconds()],
),
});
notifyListeners();
return updatedPost;
}
return post;
}
@override

View file

@ -15,6 +15,7 @@ class TimelinePostReaction {
this.reaction,
this.imageUrl,
this.creator,
this.createdAtString,
});
factory TimelinePostReaction.fromJson(
@ -29,6 +30,7 @@ class TimelinePostReaction {
reaction: json['reaction'] as String?,
imageUrl: json['image_url'] as String?,
createdAt: DateTime.parse(json['created_at'] as String),
createdAtString: json['created_at'] as String,
);
/// The unique identifier of the reaction.
@ -52,6 +54,9 @@ class TimelinePostReaction {
/// Reaction creation date.
final DateTime createdAt;
/// Reaction creation date as String with microseconds.
final String? createdAtString;
TimelinePostReaction copyWith({
String? id,
String? postId,
@ -79,4 +84,13 @@ class TimelinePostReaction {
'created_at': createdAt.toIso8601String(),
},
};
Map<String, dynamic> toJsonWithMicroseconds() => <String, dynamic>{
id: {
'creator_id': creatorId,
'reaction': reaction,
'image_url': imageUrl,
'created_at': createdAtString,
},
};
}