-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from near/develop
Fix Like Button + Post Preview
- Loading branch information
Showing
11 changed files
with
165 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters