Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions apps/client/src/pages/community/community-edit/loader.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 말한 loader 는 createBrowserRouter 레벨에 있는 React Router 에 loader 에용

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createBrowserRouter 아래 protectedRoutes 안의 community-edit 라우트에 loader를 연결해서 페이지 컴포넌트 렌더 전에 작성자 권한을 확인하도록 구현했는데 지욱님이 말씀하시는건 community-edit과 같은 개별 라우트 레벨이 아니라 createBrowserRouter에서 ProtectedRoute가 있는 상위 route 객체 레벨에 loader를 두는 방향인가욥??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가로 작성자 권한 체크는 현재 기준으로 community-edit 라우트에서만 사용되고 있어서 우선은 상위 라우트 레벨이 아니라 community-edit 개별 라우트의 loader에서 처리 하는게 좋을것 같다고 생각했었어요!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞는 것 같아요 depth 가 추가로 들어가는 구조 자체가 좀 불편하긴한데 지금 상황에선 이렇게 구현 하는게 맞을 것 같아용

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { LoaderFunctionArgs, redirect } from 'react-router';

import { isPostAuthor } from '@widgets/community/utils/is-post-author';

import { COMMUNITY_QUERY_OPTIONS } from '@shared/api/domain/community/queries';
import { USER_QUERY_OPTIONS } from '@shared/api/domain/queries';
import { routePath } from '@shared/router/path';
import { queryClient } from '@shared/utils/query-client';

export const communityEditLoader = async ({ params }: LoaderFunctionArgs) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오오오 LoaderFunctionArgs 이런것도 있군요... react router loader 함수가 받는 params 인자 타입을 정의해주는 기능 처음 알아갑니다 !!

const postId = params.postId;

if (!postId) {
throw new Error('글 수정 페이지에서 postId를 찾을 수 없습니다.');
}

const [feedDetailData, userData] = await Promise.all([
queryClient.ensureQueryData(COMMUNITY_QUERY_OPTIONS.FEED_DETAIL(postId)),
queryClient.ensureQueryData(USER_QUERY_OPTIONS.PROFILE()),
]);
Comment on lines +17 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise.all을 쓰신 이유가 두개의 요청을 동시에 시작하기 위해서인가요? 만약에 한쪽 요청이 실패했을 때 전체 loader가 실패하는 동작도 의도하신 걸까요? JW입니당..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise.all을 쓰신 이유가 두개의 요청을 동시에 시작하기 위해서인가요?

네 맞아요! 두 쿼리를 Promise.all 없이 사용하면 FEED_DETAIL 먼저 받아오고 PROFILE을 받아오니 순차적으로 호출하면 시간이 더 딜레이 될것 같아요. 어차피 독립적인 요청이니 한번에 처리한게 맞습니다


if (!isPostAuthor(feedDetailData?.writerId, userData?.data?.userId)) {
return redirect(routePath.COMMUNITY_DETAIL.replace(':postId', postId));
}

return null;
};
8 changes: 4 additions & 4 deletions apps/client/src/pages/home/home-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { FeaturesSection } from '@widgets/home/components/features-section/featu
import { InfoSection } from '@widgets/home/components/info-section/info-section.tsx';
import { RecommendedInfoSection } from '@widgets/home/components/info-section/recommended-info-section.tsx';

import { HOME_QUERY_OPTIONS } from '@shared/api/domain/home/queries.ts';
import { USER_QUERY_OPTIONS } from '@shared/api/domain/queries.ts';
import { routePath } from '@shared/router/path.ts';

import * as styles from './home-page.css.ts';
Expand All @@ -20,7 +20,7 @@ const HomePage = () => {
navigate(path);
};

const { data: userData } = useSuspenseQuery(HOME_QUERY_OPTIONS.USER_INFO());
const { data: userData } = useSuspenseQuery(USER_QUERY_OPTIONS.PROFILE());

return (
<section className={styles.homePage}>
Expand All @@ -41,9 +41,9 @@ const HomePage = () => {
/>
}
/>
{userData?.isRecommendInsurance ? (
{userData?.data?.isRecommendInsurance ? (
<>
<RecommendedInfoSection userName={userData?.username} />
<RecommendedInfoSection userName={userData?.data.username} />
<FeaturesSection height={'md'} />
</>
) : (
Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/pages/my/my-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Icon } from '@bds/ui/icons';

import Body from '@widgets/mypage/components/body/body';

import { USER_QUERY_OPTIONS } from '@shared/api/domain/mypage/queries';
import { USER_QUERY_OPTIONS } from '@shared/api/domain/queries';
import { useNavigateTo } from '@shared/hooks/use-navigate-to';
import { routePath } from '@shared/router/path';

Expand Down
9 changes: 5 additions & 4 deletions apps/client/src/pages/onboarding/onboarding-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import {
import { buildSubmitPayload } from '@widgets/onboarding/utils/build-submit-payload';

import {
INSURANCE_QUERY_OPTIONS,
usePostUserInfo,
USER_QUERY_OPTIONS,
} from '@shared/api/domain/onboarding/queries';
import { USER_QUERY_OPTIONS } from '@shared/api/domain/queries';
import { SwitchCase } from '@shared/components/switch-case';
import { useFunnel } from '@shared/hooks/use-funnel';
import { routePath } from '@shared/router/path';
Expand Down Expand Up @@ -69,12 +70,12 @@ const OnboardingPage = () => {
const progressTotal = stepSlugs.filter((s) => !excluded.includes(s)).length;

const { data: userData } = useSuspenseQuery(USER_QUERY_OPTIONS.PROFILE());
const { data: userJobs } = useSuspenseQuery(USER_QUERY_OPTIONS.JOBS());
const { data: userJobs } = useSuspenseQuery(INSURANCE_QUERY_OPTIONS.JOBS());
const { data: userDiseases } = useSuspenseQuery(
USER_QUERY_OPTIONS.DISEASES(),
INSURANCE_QUERY_OPTIONS.DISEASES(),
);
const { data: userCoverages } = useSuspenseQuery(
USER_QUERY_OPTIONS.COVERAGES(),
INSURANCE_QUERY_OPTIONS.COVERAGES(),
);

const { mutate } = usePostUserInfo(() => {
Expand Down
6 changes: 2 additions & 4 deletions apps/client/src/pages/report/report-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import { Icon } from '@bds/ui/icons';
import ReportDetail from '@widgets/report/components/report-detail/report-detail';
import Summarize from '@widgets/report/components/summarize/summarize';

import {
INSURANCE_QUERY_OPTIONS,
USER_QUERY_OPTIONS,
} from '@shared/api/domain/report/queries';
import { USER_QUERY_OPTIONS } from '@shared/api/domain/queries';
import { INSURANCE_QUERY_OPTIONS } from '@shared/api/domain/report/queries';
import { routePath } from '@shared/router/path';

import * as styles from './report-page.css';
Expand Down
18 changes: 2 additions & 16 deletions apps/client/src/shared/api/domain/home/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { queryOptions } from '@tanstack/react-query';

import { END_POINT } from '@shared/api/config/end-point.ts';
import { api } from '@shared/api/config/instance';
import { HOME_QUERY_KEY, USER_QUERY_KEY } from '@shared/api/keys/query-key.ts';
import { ReportSummaryRes, UserProfile } from '@shared/api/types/types';
import { HOME_QUERY_KEY } from '@shared/api/keys/query-key.ts';
import { ReportSummaryRes } from '@shared/api/types/types';

export const HOME_QUERY_OPTIONS = {
REPORT_SUMMARY: () => {
Expand All @@ -13,13 +13,6 @@ export const HOME_QUERY_OPTIONS = {
select: (data: ReportSummaryRes | null) => data?.data,
});
},
USER_INFO: () => {
return queryOptions({
queryKey: USER_QUERY_KEY.PROFILE(),
queryFn: getUserProfile,
select: (data: UserProfile | null) => data?.data,
});
},
};

export const getReportSummary = async (): Promise<ReportSummaryRes | null> => {
Expand All @@ -28,10 +21,3 @@ export const getReportSummary = async (): Promise<ReportSummaryRes | null> => {
.json<ReportSummaryRes>();
return response;
};

export const getUserProfile = async (): Promise<UserProfile | null> => {
const response = await api
.get(END_POINT.USER.GET_USER_INFO)
.json<UserProfile>();
return response;
};
56 changes: 21 additions & 35 deletions apps/client/src/shared/api/domain/mypage/queries.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import {
infiniteQueryOptions,
mutationOptions,
queryOptions,
} from '@tanstack/react-query';
import { infiniteQueryOptions, mutationOptions } from '@tanstack/react-query';

import { END_POINT } from '@shared/api/config/end-point.ts';
import { api } from '@shared/api/config/instance';
import {
AUTH_MUTATION_KEY,
USER_MUTATION_KEY,
USER_QUERY_KEY,
} from '@shared/api/keys/query-key.ts';
import {
KakaoLogoutResponse,
KakaoWithdrawResponse,
MePostResponse,
UserProfile,
UserProfileEditRequestBody,
UserProfileEditResponse,
} from '@shared/api/types/types';
Expand All @@ -24,12 +20,6 @@ import {
// =============================================================================

export const USER_QUERY_OPTIONS = {
PROFILE: () =>
queryOptions({
queryKey: USER_QUERY_KEY.PROFILE(),
queryFn: getUserProfile,
}),

ME_POSTS: () =>
infiniteQueryOptions({
queryKey: USER_QUERY_KEY.ME_POSTS(),
Expand All @@ -53,13 +43,6 @@ export const USER_QUERY_OPTIONS = {
// QUERY FUNCTIONS
// =============================================================================

export const getUserProfile = async (): Promise<UserProfile | null> => {
const response = await api
.get(END_POINT.USER.GET_USER_INFO)
.json<UserProfile>();
return response;
};

export const getMePosts = async ({ pageParam }: { pageParam: number }) => {
const url =
pageParam === 0
Expand All @@ -85,32 +68,42 @@ export const getMeComments = async ({ pageParam }: { pageParam: number }) => {
// =============================================================================

export const USER_MUTATION_OPTIONS = {
PATCH_USER_PROFILE: () => {
return mutationOptions({
mutationKey: USER_MUTATION_KEY.USER_PROFILE(),
mutationFn: ({ body }: { body: UserProfileEditRequestBody }) =>
patchUserProfile(body),
});
},
};

export const AUTH_MUTATION_OPTIONS = {
KAKAO_LOGOUT: () => {
return mutationOptions({
mutationKey: USER_QUERY_KEY.KAKAO_LOGOUT(),
mutationKey: AUTH_MUTATION_KEY.KAKAO_LOGOUT(),
mutationFn: kakaoLogout,
});
},

KAKAO_WITHDRAW: () => {
return mutationOptions({
mutationKey: USER_QUERY_KEY.KAKAO_WITHDRAW(),
mutationKey: AUTH_MUTATION_KEY.KAKAO_WITHDRAW(),
mutationFn: kakaoWithdraw,
});
},
PATCH_USER_PROFILE: () => {
return mutationOptions({
mutationKey: USER_MUTATION_KEY.USER_PROFILE(),
mutationFn: ({ body }: { body: UserProfileEditRequestBody }) =>
patchUserProfile(body),
});
},
};

// =============================================================================
// MUTATION FUNCTIONS
// =============================================================================

export const patchUserProfile = async (data: UserProfileEditRequestBody) => {
const response = await api
.patch(END_POINT.USER.PATCH_USER_INFO, { json: data })
.json<UserProfileEditResponse>();
return response;
};

export const kakaoLogout = async (redirectUrl: string) => {
const response = await api
.post(`${END_POINT.AUTH.KAKAO_LOGOUT}?redirect-url=${redirectUrl}`)
Expand All @@ -124,10 +117,3 @@ export const kakaoWithdraw = async () => {
.json<KakaoWithdrawResponse>();
return response;
};

export const patchUserProfile = async (data: UserProfileEditRequestBody) => {
const response = await api
.patch(END_POINT.USER.PATCH_USER_INFO, { json: data })
.json<UserProfileEditResponse>();
return response;
};
16 changes: 1 addition & 15 deletions apps/client/src/shared/api/domain/onboarding/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,9 @@ import {
UserInfoOptions,
UserInfoSubmitRequest,
UserInfoSubmitResponse,
UserProfile,
} from '@shared/api/types/types';

export const USER_QUERY_OPTIONS = {
PROFILE: () => {
return queryOptions({
queryKey: USER_QUERY_KEY.PROFILE(),
queryFn: getUserProfile,
});
},
export const INSURANCE_QUERY_OPTIONS = {
JOBS: () => {
return queryOptions({
queryKey: USER_QUERY_KEY.JOBS(),
Expand Down Expand Up @@ -57,13 +50,6 @@ export const usePostUserInfo = (onSuccessCallback?: () => void) => {
});
};

export const getUserProfile = async (): Promise<UserProfile | null> => {
const response = await api
.get(END_POINT.USER.GET_USER_INFO)
.json<UserProfile>();
return response;
};

export const getUserInfoJobs = async (): Promise<UserInfoJobs | null> => {
const response = await api
.get(END_POINT.USER.GET_USER_INFO_JOBS)
Expand Down
28 changes: 27 additions & 1 deletion apps/client/src/shared/api/domain/queries.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
import { queryOptions } from '@tanstack/react-query';
import ky from '@toss/ky';

import { USER_QUERY_KEY } from '@shared/api/keys/query-key';

import { END_POINT } from '../config/end-point';
import { api } from '../config/instance';
import { ImageUploadResponse } from '../types/types';
import { ImageUploadResponse, UserProfile } from '../types/types';

// =============================================================================
// QUERY OPTIONS
// =============================================================================

export const USER_QUERY_OPTIONS = {
PROFILE: () =>
queryOptions({
queryKey: USER_QUERY_KEY.PROFILE(),
queryFn: getUserProfile,
}),
};

// =============================================================================
// QUERY FUNCTIONS
// =============================================================================

export const getUserProfile = async (): Promise<UserProfile | null> => {
const response = await api
.get(END_POINT.USER.GET_USER_INFO)
.json<UserProfile>();
return response;
};

// =============================================================================
// MUTATION FUNCTIONS
Expand Down
22 changes: 1 addition & 21 deletions apps/client/src/shared/api/domain/report/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { queryOptions } from '@tanstack/react-query';

import { END_POINT } from '@shared/api/config/end-point';
import { api } from '@shared/api/config/instance';
import {
INSURANCE_QUERY_KEY,
USER_QUERY_KEY,
} from '@shared/api/keys/query-key';
import { INSURANCE_QUERY_KEY } from '@shared/api/keys/query-key';
import {
InsuranceIpwonReport,
InsuranceJanghaeReport,
Expand All @@ -14,7 +11,6 @@ import {
InsuranceSamangReport,
InsuranceSummary,
InsuranceSusulReport,
UserProfile,
} from '@shared/api/types/types';

export const INSURANCE_QUERY_OPTIONS = {
Expand Down Expand Up @@ -63,15 +59,6 @@ export const INSURANCE_QUERY_OPTIONS = {
},
};

export const USER_QUERY_OPTIONS = {
PROFILE: () => {
return queryOptions({
queryKey: USER_QUERY_KEY.PROFILE(),
queryFn: getUserProfile,
});
},
};

export const getInsuranceReport = async (
reportId: string,
): Promise<InsuranceReport | null> => {
Expand Down Expand Up @@ -148,10 +135,3 @@ export const getInsuranceSamangReport = async (
.json<InsuranceSamangReport>();
return response;
};

export const getUserProfile = async (): Promise<UserProfile | null> => {
const response = await api
.get(END_POINT.USER.GET_USER_INFO)
.json<UserProfile>();
return response;
};
2 changes: 0 additions & 2 deletions apps/client/src/shared/api/keys/query-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export const USER_QUERY_KEY = {
DISEASES: () => [...USER_QUERY_KEY.ALL, 'diseases'],
COVERAGES: () => [...USER_QUERY_KEY.ALL, 'coverages'],
OPTIONS: () => [...USER_QUERY_KEY.ALL, 'options'],
KAKAO_LOGOUT: () => [...USER_QUERY_KEY.ALL, 'kakao-logout'],
KAKAO_WITHDRAW: () => [...USER_QUERY_KEY.ALL, 'kakao-withdraw'],
} as const;

export const USER_MUTATION_KEY = {
Expand Down
2 changes: 2 additions & 0 deletions apps/client/src/shared/router/routes/global-routes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import CommunityEdit from '@pages/community/community-edit/community-edit';
import { communityEditLoader } from '@pages/community/community-edit/loader';
import CommunitySearch from '@pages/community/community-search/community-search';
import SplashPage from '@pages/splash/splash-page.tsx';

Expand Down Expand Up @@ -47,6 +48,7 @@ export const protectedRoutes = [
{
path: routePath.COMMUNITY_EDIT,
Component: CommunityEdit,
loader: communityEditLoader,
},
{
path: routePath.COMMUNITY_SEARCH,
Expand Down
Loading
Loading