Skip to content

Commit 43f93f1

Browse files
authored
Feat: total user count api 연결 및 쿼리 무효화 로직 수정 (#195)
* feat: user count api 연결 및 캐싱 재정비 * refactor: 코드리뷰 반영 * refactor: 코드리뷰 반영
1 parent b0c7f15 commit 43f93f1

9 files changed

Lines changed: 69 additions & 13 deletions

File tree

app/(main)/_components/home-caption-section/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import ShakeYMotion from "@/shared/ui/motion/shakeY-motion";
22
import * as styles from "./home-caption-section.css";
33

4-
const HomeCaptionSection = () => {
4+
interface HomeCaptionSectionProps {
5+
totalUserCount: number;
6+
}
7+
8+
const HomeCaptionSection = ({ totalUserCount }: HomeCaptionSectionProps) => {
59
return (
610
<ShakeYMotion>
7-
<p className={styles.caption}>7명이 이야기를 주고 받는 중</p>
11+
<p className={styles.caption}>
12+
{totalUserCount}명이 이야기를 주고 받는 중
13+
</p>
814
</ShakeYMotion>
915
);
1016
};

app/(main)/page.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ import HomeCaptionSection from "./_components/home-caption-section";
77
import HomeTitleSection from "./_components/home-title-section";
88

99
import FloatingStarsContainer from "@/shared/ui/floating-stars-container";
10+
import { useQuery } from "@tanstack/react-query";
11+
import { userQueryOptions } from "@/shared/api/queries/user";
1012

1113
import * as styles from "./home.css";
1214

1315
const Home = () => {
16+
const { data } = useQuery(
17+
userQueryOptions.totalUserCount()
18+
);
19+
20+
const totalUserCount = data?.result.userTotalCount;
21+
1422
return (
1523
<div>
1624
<main>
@@ -26,7 +34,7 @@ const Home = () => {
2634
/>
2735
<HomeTitleSection />
2836
<HomeButtonSection />
29-
<HomeCaptionSection />
37+
<HomeCaptionSection totalUserCount={totalUserCount || 0} />
3038
<div className={styles.physicsContainer}>
3139
<Physics
3240
gravX={0}

app/(sub)/capsule-detail/_components/write-modal/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { CapsuleDetailRes } from "@/shared/types/api/capsule";
1313
import type { WriteLetterReq } from "@/shared/types/api/letter";
1414
import { formatOpenDateString } from "@/shared/utils/date";
1515
import Image from "next/image";
16-
import { useState, useEffect } from "react";
16+
import { useState } from "react";
1717
import { useForm, useController } from "react-hook-form";
1818
import Modal from "../modal";
1919
import { useImageUpload } from "./_hooks/use-image-upload";

shared/api/mutations/capsule.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const useCreateCapsule = () => {
1717
});
1818
},
1919
onSuccess: () => {
20-
queryClient.invalidateQueries({ queryKey: capsuleQueryKeys.all() });
20+
queryClient.invalidateQueries({ queryKey: ["capsule", "my"] });
2121
},
2222
});
2323
};
@@ -31,9 +31,15 @@ export const useLikeToggle = () => {
3131
} else {
3232
await apiClient.put(ENDPOINTS.LIKE_TOGGLE(id));
3333
}
34+
return id;
3435
},
35-
onSuccess: () => {
36-
queryClient.invalidateQueries({ queryKey: capsuleQueryKeys.all() });
36+
onSuccess: async (id: string) => {
37+
await Promise.all([
38+
queryClient.invalidateQueries({
39+
queryKey: capsuleQueryKeys.detail(id),
40+
}),
41+
queryClient.invalidateQueries({ queryKey: ["capsule", "my"] }),
42+
]);
3743
},
3844
});
3945
};
@@ -44,8 +50,13 @@ export const useLeaveCapsule = () => {
4450
mutationFn: (id: string) => {
4551
return apiClient.delete(ENDPOINTS.LEAVE_CAPSULE(id));
4652
},
47-
onSuccess: () => {
48-
queryClient.invalidateQueries({ queryKey: capsuleQueryKeys.all() });
53+
onSuccess: async (_data, id: string) => {
54+
await Promise.all([
55+
queryClient.invalidateQueries({ queryKey: ["capsule", "my"] }),
56+
queryClient.invalidateQueries({
57+
queryKey: capsuleQueryKeys.detail(id),
58+
}),
59+
]);
4960
},
5061
});
5162
};

shared/api/queries/capsule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ const getCapsuleLists = (
9696
const getSearchCapsuleLists = (
9797
keyword: string,
9898
page?: number,
99-
size?: number,
99+
size?: number
100100
) => {
101101
return apiClient.get<CapsuleListsRes>(
102-
ENDPOINTS.CAPSULE_SEARCH_LISTS(keyword, page, size),
102+
ENDPOINTS.CAPSULE_SEARCH_LISTS(keyword, page, size)
103103
);
104104
};
105105

shared/api/queries/user.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { apiClient } from "@/shared/api/api-client";
22
import { ENDPOINTS } from "@/shared/constants/endpoints";
3-
import type { UserInfo } from "@/shared/types/api/user";
3+
import type { UserInfo, TotalUserCountRes } from "@/shared/types/api/user";
44
import { queryOptions } from "@tanstack/react-query";
5+
import { CACHE_TIME } from "@/shared/constants/api";
56

67
const userQueryKeys = {
78
all: () => ["user"],
89
userInfo: () => [...userQueryKeys.all(), "userInfo"],
10+
totalUserCount: () => [...userQueryKeys.all(), "totalUserCount"],
911
} as const;
1012

1113
export const userQueryOptions = {
@@ -20,4 +22,17 @@ export const userQueryOptions = {
2022
},
2123
...options,
2224
}),
25+
26+
totalUserCount: (options?: {
27+
enabled?: boolean;
28+
onError?: (error: Error) => void;
29+
}) =>
30+
queryOptions({
31+
queryKey: userQueryKeys.totalUserCount(),
32+
queryFn: () => {
33+
return apiClient.get<TotalUserCountRes>(ENDPOINTS.TOTAL_USER_COUNT);
34+
},
35+
...options,
36+
staleTime: CACHE_TIME.LONG,
37+
}),
2338
};

shared/constants/api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ export const HTTP_STATUS_CODE = {
77
CONFLICT: 409,
88
INTERNAL_SERVER_ERROR: 500,
99
} as const;
10+
11+
export const CACHE_TIME = {
12+
SHORT: 1000 * 60 * 3,
13+
MEDIUM: 1000 * 60 * 5,
14+
LONG: 1000 * 60 * 10,
15+
} as const;

shared/constants/endpoints.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export const ENDPOINTS = {
1414
type === "all" ? "" : `&type=${type}`
1515
}`,
1616
CAPSULE_SEARCH_LISTS: (keyword: string, page = 0, size = 20) =>
17-
`api/v1/capsules/search?keyword=${encodeURIComponent(keyword)}&page=${page}&size=${size}`,
17+
`api/v1/capsules/search?keyword=${encodeURIComponent(
18+
keyword
19+
)}&page=${page}&size=${size}`,
1820

1921
LEAVE_CAPSULE: (id: string) => `api/v1/capsules/${id}/leave`,
2022
LIKE_TOGGLE: (id: string) => `api/v1/capsules/${id}/like`,
@@ -26,7 +28,9 @@ export const ENDPOINTS = {
2628
) =>
2729
`api/v1/capsules/my?page=${page}&size=${size}&sort=${sort}&filter=${filter}`,
2830

31+
// 유저
2932
USER_INFO: "api/v1/users/my-info",
33+
TOTAL_USER_COUNT: "api/v1/users/total-count",
3034

3135
// 편지
3236
WRITE_LETTER: "api/v1/letters",

shared/types/api/user.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ export interface UserInfo {
55
email: string;
66
};
77
}
8+
9+
export interface TotalUserCountRes {
10+
result: {
11+
userTotalCount: number;
12+
};
13+
}

0 commit comments

Comments
 (0)