Bookmarks and fast reactions

This commit is contained in:
aeroreyna
2022-03-08 21:16:38 -08:00
parent 2be81cc9d3
commit 9d464e45bc
2 changed files with 31 additions and 15 deletions

View File

@@ -11,10 +11,9 @@ let Comment = ({ comment, postid, viewer }) => {
let [likes, changeLikes] = useState(Object.keys(comment.reactions).length);
const newCommentReaction = () => {
if (!comment.reactions[viewer._id]) {
API.newCommentReaction(postid, comment.createdAt).then(() => {
comment.reactions[viewer._id] = { type: "like" };
changeLikes(likes+1);
});
API.newCommentReaction(postid, comment.createdAt);
} else {
//API.removePostReaction(viewer._id).then(() => {
// delete comment.reactions[viewer._id];

View File

@@ -12,7 +12,8 @@ let Post = (props) => {
const viewer = props.viewer;
let [showCommentsB, changeshowCommentsB] = useState(false);
let [post, changePost] = useState(props.post);
let [likes, changeLikes] = useState(Object.keys(post.reactions).length)
let [likes, changeLikes] = useState(Object.keys(post.reactions).length);
let [bookmarked, changeBookmarked] = useState(post.bookmarks && post.bookmarks.includes(viewer._id));
let toProfileText = post.toProfile && post.toProfile !== post.profileid ?
<Text> {">"} <UserName profileid={post.toProfile} /></Text> : undefined;
let cleanContent = post.content.replace(/@[A-z]+:.+\w/g, '');
@@ -24,20 +25,30 @@ let Post = (props) => {
const newPostReaction = () => {
let newPostObj = { ...post };
if (!newPostObj.reactions[viewer._id]) {
API.newPostReaction(post._id).then(() => {
newPostObj.reactions[viewer._id] = { type: "like" };
changeLikes(likes + 1);
});
newPostObj.reactions[viewer._id] = { type: "like" };
API.newPostReaction(post._id);
} else {
API.removePostReaction(viewer._id).then(() => {
delete newPostObj.reactions[viewer._id];
changeLikes(likes - 1);
});
delete newPostObj.reactions[viewer._id];
API.removePostReaction(viewer._id);
}
changePost(newPostObj);
}
const newPostBookmark = () => {
if (!post.bookmarks || !post.bookmarks.includes(viewer._id)) {
changeBookmarked(true);
if (!post.bookmarks) post.bookmarks = [];
post.bookmarks.push(viewer._id);
API.newPostBookmark(post._id);
} else {
changeBookmarked(false);
post.bookmarks = post.bookmarks.filter(id => id != viewer._id);
API.removePostReaction(post._id)
}
}
const renderComment = ({ item }) => (
<Comment comment={item} viewer={viewer} />
<Comment comment={item} viewer={viewer} postid={post._id} />
);
return (
<Card style={styles.card}>
@@ -61,7 +72,13 @@ let Post = (props) => {
</Button>
<Button icon="forum" labelStyle={{ fontSize: 18 }} style={{ flow: 1 }} onPress={() => { changeshowCommentsB(!showCommentsB) }} >{post.comments.length}</Button>
<Button icon="ios-share" style={{ flow: 1 }} labelStyle={{ fontSize: 18 }}></Button>
<Button icon={!post.bookmarks || !post.bookmarks.includes(viewer._id) ? "bookmark-outline" : "bookmark"} style={{ flow: 1 }} labelStyle={{ fontSize: 18 }}></Button>
<Button
icon={!bookmarked ? "bookmark-outline" : "bookmark"}
style={{ flow: 1 }}
labelStyle={{ fontSize: 18 }}
onPress={newPostBookmark}
>
</Button>
</Card.Actions>
{showCommentsB && <NewComment postid={post._id} newComentAdded={newComentAdded} />}
{