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: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
go_router: ^12.1.1 go_router: any
flutter_timeline_view: flutter_timeline_view:
git: git:
url: https://github.com/Iconica-Development/flutter_timeline url: https://github.com/Iconica-Development/flutter_timeline

View file

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

View file

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