Skip to content

Commit ad6c147

Browse files
authored
feat: prefetch를 적용해요 (#323) (#324)
* feat: prefetch를 적용해요 (#323) * chore: type err fix (#323)
1 parent 66feca6 commit ad6c147

File tree

20 files changed

+252
-43
lines changed

20 files changed

+252
-43
lines changed

services/ahhachul.com/src/apis/request/complaint.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export const fetchComplaintList = async (req: ComplaintListParams) => {
2323
};
2424

2525
export const createComplaint = async (req: ComplaintForm) => {
26-
console.log('req:', req);
2726
const formData = new FormData();
2827
const formDataWithoutImages = extractFormData(req, 'images');
2928
const jsonBlob = createJsonBlob(formDataWithoutImages);
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import axiosInstance from '@/apis/fetcher';
2-
import { ITrain, WithSubwayLineId, WithSubwayStationId, type ApiResponse } from '@/types';
2+
import {
3+
ITrain,
4+
SubwayLineServerModel,
5+
WithSubwayLineId,
6+
WithSubwayStationId,
7+
type ApiResponse,
8+
} from '@/types';
39

410
interface APITrainInfoParams extends WithSubwayLineId, WithSubwayStationId {}
511
interface APITrainInfoResponse {
612
trainRealTimes: ITrain[];
713
}
814

15+
export const fetchSubwayLines = async () =>
16+
await axiosInstance.get<ApiResponse<SubwayLineServerModel>>('/subway-lines');
17+
918
export const fetchTrainInfo = (params: APITrainInfoParams) =>
1019
axiosInstance.get<ApiResponse<APITrainInfoResponse>>('/trains/real-times', { params });

services/ahhachul.com/src/apis/request/user.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
1+
import axios from 'axios';
2+
13
import axiosInstance from '@/apis/fetcher';
24
import type { ApiResponse, UserProfileResponseDto } from '@/types';
5+
import { getAccessTokenInLocalStorage } from '@/utils/localStorage';
6+
7+
import { BASE_URL } from '../baseUrl';
8+
import { API_PREFIX } from '../endpointPrefix';
39

410
export const fetchUserProfile = async () => {
511
const { data } = await axiosInstance.get<ApiResponse<UserProfileResponseDto>>('/members');
612

713
return data;
814
};
915

16+
export const prefetchUserProfile = async () => {
17+
const accessToken = getAccessTokenInLocalStorage();
18+
19+
const { data } = await axios.get<ApiResponse<UserProfileResponseDto>>(
20+
`${BASE_URL.SERVER}${API_PREFIX}/members`,
21+
{
22+
headers: {
23+
Authorization: `Bearer ${accessToken}`,
24+
},
25+
},
26+
);
27+
28+
return data;
29+
};
30+
1031
export const fetchUserFavoriteStations = async () => {
1132
const { data } = await axiosInstance.get<ApiResponse<{ id: 'hello' }>>(
1233
'/members/bookmarks/stations',

services/ahhachul.com/src/components/common/editor/plugins/hooks/useReport.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export function useReport(): (arg0: string) => ReturnType<typeof setTimeout> {
4343
return useCallback(
4444
content => {
4545
// eslint-disable-next-line no-console
46-
console.log(content);
4746
const element = getElement();
4847
if (timer.current !== null) {
4948
clearTimeout(timer.current);

services/ahhachul.com/src/components/domain/complaint/searchResults/searchedList/SearchedList.component.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const ComplaintSearchedList = ({
2323
subwayLineId,
2424
});
2525

26-
const lostArticles = extractInfinitePageData(data);
26+
const complaintArticles = extractInfinitePageData(data);
2727

2828
const throttledFetchNextPage = useThrottle(() => {
2929
if (!isFetchingNextPage) {
@@ -35,11 +35,11 @@ const ComplaintSearchedList = ({
3535
callback: throttledFetchNextPage,
3636
});
3737

38-
if (!lostArticles.length) return <UiComponent.EmptyList />;
38+
if (!complaintArticles.length) return <UiComponent.EmptyList />;
3939

4040
return (
4141
<S.Section isScale={isScale}>
42-
{lostArticles.map((post, idx) => (
42+
{complaintArticles.map((post, idx) => (
4343
<StackFlow.Link
4444
key={`${post.id}${idx}`}
4545
activityName="ComplaintDetailPage"

services/ahhachul.com/src/components/domain/lostFound/postDetail/template/LostFoundDetail.component.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ interface LostFoundDetailProps {
1818
const LostFoundDetail = ({ id }: LostFoundDetailProps) => {
1919
const { data: post } = useFetchLostFoundDetail(id);
2020

21-
console.log('post', post);
22-
2321
const images = post.isFromLost112
2422
? [
2523
{

services/ahhachul.com/src/contexts/tanstack-query.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { type PropsWithChildren } from 'react';
22

33
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
44

5-
const queryClient = new QueryClient({
5+
export const queryClient = new QueryClient({
66
defaultOptions: {
77
queries: {
88
retry: false,

services/ahhachul.com/src/hooks/domain/my/useUser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ApiResponse, UserProfileResponseDto } from '@/types';
55

66
const useUser = () => {
77
const queryClient = useQueryClient();
8-
const data = queryClient.getQueryData<ApiResponse<UserProfileResponseDto>>(userKeys.infos());
8+
const data = queryClient.getQueryData<ApiResponse<UserProfileResponseDto>>(userKeys.info());
99

1010
return { user: data?.result ?? null };
1111
};

services/ahhachul.com/src/main.tsx

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
1-
import { Suspense } from 'react';
2-
import { createRoot } from 'react-dom/client';
1+
import { prefetchUserProfile } from './apis/request';
2+
import { fetchSubwayLines } from './apis/request/subway';
3+
import { queryClient } from './contexts/tanstack-query';
4+
import { subwayKeys } from './services/subway';
5+
import { userKeys } from './services/user';
6+
import { getAccessTokenInLocalStorage } from './utils/localStorage';
37

4-
import App from './App';
8+
async function init() {
9+
await queryClient.prefetchQuery({
10+
queryKey: subwayKeys.subwayLine(),
11+
queryFn: fetchSubwayLines,
12+
staleTime: Infinity,
13+
gcTime: Infinity,
14+
});
515

6-
createRoot(document.getElementById('root')!).render(
7-
<Suspense fallback={null}>
8-
<App />
9-
</Suspense>,
10-
);
16+
const accessToken = getAccessTokenInLocalStorage();
17+
18+
if (accessToken) {
19+
try {
20+
await queryClient.prefetchQuery({
21+
queryKey: userKeys.info(),
22+
queryFn: prefetchUserProfile,
23+
retry: false,
24+
});
25+
} catch (error) {
26+
console.log('Failed to prefetch user profile, continuing...');
27+
}
28+
}
29+
30+
const { render } = await import('./render');
31+
32+
render();
33+
}
34+
35+
init();

services/ahhachul.com/src/pages/my/page.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ import type { ActivityComponentType } from '@stackflow/react';
44

55
import { ChevronIcon } from '@/assets/icons/system';
66
import { HeaderComponent, LayoutComponent } from '@/components';
7+
import { useFetchSubwayLines } from '@/services/subway';
8+
import { useFetchUserProfile } from '@/services/user';
79
import { useFlow } from '@/stackflow';
810

911
const MyPage: ActivityComponentType = () => {
1012
const { push } = useFlow();
13+
const { data } = useFetchSubwayLines();
14+
const { data: userInfo } = useFetchUserProfile();
15+
console.log('useFetchSubwayLines data :', data);
16+
console.log('userInfo data :', userInfo);
1117

1218
return (
1319
<LayoutComponent.Base

0 commit comments

Comments
 (0)