Skip to content

Commit 540809c

Browse files
authored
[25.06.03 / TASK-197] Feature - 총 통계 그래프 API 연동 (#40)
* feature: 임시 퍼블리싱 완료 * refactor: 호버 없이도 정보가 뜨도록 수정 * refactor: 반복된 객체 분리 * modify: 모달 주석 제거 * modify: 총 통계 API 연동 * refactor: id 값 변경 추가로 필요없는 id prop 제거 * refactor: 그래프 일부 영역이 잘리는 문제 해결 * refactor: 타입 변경 * refactor: 타입 정리 다만 네이밍이나 위치 등이 조금 애매하여 나중에 전체적인 리팩토링이 필요할 것으로 보임
1 parent b3c7cbb commit 540809c

File tree

10 files changed

+51
-38
lines changed

10 files changed

+51
-38
lines changed

src/apis/dashboard.request.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { PostDetailDto, PostListDto, PostSummaryDto } from '@/types';
2-
import { PATHS } from '@/constants';
1+
import { PostDetailDto, PostListDto, PostSummaryDto, TotalStatsDto } from '@/types';
2+
import { PATHS, SidebarIdType } from '@/constants';
3+
34
import { instance } from './instance.request';
45

56
type SortType = {
@@ -18,3 +19,6 @@ export const postSummary = async () => await instance<null, PostSummaryDto>(PATH
1819

1920
export const postDetail = async (path: string, start: string, end: string) =>
2021
await instance<null, PostDetailDto>(`${PATHS.DETAIL}/${path}?start=${start}&end=${end}`);
22+
23+
export const totalStats = async (type: SidebarIdType, period: number = 7) =>
24+
await instance<null, TotalStatsDto>(`${PATHS.TOTALSTATS}?period=${period}&type=${type}`);

src/app/(auth-required)/main/components/Summary/BarContent.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ interface IProp {
55
content: number;
66
increasement?: number;
77
prefix?: string;
8-
id?: string;
98
}
109

1110
const afterContent =
1211
'before:text-PRIMARY-SUB before:content-[attr(data-increasement)_"↑"] before:mr-2 before:text-T5';
1312

14-
export const BarContent = ({ title, content, increasement, prefix = '개', id }: IProp) => {
13+
export const BarContent = ({ title, content, increasement, prefix = '개' }: IProp) => {
1514
return (
1615
<div className="w-full flex justify-between items-center">
1716
<span className="text-ST5 text-TEXT-ALT">{title}</span>
1817
<span
1918
className={`flex items-center text-TEXT-MAIN text-T4 ${increasement ? afterContent : ''}`}
2019
data-increasement={parseNumber(increasement ? increasement : 0)}
21-
id={id}
2220
>
2321
{parseNumber(content) + prefix}
2422
</span>

src/app/(auth-required)/main/components/Summary/Modal.tsx

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import {
1010
Legend,
1111
} from 'chart.js';
1212
import ChartDataLabels from 'chartjs-plugin-datalabels';
13+
import { useQuery } from '@tanstack/react-query';
1314
import { Modal as Layout } from '@/components';
14-
import { COLORS } from '@/constants';
15+
import { COLORS, PATHS, sidebarId, SidebarIdType } from '@/constants';
1516
import { graphOptions } from '@/constants/graph.constant';
17+
import { totalStats } from '@/apis/dashboard.request';
18+
import { convertDateToKST } from '@/utils/dateUtil';
1619

1720
ChartJS.register(
1821
CategoryScale,
@@ -25,47 +28,45 @@ ChartJS.register(
2528
ChartDataLabels,
2629
);
2730

28-
export const table = {
29-
totalViews: '전체 조회수 통계',
30-
totalLikes: '전체 좋아요 통계',
31-
totalPosts: '총 게시글 통계',
32-
};
33-
3431
const datasets = {
3532
backgroundColor: COLORS.TEXT.MAIN,
3633
borderColor: COLORS.PRIMARY.MAIN,
3734
};
3835

3936
const defaultData = {
40-
labels: [
41-
'2025-03-01',
42-
'2025-03-02',
43-
'2025-03-03',
44-
'2025-03-04',
45-
'2025-03-05',
46-
'2025-03-06',
47-
'2025-03-07',
48-
],
37+
labels: [],
4938
datasets: [
5039
{
51-
label: 'Temp',
52-
data: [10, 11, 14, 23, 36, 50, 79],
40+
label: 'default',
41+
data: [],
5342
...datasets,
5443
},
5544
],
5645
};
5746

58-
export const Modal = ({ name }: { name: keyof typeof table }) => {
47+
export const Modal = ({ name }: { name: SidebarIdType }) => {
48+
const { data } = useQuery({
49+
queryKey: [PATHS.TOTALSTATS, name],
50+
queryFn: async () => await totalStats(name),
51+
select: (res) => ({
52+
labels: res.map((i) => convertDateToKST(i.date)?.short),
53+
datasets: [{ label: name, data: res.map((i) => i.value), ...datasets }],
54+
}),
55+
});
56+
5957
return (
6058
<Layout
61-
title={table[name]}
62-
className="w-[1000px] max-w-full max-TBL:w-[700px] max-MBI:w-full transition-all overflow-hidden"
59+
title={sidebarId[name]}
60+
className="w-[1000px] max-w-full gap-1 max-TBL:w-[700px] max-MBI:w-full transition-all overflow-hidden"
6361
>
6462
<Line
65-
data={defaultData}
63+
data={data || defaultData}
6664
options={graphOptions}
6765
className="w-full h-[auto_!important] max-h-[300px]"
6866
/>
67+
<span className="text-ST5 self-end text-PRIMARY-MAIN">
68+
* 7일 전까지의 데이터만 표시됩니다
69+
</span>
6970
</Layout>
7071
);
7172
};

src/app/(auth-required)/main/components/Summary/SidebarContent.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { useModal } from '@/hooks';
22
import { parseNumber } from '@/utils/numberUtil';
3-
import { Modal, table } from './Modal';
3+
import { SidebarIdType } from '@/constants';
4+
import { Modal } from './Modal';
45

56
interface IProp {
67
title: string;
78
content: number;
89
increasement?: number;
910
prefix?: string;
10-
id: keyof typeof table;
11+
id: SidebarIdType;
1112
}
1213

1314
const afterContent =

src/app/(auth-required)/main/components/Summary/index.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ export const Summary = ({ totalPostCount, stats }: PostSummaryDto) => {
2020
content={stats?.totalViews}
2121
prefix="회"
2222
increasement={stats?.totalViews - stats?.yesterdayViews}
23-
id="totalViews"
23+
id="view"
2424
/>
2525
<SidebarContent
2626
title="전체 좋아요 수"
2727
content={stats?.totalLikes}
2828
increasement={stats?.totalLikes - stats?.yesterdayLikes}
29-
id="totalLikes"
29+
id="like"
3030
/>
31-
<SidebarContent title="총 게시글 수" content={totalPostCount} id="totalPosts" />
31+
<SidebarContent title="총 게시글 수" content={totalPostCount} id="post" />
3232
</aside>
3333
<section
3434
className={`flex flex-col w-full px-5 bg-BG-SUB rounded-[4px] cursor-pointer MBI:hidden`}
@@ -45,15 +45,13 @@ export const Summary = ({ totalPostCount, stats }: PostSummaryDto) => {
4545
content={stats?.totalViews}
4646
prefix="회"
4747
increasement={stats?.totalViews - stats?.yesterdayViews}
48-
id="totalViews"
4948
/>
5049
<BarContent
5150
title="전체 좋아요 수"
5251
content={stats?.totalLikes}
5352
increasement={stats?.totalLikes - stats?.yesterdayLikes}
54-
id="totalLikes"
5553
/>
56-
<BarContent title="총 게시글 수" content={totalPostCount} id="totalPosts" />
54+
<BarContent title="총 게시글 수" content={totalPostCount} />
5755
</div>
5856
)}
5957
</section>

src/constants/graph.constant.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ export const graphOptions = {
77
interaction: { mode: 'nearest', intersect: false },
88
layout: {
99
padding: {
10-
top: 30,
10+
top: 40,
1111
bottom: 10,
12-
left: 10,
13-
right: 10,
12+
left: 15,
13+
right: 15,
1414
},
1515
},
1616
plugins: {

src/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './searchParams.constant';
2+
export * from './sidebar.constant';
23
export * from './screens.constant';
34
export * from './colors.constant';
45
export * from './sizes.constant';

src/constants/paths.constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export const PATHS = {
1010
ME: '/me',
1111
LOGOUT: '/logout',
1212
DETAIL: '/post',
13+
TOTALSTATS: '/total-stats',
1314
LEADERBOARD: '/leaderboard',
1415
};

src/constants/sidebar.constant.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const sidebarId = {
2+
view: '전체 조회수 통계',
3+
like: '전체 좋아요 통계',
4+
post: '총 게시글 통계',
5+
};
6+
7+
export type SidebarIdType = keyof typeof sidebarId;

src/types/dashboard.type.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ export type PostDetailValue = {
3535
export type PostDetailDto = {
3636
post: PostDetailValue[];
3737
};
38+
39+
export type TotalStatsDto = Array<{ date: string; value: number }>;

0 commit comments

Comments
 (0)