Skip to content

Commit

Permalink
feat: 조회수 기능 추가
Browse files Browse the repository at this point in the history
Co-authored-by: 이도현 <[email protected]>
  • Loading branch information
hae-on and Creative-Lee committed Oct 4, 2023
1 parent 426afe0 commit c5ba5b3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions frontend/src/apis/articles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ export const requestGetFilteredArticle = (course: string, bookmark: boolean) =>
export const requestPutArticleLike = ({ articleId, like }: ArticleLikePutRequest) => {
return client.put(`/articles/${articleId}/like`, { like });
};

export const requestPostArticleView = (articleId: number) => {
return client.post(`/articles/${articleId}/views`);
};
4 changes: 3 additions & 1 deletion frontend/src/components/Article/Article.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ArticleType } from '../../models/Article';
import Scrap from '../Reaction/Scrap';
import { useRef, useState } from 'react';
import {
usePostArticleViewsMutation,
usePutArticleBookmarkMutation,
usePutArticleLikeMutation,
} from '../../hooks/queries/article';
Expand All @@ -25,6 +26,7 @@ const Article = ({
const [like, setLike] = useState(isLiked);
const { mutate: putBookmark } = usePutArticleBookmarkMutation();
const { mutate: putLike } = usePutArticleLikeMutation();
const { mutate: postViews } = usePostArticleViewsMutation();

const toggleBookmark: React.MouseEventHandler<HTMLButtonElement> = (e) => {
e.preventDefault();
Expand All @@ -49,7 +51,7 @@ const Article = ({
};

return (
<Styled.Container>
<Styled.Container onClick={() => postViews(id)}>
<Styled.Anchor href={url} target="_blank" rel="noopener noreferrer">
<Styled.ThumbnailWrapper>
<Styled.Thumbnail src={imageUrl} />
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/hooks/queries/article.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMutation, useQuery, useQueryClient } from 'react-query';
import {
requestGetFilteredArticle,
requestPostArticleView,
requestPostArticles,
requestPutArticleBookmark,
requestPutArticleLike,
Expand Down Expand Up @@ -48,3 +49,14 @@ export const usePutArticleLikeMutation = () => {
onError: () => {},
});
};

export const usePostArticleViewsMutation = () => {
return useMutation(requestPostArticleView, {
onSuccess: () => {
alert(SUCCESS_MESSAGE.CREATE_ARTICLE);
},
onError: () => {
alert(ERROR_MESSAGE.DEFAULT);
},
});
};
4 changes: 4 additions & 0 deletions frontend/src/mocks/handlers/articles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ export const articlesHandler = [

return res(ctx.status(200), ctx.json(filteredArticle));
}),

rest.post(`${BASE_URL}/articles/:articleId/views`, (req, res, ctx) => {
return res(ctx.status(200));
}),
];

0 comments on commit c5ba5b3

Please sign in to comment.