Refactor commentsReplies

This commit is contained in:
saltrafael 2021-10-10 08:41:51 -03:00 committed by infinite-persistence
parent f7cb39c496
commit cafefb2a33
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0
2 changed files with 68 additions and 86 deletions

View file

@ -7,7 +7,7 @@ import CommentsReplies from './view';
const select = (state, props) => ({ const select = (state, props) => ({
fetchedReplies: makeSelectRepliesForParentId(props.parentId)(state), fetchedReplies: makeSelectRepliesForParentId(props.parentId)(state),
claimIsMine: makeSelectClaimIsMine(props.uri)(state), claimIsMine: makeSelectClaimIsMine(props.uri)(state),
commentingEnabled: IS_WEB ? Boolean(selectUserVerifiedEmail(state)) : true, userCanComment: IS_WEB ? Boolean(selectUserVerifiedEmail(state)) : true,
myChannels: selectMyChannelClaims(state), myChannels: selectMyChannelClaims(state),
isFetchingByParentId: selectIsFetchingCommentsByParentId(state), isFetchingByParentId: selectIsFetchingCommentsByParentId(state),
}); });

View file

@ -1,8 +1,8 @@
// @flow // @flow
import * as ICONS from 'constants/icons'; import * as ICONS from 'constants/icons';
import React from 'react';
import Comment from 'component/comment';
import Button from 'component/button'; import Button from 'component/button';
import Comment from 'component/comment';
import React from 'react';
import Spinner from 'component/spinner'; import Spinner from 'component/spinner';
type Props = { type Props = {
@ -12,13 +12,13 @@ type Props = {
claimIsMine: boolean, claimIsMine: boolean,
myChannels: ?Array<ChannelClaim>, myChannels: ?Array<ChannelClaim>,
linkedCommentId?: string, linkedCommentId?: string,
commentingEnabled: boolean, userCanComment: boolean,
threadDepth: number, threadDepth: number,
numDirectReplies: number, // Total replies for parentId as reported by 'comment[replies]'. Includes blocked items. numDirectReplies: number, // Total replies for parentId as reported by 'comment[replies]'. Includes blocked items.
isFetchingByParentId: { [string]: boolean }, isFetchingByParentId: { [string]: boolean },
onShowMore?: () => void,
hasMore: boolean, hasMore: boolean,
supportDisabled: boolean, supportDisabled: boolean,
onShowMore?: () => void,
}; };
function CommentsReplies(props: Props) { function CommentsReplies(props: Props) {
@ -29,99 +29,81 @@ function CommentsReplies(props: Props) {
claimIsMine, claimIsMine,
myChannels, myChannels,
linkedCommentId, linkedCommentId,
commentingEnabled, userCanComment,
threadDepth, threadDepth,
numDirectReplies, numDirectReplies,
isFetchingByParentId, isFetchingByParentId,
onShowMore,
hasMore, hasMore,
supportDisabled, supportDisabled,
onShowMore,
} = props; } = props;
const [isExpanded, setExpanded] = React.useState(true); const [isExpanded, setExpanded] = React.useState(true);
function showMore() { return !numDirectReplies ? null : (
if (onShowMore) { <div className="comment__replies-container">
onShowMore(); {!isExpanded ? (
} <div className="comment__actions--nested">
} <Button
className="comment__action"
label={__('Show Replies')}
onClick={() => setExpanded(!isExpanded)}
icon={isExpanded ? ICONS.UP : ICONS.DOWN}
/>
</div>
) : (
<div className="comment__replies">
<Button className="comment__threadline" aria-label="Hide Replies" onClick={() => setExpanded(false)} />
// todo: implement comment_list --mine in SDK so redux can grab with selectCommentIsMine <ul className="comments--replies">
function isMyComment(channelId: string) { {fetchedReplies &&
if (myChannels != null && channelId != null) { fetchedReplies.map((comment) => (
for (let i = 0; i < myChannels.length; i++) { <Comment
if (myChannels[i].claim_id === channelId) { threadDepth={threadDepth}
return true; uri={uri}
} authorUri={comment.channel_url}
} author={comment.channel_name}
} claimId={comment.claim_id}
return false; commentId={comment.comment_id}
} key={comment.comment_id}
message={comment.comment}
const displayedComments = fetchedReplies; timePosted={comment.timestamp * 1000}
claimIsMine={claimIsMine}
return ( commentIsMine={
Boolean(numDirectReplies) && ( comment.channel_id &&
<div className="comment__replies-container"> myChannels &&
{Boolean(numDirectReplies) && !isExpanded && ( myChannels.some(({ claim_id }) => claim_id === comment.channel_id)
}
linkedCommentId={linkedCommentId}
commentingEnabled={userCanComment}
supportAmount={comment.support_amount}
numDirectReplies={comment.replies}
isModerator={comment.is_moderator}
isGlobalMod={comment.is_global_mod}
supportDisabled={supportDisabled}
/>
))}
</ul>
</div>
)}
{isExpanded && fetchedReplies && hasMore && (
<div className="comment__actions--nested">
<Button
button="link"
label={__('Show more')}
onClick={() => onShowMore && onShowMore()}
className="button--uri-indicator"
/>
</div>
)}
{isFetchingByParentId[parentId] && (
<div className="comment__replies-container">
<div className="comment__actions--nested"> <div className="comment__actions--nested">
<Button <Spinner type="small" />
className="comment__action"
label={__('Show Replies')}
onClick={() => setExpanded(!isExpanded)}
icon={isExpanded ? ICONS.UP : ICONS.DOWN}
/>
</div> </div>
)} </div>
{isExpanded && ( )}
<div> </div>
<div className="comment__replies">
<Button className="comment__threadline" aria-label="Hide Replies" onClick={() => setExpanded(false)} />
<ul className="comments--replies">
{displayedComments &&
displayedComments.map((comment) => {
return (
<Comment
threadDepth={threadDepth}
uri={uri}
authorUri={comment.channel_url}
author={comment.channel_name}
claimId={comment.claim_id}
commentId={comment.comment_id}
key={comment.comment_id}
message={comment.comment}
timePosted={comment.timestamp * 1000}
claimIsMine={claimIsMine}
commentIsMine={comment.channel_id && isMyComment(comment.channel_id)}
linkedCommentId={linkedCommentId}
commentingEnabled={commentingEnabled}
supportAmount={comment.support_amount}
numDirectReplies={comment.replies}
isModerator={comment.is_moderator}
isGlobalMod={comment.is_global_mod}
supportDisabled={supportDisabled}
/>
);
})}
</ul>
</div>
</div>
)}
{isExpanded && fetchedReplies && hasMore && (
<div className="comment__actions--nested">
<Button button="link" label={__('Show more')} onClick={showMore} className="button--uri-indicator" />
</div>
)}
{isFetchingByParentId[parentId] && (
<div className="comment__replies-container">
<div className="comment__actions--nested">
<Spinner type="small" />
</div>
</div>
)}
</div>
)
); );
} }