Skip to content

Commit ce6bbaa

Browse files
committed
feat(): Adding like page support and refresh the data
1 parent c52f4ba commit ce6bbaa

6 files changed

Lines changed: 24 additions & 10 deletions

File tree

src/screens/articles/components/CommentBox.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { useAddComment, useGetComments } from '@services';
1212
import { useMemo, useState } from 'react';
1313

1414
const CommentBox = ({ articleKey }: { articleKey: string }) => {
15-
const { data, refetch } = useGetComments(articleKey);
15+
const { data } = useGetComments(articleKey);
1616
const { mutate } = useAddComment();
1717

1818
const comments = useMemo(() => (data as any)?.data?.rows, [data]);
@@ -36,7 +36,6 @@ const CommentBox = ({ articleKey }: { articleKey: string }) => {
3636
},
3737
});
3838
setState({ email: '', name: '', comment: '' });
39-
refetch();
4039
};
4140

4241
return (

src/screens/articles/components/MarkdownViewer.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ const MarkdownViewer = ({ articleKey }: { articleKey: string }) => {
3939
<MdPreview mdString={mdString ?? ''} />
4040
<CommentBox articleKey={articleKey} />
4141
</Box>
42-
<SideViewer mdString={mdString ?? ''} likes={likes} views={views} />
42+
<SideViewer
43+
mdString={mdString ?? ''}
44+
likes={likes}
45+
views={views}
46+
articleKey={articleKey}
47+
/>
4348
</HStack>
4449
);
4550
};

src/screens/articles/components/SideViewer.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import { HEADING_TYPE_REGEX } from './constants';
44
import { useLikeArticleData } from '@services';
55

66
const SideViewer = ({
7+
articleKey,
78
mdString,
89
likes,
910
views,
1011
}: {
12+
articleKey: string;
1113
mdString: string;
1214
likes: number;
1315
views: number;
1416
}) => {
15-
const { mutate } = useLikeArticleData();
17+
const { mutate: likeArticle } = useLikeArticleData();
1618
// Extract headings from the markdown string and remove dots.
1719
const headings = useMemo(
1820
() =>
@@ -59,7 +61,7 @@ const SideViewer = ({
5961
<Divider />
6062

6163
<HStack w={'100%'} justifyContent={'space-evenly'}>
62-
<Button size={'sm'} onClick={mutate}>
64+
<Button size={'sm'} onClick={() => likeArticle({ articleKey })}>
6365
Like Article👍
6466
</Button>
6567
<Button
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { PostRequest } from '../client/client';
22
import { LIKE_ARTICLE_URL } from './constants';
33

4-
const getArticlesData = async () => PostRequest(LIKE_ARTICLE_URL, {});
4+
const getArticlesData = async (articleKey: unknown) =>
5+
PostRequest(LIKE_ARTICLE_URL, articleKey);
56

67
export default getArticlesData;

src/services/hooks/articles/useAddComment.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ const useAddComment = () => {
99
return useCallSBMutation({
1010
method: (data) => addComment(data),
1111
mutationOptions: {
12-
onSuccess: () => {
12+
onSuccess: (_data, variables: any) => {
1313
toast({
1414
title: 'Comment added.',
1515
status: 'success',
1616
duration: 3000,
1717
isClosable: true,
1818
});
19-
queryClient.invalidateQueries({ queryKey: ['comments'] });
19+
queryClient.invalidateQueries({
20+
queryKey: ['comments', variables.articleKey],
21+
});
2022
},
2123
onError: () => {
2224
toast({

src/services/hooks/articles/useLikeArticleData.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import { useToast } from '@chakra-ui/react';
22
import { likePageArticleData } from '../../backend';
33
import { useCallSBMutation } from '../common';
4+
import { useQueryClient } from '@tanstack/react-query';
45

56
const useLikeArticleData = () => {
67
const toast = useToast();
8+
const queryClient = useQueryClient();
79
return useCallSBMutation({
8-
method: () => likePageArticleData(),
10+
method: (articleKey) => likePageArticleData(articleKey),
911
mutationOptions: {
10-
onSuccess: () => {
12+
onSuccess: (_data, variable: any) => {
1113
toast({
1214
title: 'Article Liked.',
1315
status: 'success',
1416
duration: 3000,
1517
isClosable: true,
1618
});
19+
queryClient.invalidateQueries({
20+
queryKey: ['article', variable.articleKey],
21+
});
1722
},
1823
onError: () => {
1924
toast({

0 commit comments

Comments
 (0)