-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: Add Comments tab to User Profile #653
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
93 changes: 93 additions & 0 deletions
93
...ile-page/components/user-profile/components/profile-comment-card/profile-comment-card.tsx
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,93 @@ | ||
/** | ||
* Copyright 2022 Expedia, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { HStack, Stack, Text, Tooltip, VStack } from '@chakra-ui/react'; | ||
|
||
import { ItemTypeIcon } from '../../../../../../components/item-type-icon/item-type-icon'; | ||
import { LikeButton } from '../../../../../../components/like-button/like-button'; | ||
import { LikedByTooltip } from '../../../../../../components/liked-by-tooltip/liked-by-tooltip'; | ||
import { Link } from '../../../../../../components/link/link'; | ||
import { MarkdownContainer } from '../../../../../../components/markdown-container/markdown-container'; | ||
import type { Comment, User } from '../../../../../../models/generated/graphql'; | ||
import { formatDateIntl, formatRelativeIntl } from '../../../../../../shared/date-utils'; | ||
|
||
interface Props { | ||
comment: Comment; | ||
userName: string; | ||
displayName: string; | ||
onFetchLikedBy: (commentId: string) => Promise<User[]>; | ||
onLike: (commentId: string, liked: boolean) => Promise<boolean>; | ||
} | ||
|
||
export const ProfileCommentCard = ({ comment, userName, displayName, onFetchLikedBy, onLike }: Props) => { | ||
const likeLabel = comment.viewerHasLiked ? 'Unlike this comment' : 'Like this comment'; | ||
|
||
const toggleLike = async (liked: boolean) => { | ||
return onLike(comment.id, liked); | ||
}; | ||
|
||
return ( | ||
<VStack spacing="1rem" align="stretch"> | ||
<Stack direction={{ base: 'column', md: 'row' }}> | ||
<Stack direction="row"> | ||
{' '} | ||
<ItemTypeIcon itemType={comment?.insight.itemType ?? 'insight'} /> | ||
<Link to={`/${comment?.insight.itemType}/${comment?.insight.fullName}`} display="inline-block"> | ||
<Text fontWeight="bold" fontSize="md" display="inline-block"> | ||
{comment?.insight.name} | ||
</Text> | ||
</Link> | ||
</Stack> | ||
|
||
<Tooltip | ||
label={formatDateIntl(comment.createdAt)} | ||
aria-label={`Occurred at ${formatDateIntl(comment.createdAt)}`} | ||
> | ||
<Text fontSize="sm" color="polar.600" flexShrink={0}> | ||
{formatRelativeIntl(comment.createdAt)} | ||
</Text> | ||
</Tooltip> | ||
|
||
<Text color="polar.600" fontSize="sm"> | ||
{comment.isEdited && ( | ||
<Tooltip label="This comment was edited" aria-label="This comment was edited"> | ||
{' (edited)'} | ||
</Tooltip> | ||
)} | ||
</Text> | ||
|
||
<HStack spacing="0.25rem" flexGrow={1} justify="flex-end" align="center"> | ||
<LikedByTooltip | ||
label={likeLabel} | ||
likeCount={comment.likeCount} | ||
onFetchLikedBy={() => onFetchLikedBy(comment.id)} | ||
placement="bottom" | ||
> | ||
<LikeButton | ||
label={likeLabel} | ||
liked={comment.viewerHasLiked} | ||
likeCount={comment.likeCount} | ||
onLike={toggleLike} | ||
disabled={comment.isOwnComment} | ||
/> | ||
</LikedByTooltip> | ||
</HStack> | ||
</Stack> | ||
|
||
<MarkdownContainer contents={comment.commentText} /> | ||
</VStack> | ||
); | ||
}; |
128 changes: 128 additions & 0 deletions
128
...src/pages/profile-page/components/user-profile/components/user-comments/user-comments.tsx
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,128 @@ | ||
/** | ||
* Copyright 2022 Expedia, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Flex, StackDivider, Text, useToast, VStack } from '@chakra-ui/react'; | ||
import { gql, useMutation } from 'urql'; | ||
|
||
import type { User, Comment } from '../../../../../../models/generated/graphql'; | ||
import { useLikedBy } from '../../../../../../shared/useLikedBy'; | ||
import { ProfileCommentCard } from '../profile-comment-card/profile-comment-card'; | ||
|
||
const COMMENT_FRAGMENT = gql` | ||
fragment CommentFields on Comment { | ||
id | ||
commentText | ||
createdAt | ||
isEdited | ||
isDeleted | ||
isOwnComment | ||
viewerHasLiked | ||
likeCount | ||
author { | ||
id | ||
userName | ||
displayName | ||
} | ||
childComments { | ||
edges { | ||
node { | ||
id | ||
commentText | ||
createdAt | ||
isEdited | ||
isDeleted | ||
isOwnComment | ||
viewerHasLiked | ||
likeCount | ||
author { | ||
id | ||
userName | ||
displayName | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const LIKE_COMMENT_MUTATION = gql` | ||
${COMMENT_FRAGMENT} | ||
mutation LikeComment($commentId: ID!, $liked: Boolean!) { | ||
likeComment(commentId: $commentId, liked: $liked) { | ||
...CommentFields | ||
} | ||
} | ||
`; | ||
|
||
interface Props { | ||
user: User; | ||
} | ||
|
||
export const UserComments = ({ user }: Props) => { | ||
const toast = useToast(); | ||
|
||
const comments = user.userComments?.edges.map((e) => e.node) || []; | ||
const totalComments = user.userComments?.pageInfo?.total || 0; | ||
|
||
const [, likeComment] = useMutation(LIKE_COMMENT_MUTATION); | ||
|
||
const onLike = async (commentId: string, liked: boolean): Promise<boolean> => { | ||
console.log('Liking comment:', commentId); | ||
const { error } = await likeComment({ | ||
commentId, | ||
liked | ||
}); | ||
|
||
if (error) { | ||
toast({ | ||
position: 'bottom-right', | ||
title: 'Unable to like comment.', | ||
status: 'error', | ||
duration: 9000, | ||
isClosable: true | ||
}); | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
const { onFetchLikedBy } = useLikedBy('comment'); | ||
|
||
return ( | ||
<VStack spacing="1rem" p="1rem" pt={0} align="stretch" divider={<StackDivider borderColor="snowstorm.100" />}> | ||
<Flex mb="0.5rem"> | ||
<Text textTransform="uppercase" fontSize="sm" fontWeight="bold" color="polar.600"> | ||
{totalComments} result{totalComments > 1 && 's'} | ||
</Text> | ||
</Flex> | ||
{comments.map((comment: Comment) => { | ||
return ( | ||
<ProfileCommentCard | ||
comment={comment} | ||
displayName={user.displayName} | ||
key={comment.id} | ||
userName={user.userName} | ||
onFetchLikedBy={onFetchLikedBy} | ||
onLike={onLike} | ||
/> | ||
); | ||
})} | ||
</VStack> | ||
); | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably need to add
.whereNull('comment.deletedAt')
here.