Skip to content

Commit 6ad28e6

Browse files
authored
Merge pull request #410 from boostcampwm-2024/feat/redirect-to-detail-from-search
🐛 fix: 검색시 디테일 페이지로 이동
2 parents e4542d9 + ca327d0 commit 6ad28e6

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

client/src/components/search/SearchResults/SearchResultItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { CommandItem } from "@/components/ui/command";
44
import { useSearchStore } from "@/store/useSearchStore";
55
import { SearchResult } from "@/types/search";
66

7-
export default function SearchResultItem({ title, blogName, path }: SearchResult) {
7+
export default function SearchResultItem({ id, title, blogName }: SearchResult) {
88
const { searchParam } = useSearchStore();
99
return (
1010
<CommandItem className="flex flex-col items-start">
11-
<a href={path} className="hover:underline">
11+
<a href={`${id}`} className="hover:underline">
1212
<p className=" text-sm text-500">
1313
<SearchHighlight text={title} highlight={searchParam} />
1414
</p>

client/src/hooks/common/usePostCardActions.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ export const usePostCardActions = (post: Post) => {
2323
const incrementView = ({ post, isWindowOpened }: PostWithState): PostWithState => {
2424
if (isWindowOpened) {
2525
mutate(undefined, {
26-
onSuccess: () => {
27-
console.log("조회수 증가 성공");
28-
},
26+
onSuccess: () => {},
2927
onError: (error) => {
3028
console.error("조회수 증가 실패", error);
3129
},
@@ -41,3 +39,17 @@ export const usePostCardActions = (post: Post) => {
4139

4240
return { openPost, incrementView };
4341
};
42+
export function useIncrementViewByPostId(id: number) {
43+
const { mutate } = usePostViewIncrement(id);
44+
45+
const increment = () => {
46+
mutate(undefined, {
47+
onSuccess: () => {},
48+
onError: (error) => {
49+
console.error("조회수 증가 실패", error);
50+
},
51+
});
52+
};
53+
54+
return increment;
55+
}

client/src/pages/PostDetailPage.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRef } from "react";
1+
import { useEffect, useRef } from "react";
22
import { useParams } from "react-router-dom";
33

44
import { PostContent } from "@/components/common/Card/detail/PostContent";
@@ -8,16 +8,25 @@ import Header from "@/components/layout/Header";
88
import Loading from "@/pages/Loading";
99
import NotFound from "@/pages/NotFound";
1010

11+
import { useIncrementViewByPostId } from "@/hooks/common/usePostCardActions";
1112
import { usePostDetail } from "@/hooks/queries/usePostDetail";
1213

1314
export default function PostDetailPage() {
1415
const { id } = useParams();
15-
if (id && !/^\d+$/.test(id)) {
16+
17+
const numericId = Number(id);
18+
const increment = useIncrementViewByPostId(numericId);
19+
const { data, isLoading, error } = usePostDetail(numericId);
20+
const modalRef = useRef<HTMLDivElement>(null);
21+
22+
useEffect(() => {
23+
increment();
24+
}, [id]);
25+
26+
if (!id || !/^\d+$/.test(id)) {
1627
return <NotFound />;
1728
}
1829

19-
const { data, isLoading, error } = usePostDetail(Number(id));
20-
const modalRef = useRef<HTMLDivElement>(null);
2130
if (isLoading) {
2231
return <Loading />;
2332
}

0 commit comments

Comments
 (0)