Skip to content

Commit 6b2416b

Browse files
Api(client): 아티클 읽음 상태 변경 API 연결 (#96)
* feat: card onClick interface 추가 및 연결 * feat: 아티클 읽음 상태 변경 API axios 및 query 함수 작성 * api: 아티클 읽음 상태 변경 API 연결
1 parent 9709082 commit 6b2416b

File tree

8 files changed

+108
-26
lines changed

8 files changed

+108
-26
lines changed

apps/client/src/pages/myBookmark/MyBookmark.tsx

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,26 @@ import { useAnchoredMenu } from '@shared/hooks/useAnchoredMenu';
1313
import { belowOf } from '@shared/utils/anchorPosition';
1414
import NoArticles from '@pages/myBookmark/components/NoArticles/NoArticles';
1515
import { Icon } from '@pinback/design-system/icons';
16+
import { useQueryClient } from '@tanstack/react-query';
17+
import { usePutArticleReadStatus } from '@shared/apis/queries';
1618

1719
const MyBookmark = () => {
1820
const [activeBadge, setActiveBadge] = useState<'all' | 'notRead'>('all');
21+
const [isEditOpen, setIsEditOpen] = useState(false);
22+
1923
const [searchParams] = useSearchParams();
2024
const category = searchParams.get('category');
2125
const categoryId = searchParams.get('id');
22-
const [isEditOpen, setIsEditOpen] = useState(false);
26+
27+
const queryClient = useQueryClient();
28+
const { data: articles } = useGetBookmarkArticles(0, 20);
29+
const { data: unreadArticles } = useGetBookmarkUnreadArticles(0, 20);
30+
const { data: categoryArticles } = useGetCategoryBookmarkArticles(
31+
categoryId,
32+
1,
33+
10
34+
);
35+
const { mutate: updateToReadStatus } = usePutArticleReadStatus();
2336

2437
const {
2538
state: menu,
@@ -32,14 +45,6 @@ const MyBookmark = () => {
3245
const getBookmarkTitle = (id: number | null) =>
3346
id == null ? '' : (REMIND_MOCK_DATA.find((d) => d.id === id)?.title ?? '');
3447

35-
const { data: articles } = useGetBookmarkArticles(0, 20);
36-
const { data: unreadArticles } = useGetBookmarkUnreadArticles(0, 20);
37-
const { data: categoryArticles } = useGetCategoryBookmarkArticles(
38-
categoryId,
39-
1,
40-
10
41-
);
42-
4348
const articlesToDisplay =
4449
activeBadge === 'all' ? articles?.articles : unreadArticles?.articles;
4550

@@ -93,7 +98,27 @@ const MyBookmark = () => {
9398
content={article.memo}
9499
category={article.category.categoryName}
95100
date={new Date(article.createdAt).toLocaleDateString('ko-KR')}
96-
onClick={() => {}}
101+
onClick={() => {
102+
window.open(article.url, '_blank');
103+
104+
updateToReadStatus(article.articleId, {
105+
onSuccess: () => {
106+
// TODO: 쿼리키 팩토리 패턴 적용
107+
queryClient.invalidateQueries({
108+
queryKey: ['bookmarkReadArticles'],
109+
});
110+
queryClient.invalidateQueries({
111+
queryKey: ['bookmarkUnreadArticles'],
112+
});
113+
queryClient.invalidateQueries({
114+
queryKey: ['categoryBookmarkArticles'],
115+
});
116+
},
117+
onError: (error) => {
118+
console.error(error);
119+
},
120+
});
121+
}}
97122
onOptionsClick={(e) =>
98123
openMenu(article.articleId, e.currentTarget)
99124
}

apps/client/src/pages/remind/Remind.tsx

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,23 @@ import { useGetRemindArticles } from '@pages/remind/apis/queries';
99
import { formatLocalDateTime } from '@shared/utils/formatDateTime';
1010
import NoReadArticles from '@pages/remind/components/noReadArticles/NoReadArticles';
1111
import NoUnreadArticles from '@pages/remind/components/noUnreadArticles/NoUnreadArticles';
12+
import { usePutArticleReadStatus } from '@shared/apis/queries';
13+
import { useQueryClient } from '@tanstack/react-query';
1214

1315
const Remind = () => {
1416
const [isEditOpen, setIsEditOpen] = useState(false);
17+
const [activeBadge, setActiveBadge] = useState<'read' | 'notRead'>('notRead');
18+
const formattedDate = formatLocalDateTime();
19+
20+
const queryClient = useQueryClient();
21+
22+
const { mutate: updateToReadStatus } = usePutArticleReadStatus();
23+
const { data } = useGetRemindArticles(
24+
formattedDate,
25+
activeBadge === 'read',
26+
1,
27+
10
28+
);
1529

1630
const {
1731
state: menu,
@@ -23,15 +37,6 @@ const Remind = () => {
2337

2438
const getItemTitle = (id: number | null) =>
2539
id == null ? '' : (REMIND_MOCK_DATA.find((d) => d.id === id)?.title ?? '');
26-
const [activeBadge, setActiveBadge] = useState<'read' | 'notRead'>('notRead');
27-
const formattedDate = formatLocalDateTime();
28-
29-
const { data } = useGetRemindArticles(
30-
formattedDate,
31-
activeBadge === 'read',
32-
1,
33-
10
34-
);
3540

3641
const handleBadgeClick = (badgeType: 'read' | 'notRead') => {
3742
setActiveBadge(badgeType);
@@ -72,6 +77,20 @@ const Remind = () => {
7277
onOptionsClick: (e) =>
7378
openMenu(article.category.categoryId, e.currentTarget),
7479
})}
80+
onClick={() => {
81+
window.open(article.url, '_blank');
82+
83+
updateToReadStatus(article.articleId, {
84+
onSuccess: () => {
85+
queryClient.invalidateQueries({
86+
queryKey: ['remindArticles'],
87+
});
88+
},
89+
onError: (error) => {
90+
console.error(error);
91+
},
92+
});
93+
}}
7594
/>
7695
))}
7796
</div>

apps/client/src/shared/apis/axios.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ export const postSignUp = async (responsedata: postSignUpRequest) => {
3939
return data;
4040
};
4141

42+
export const putArticleReadStatus = async (articleId: number) => {
43+
const { data } = await apiRequest.put(
44+
`/api/v1/articles/${articleId}/readStatus`
45+
);
46+
return data;
47+
};
48+
4249
export const deleteCategory = async (id: number) => {
4350
const response = await apiRequest.delete(`/api/v1/categories/${id}`);
4451
return response;

apps/client/src/shared/apis/queries.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
import { useMutation, useQuery, UseQueryResult } from '@tanstack/react-query';
1+
import {
2+
useMutation,
3+
UseMutationResult,
4+
useQuery,
5+
UseQueryResult,
6+
} from '@tanstack/react-query';
27
import {
38
deleteCategory,
49
getDashboardCategories,
510
postCategory,
611
postSignUp,
712
postSignUpRequest,
813
putCategory,
14+
getAcorns,
15+
putArticleReadStatus,
916
} from '@shared/apis/axios';
1017
import { AxiosError } from 'axios';
11-
import { DashboardCategoriesResponse, AcornsResponse } from '@shared/types/api';
12-
import { getAcorns } from './axios';
18+
import {
19+
DashboardCategoriesResponse,
20+
AcornsResponse,
21+
ArticleReadStatusResponse,
22+
} from '@shared/types/api';
1323

1424
export const useGetDashboardCategories = (): UseQueryResult<
1525
DashboardCategoriesResponse,
@@ -63,3 +73,13 @@ export const usePostSignUp = () => {
6373
},
6474
});
6575
};
76+
77+
export const usePutArticleReadStatus = (): UseMutationResult<
78+
ArticleReadStatusResponse,
79+
AxiosError,
80+
number
81+
> => {
82+
return useMutation({
83+
mutationFn: (articleId: number) => putArticleReadStatus(articleId),
84+
});
85+
};

apps/client/src/shared/types/api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ export type AcornsResponse = {
1212
acornCount: number;
1313
remindDateTime: string;
1414
};
15+
16+
export interface ArticleReadStatusResponse {
17+
acornCount: number;
18+
acornCollected: boolean;
19+
}

packages/design-system/src/components/card/BaseCard.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
interface BaseCardProps {
2+
onClick?: () => void;
23
children: React.ReactNode;
34
}
45

5-
const BaseCard = ({ children }: BaseCardProps) => {
6+
const BaseCard = ({ children, onClick }: BaseCardProps) => {
67
return (
7-
<div className="border-gray200 w-[24.8rem] overflow-hidden rounded-[1.2rem] border bg-white">
8+
<div
9+
onClick={onClick}
10+
className="border-gray200 w-[24.8rem] overflow-hidden rounded-[1.2rem] border bg-white"
11+
>
812
{children}
913
</div>
1014
);

packages/design-system/src/components/card/MyBookmarkCard.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ const MyBookmarkCard = ({
1818
category,
1919
imageUrl,
2020
date,
21+
onClick,
2122
onOptionsClick,
2223
}: MyBookmarkCardProps) => {
2324
return (
24-
<BaseCard>
25+
<BaseCard onClick={onClick}>
2526
<div className="flex h-[12rem] w-full items-center justify-center overflow-hidden bg-[#F8F8FA]">
2627
{imageUrl ? (
2728
<img src={imageUrl} className="h-full w-full object-cover" />

packages/design-system/src/components/card/RemindCard.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ const RemindCard = ({
1818
category,
1919
imageUrl,
2020
timeRemaining,
21+
onClick,
2122
onOptionsClick,
2223
}: RemindCardProps) => {
2324
return (
24-
<BaseCard>
25+
<BaseCard onClick={onClick}>
2526
<div className="bg-gray900 flex items-center gap-[0.4rem] py-[1.2rem] pl-[1.6rem] text-sm text-white">
2627
<Icon name="ic_clock_active" />
2728
<span className="body2-m text-main400 mr-[0.2rem]">

0 commit comments

Comments
 (0)