Skip to content

Commit

Permalink
Merge pull request #199 from near/develop
Browse files Browse the repository at this point in the history
Fix Like Button + Post Preview
  • Loading branch information
roshaans authored Aug 28, 2023
2 parents 74a681f + bfaf2eb commit 167f957
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 35 deletions.
9 changes: 6 additions & 3 deletions src/AccountProfileOverlay.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const Card = styled.div`
const FollowButtonWrapper = styled.div`
width: 100%;
div, button {
div,
button {
width: 100%;
}
`;
Expand All @@ -61,6 +62,7 @@ const overlay = (
src="${REPL_ACCOUNT}/widget/FlagButton"
props={{
item: contentModerationItem,
disabled: !context.accountId || context.accountId === accountId,
onFlag: () => {
State.update({ hasBeenFlagged: true });
},
Expand Down Expand Up @@ -110,12 +112,13 @@ return (
props={{
type: "info",
title: "Flagged for moderation",
description: "Thanks for helping our Content Moderators. The item you flagged will be reviewed.",
description:
"Thanks for helping our Content Moderators. The item you flagged will be reviewed.",
open: state.hasBeenFlagged,
onOpenChange: (open) => {
State.update({ hasBeenFlagged: open });
},
duration: 10000
duration: 10000,
}}
/>
)}
Expand Down
3 changes: 2 additions & 1 deletion src/Comments/Comment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ return (
{blockHeight !== "now" && (
<Actions>
<Widget
src="${REPL_ACCOUNT}/widget/LikeButton"
src="${REPL_ACCOUNT}/widget/v1.LikeButton"
props={{
item: {
type: "social",
Expand Down Expand Up @@ -231,6 +231,7 @@ return (
path: `${accountId}/post/comment`,
blockHeight,
},
disabled: !context.accountId || context.accountId === accountId,
onFlag: () => {
State.update({ hasBeenFlagged: true });
},
Expand Down
2 changes: 1 addition & 1 deletion src/Feed/Post.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ return (
{blockHeight !== "now" && (
<div className="mt-1 d-flex justify-content-between">
<Widget
src="${REPL_MOB_2}/widget/LikeButton"
src="${REPL_ACCOUNT}/widget/v1.LikeButton"
props={{
notifyAccountId,
item,
Expand Down
65 changes: 40 additions & 25 deletions src/FlagButton.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { item } = props;
const { item, disabled } = props;

const Button = styled.button`
border: 0;
Expand All @@ -24,36 +24,51 @@ const Button = styled.button`
outline: none;
color: #11181c;
}
&[disabled] {
color: var(--sand8);
cursor: initial;
}
`;

const FlagButton = () => (
<Button
type="button"
aria-label="Flag for moderation"
disabled={disabled}
onClick={() => {
Social.set(
{
index: {
flag: JSON.stringify({
key: "main",
value: item,
}),
},
},
{
onCommit: () => {
props.onFlag && props.onFlag();
},
}
);
}}
>
<i className="bi bi-flag"></i>
</Button>
);

if (disabled) {
return (
<FlagButton />
);
}

return (
<OverlayTrigger
placement="top"
overlay={<Tooltip>Flag for moderation</Tooltip>}
>
<Button
type="button"
aria-label="Flag for moderation"
disabled={!context.accountId}
onClick={() => {
Social.set(
{
index: {
flag: JSON.stringify({
key: "main",
value: item,
}),
},
},
{
onCommit: () => {
props.onFlag && props.onFlag();
},
}
);
}}
>
<i className="bi bi-flag"></i>
</Button>
<FlagButton />
</OverlayTrigger>
);
2 changes: 1 addition & 1 deletion src/NestedDiscussions/Compose.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ return (
{state.showPreview ? (
<PreviewWrapper>
<Widget
src="${REPL_ACCOUNT}/widget/Posts.Post"
src="${REPL_ACCOUNT}/widget/v1.Posts.Post"
props={{
accountId: context.accountId,
blockHeight: "now",
Expand Down
1 change: 1 addition & 0 deletions src/NestedDiscussions/Preview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ return (
path: `${accountId}/discuss`,
blockHeight,
},
disabled: !context.accountId || context.accountId === accountId,
onFlag: () => {
State.update({ hasBeenFlagged: true });
},
Expand Down
2 changes: 1 addition & 1 deletion src/Posts/Compose.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ return (
{state.showPreview ? (
<PreviewWrapper>
<Widget
src="${REPL_ACCOUNT}/widget/Posts.Post"
src="${REPL_ACCOUNT}/widget/v1.Posts.Post"
props={{
accountId: context.accountId,
blockHeight: "now",
Expand Down
2 changes: 1 addition & 1 deletion src/Posts/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ if (!context.accountId) {
{state.showPreview ? (
<PreviewWrapper>
<Widget
src="${REPL_ACCOUNT}/widget/Posts.Post"
src="${REPL_ACCOUNT}/widget/v1.Posts.Post"
props={{
accountId: context.accountId,
blockHeight: "now",
Expand Down
3 changes: 2 additions & 1 deletion src/Posts/Post.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ return (
{blockHeight !== "now" && (
<Actions>
<Widget
src="${REPL_ACCOUNT}/widget/LikeButton"
src="${REPL_ACCOUNT}/widget/v1.LikeButton"
props={{
item,
notifyAccountId,
Expand Down Expand Up @@ -333,6 +333,7 @@ return (
src="${REPL_ACCOUNT}/widget/FlagButton"
props={{
item,
disabled: !context.accountId || context.accountId === accountId,
onFlag: () => {
State.update({ hasBeenFlagged: true });
},
Expand Down
108 changes: 108 additions & 0 deletions src/v1/LikeButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const item = props.item;

if (!item) {
return "";
}

const likes = Social.index("like", item);

const dataLoading = likes === null;

const likesByUsers = {};

(likes || []).forEach((like) => {
if (like.value.type === "like") {
likesByUsers[like.accountId] = like;
} else if (like.value.type === "unlike") {
delete likesByUsers[like.accountId];
}
});
if (state.hasLike === true) {
likesByUsers[context.accountId] = {
accountId: context.accountId,
};
} else if (state.hasLike === false) {
delete likesByUsers[context.accountId];
}

const accountsWithLikes = Object.keys(likesByUsers);
const hasLike = context.accountId && !!likesByUsers[context.accountId];
const totalLikes = accountsWithLikes.length;

const LikeButton = styled.button`
border: 0 !important;
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 50%;
width: 2.5em;
height: 2.5em;
&:hover {
color: red;
background: pink;
}
.bi-heart-fill {
color: red;
}
`;

const likeClick = () => {
if (state.loading) {
return;
}
State.update({
loading: true,
});
const data = {
index: {
like: JSON.stringify({
key: item,
value: {
type: hasLike ? "unlike" : "like",
},
}),
},
};

if (!hasLike && props.notifyAccountId) {
data.index.notify = JSON.stringify({
key: props.notifyAccountId,
value: {
type: "like",
item,
},
});
}
Social.set(data, {
onCommit: () => State.update({ loading: false, hasLike: !hasLike }),
onCancel: () => State.update({ loading: false }),
});
};

const title = hasLike ? "Unlike" : "Like";

return (
<div className="d-inline-flex align-items-center">
<LikeButton
disabled={state.loading || dataLoading || !context.accountId}
className="btn me-1"
title={title}
onClick={likeClick}
>
{state.loading || dataLoading ? (
<span
className="spinner-grow spinner-grow-sm p-2"
role="status"
aria-hidden="true"
/>
) : (
<i className={`bi ${hasLike ? "bi-heart-fill" : "bi-heart"}`} />
)}
</LikeButton>
{totalLikes == 0 ? (
"0"
) : (
<Widget src="mob.near/widget/LikeButton.Faces" props={{ likesByUsers }} />
)}
</div>
);
3 changes: 2 additions & 1 deletion src/v1/Posts/Post.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ return (
{blockHeight !== "now" && (
<Actions>
<Widget
src="${REPL_ACCOUNT}/widget/LikeButton"
src="${REPL_ACCOUNT}/widget/v1.LikeButton"
props={{
item,
notifyAccountId,
Expand Down Expand Up @@ -208,6 +208,7 @@ return (
src="${REPL_ACCOUNT}/widget/FlagButton"
props={{
item,
disabled: !context.accountId || context.accountId === accountId,
onFlag: () => {
State.update({ hasBeenFlagged: true });
},
Expand Down

0 comments on commit 167f957

Please sign in to comment.