From 51f397132d227edf5e07d48d673ee167d2aa5937 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Tue, 11 May 2021 18:42:53 -0400 Subject: made things faster --- src/components/comments/CommentTile.tsx | 14 +++++++------- src/components/common/LikeButton.tsx | 17 ++++++++--------- 2 files changed, 15 insertions(+), 16 deletions(-) (limited to 'src/components') diff --git a/src/components/comments/CommentTile.tsx b/src/components/comments/CommentTile.tsx index 1f1fafda..ee32f889 100644 --- a/src/components/comments/CommentTile.tsx +++ b/src/components/comments/CommentTile.tsx @@ -55,6 +55,7 @@ const CommentTile: React.FC = ({ const [showReplies, setShowReplies] = useState(false); const [showKeyboard, setShowKeyboard] = useState(false); const [shouldUpdateChild, setShouldUpdateChild] = useState(true); + const [liked, setLiked] = useState(commentObject.user_reaction !== null); const swipeRef = useRef(null); const {replyPosted} = useSelector((state: RootState) => state.user); const state: RootState = useStore().getState(); @@ -150,12 +151,9 @@ const CommentTile: React.FC = ({ screenType={screenType} /> { - handleLikeUnlikeComment(commentObject).then(() => { - setShouldUpdateParent(true); - }); - }} + liked={liked} + setLiked={setLiked} + onPress={() => handleLikeUnlikeComment(commentObject, liked)} style={styles.likeButton} /> @@ -183,7 +181,9 @@ const CommentTile: React.FC = ({ }); }}> - {commentObject.reaction_count} + {commentObject.user_reaction !== null + ? commentObject.reaction_count + (liked ? 0 : -1) + : commentObject.reaction_count + (liked ? 1 : 0)} Likes diff --git a/src/components/common/LikeButton.tsx b/src/components/common/LikeButton.tsx index 43b3ac37..81383eca 100644 --- a/src/components/common/LikeButton.tsx +++ b/src/components/common/LikeButton.tsx @@ -1,28 +1,27 @@ -import React, {useState} from 'react'; +import React from 'react'; import {Image, ImageStyle, StyleSheet, TouchableOpacity} from 'react-native'; import {normalize} from '../../utils'; interface LikeButtonProps { onPress: () => void; style: ImageStyle; - initialLikeState: boolean; + liked: boolean; + setLiked: (liked: boolean) => void; } const LikeButton: React.FC = ({ onPress, style, - initialLikeState, + liked, + setLiked, }) => { - const [filled, setFilled] = useState(initialLikeState); - const uri = filled + const uri = liked ? require('../../assets/images/heart-filled.png') : require('../../assets/images/heart-outlined.png'); return ( { - if (filled === initialLikeState) { - setFilled(!filled); - onPress(); - } + setLiked(!liked); + onPress(); }}> -- cgit v1.2.3-70-g09d2