Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(web-domains): result 페이지 meetingId를 params로 처리 및 기존 코드 걷어내기 #136

Merged
merged 4 commits into from
Aug 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { MostSelectedListScreen } from '@sambad/web-domains/result';

interface QuestionResultPageProps {
params: {
meetingId: string;
questionId: string;
};
}

const QuestionResultPage = (props: QuestionResultPageProps) => {
const {
params: { questionId },
} = props;
const { params } = props;

return <MostSelectedListScreen questionId={Number(questionId)} />;
return <MostSelectedListScreen {...params} />;
};

export default QuestionResultPage;
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { AnswerListScreen } from '@sambad/web-domains/result';

interface QuestionResultPageProps {
params: {
meetingId: string;
questionId: string;
};
}

const AnswersPage = (props: QuestionResultPageProps) => {
const {
params: { questionId },
} = props;
const { params } = props;

return <AnswerListScreen questionId={Number(questionId)} />;
return <AnswerListScreen {...params} />;
};

export default AnswersPage;
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { SelectedSameListScreen } from '@sambad/web-domains/result';

interface QuestionResultPageProps {
params: {
meetingId: string;
questionId: string;
};
}

const QuestionResultPage = (props: QuestionResultPageProps) => {
const {
params: { questionId },
} = props;
const { params } = props;

return <SelectedSameListScreen questionId={Number(questionId)} />;
return <SelectedSameListScreen {...params} />;
};

export default QuestionResultPage;
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { ResultMainScreen } from '@sambad/web-domains/result';

interface QuestionResultPageProps {
params: {
meetingId: string;
questionId: string;
};
}

const QuestionResultPage = (props: QuestionResultPageProps) => {
const {
params: { questionId },
} = props;
const { params } = props;

return <ResultMainScreen questionId={Number(questionId)} />;
return <ResultMainScreen {...params} />;
};

export default QuestionResultPage;
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { StatisticsScreen } from '@sambad/web-domains/result';

interface QuestionResultPageProps {
params: {
meetingId: string;
questionId: string;
};
}

const StatisticsPage = (props: QuestionResultPageProps) => {
const {
params: { questionId },
} = props;
const { params } = props;

return <StatisticsScreen questionId={Number(questionId)} />;
return <StatisticsScreen {...params} />;
};

export default StatisticsPage;

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useParams } from 'next/navigation';

type Params = {
[key: string]: string | string[];
};

const convertValue = (value: string | number | string[]) => {
if (Array.isArray(value)) {
return value.map((item) => (!isNaN(Number(item)) ? Number(item) : item));
}
return value !== undefined && !isNaN(Number(value)) ? Number(value) : value;
};

export const useConvertTypeParams = <T extends Params>() => {
const params = useParams<T>();

const converted = Object.entries(params).map(([key, value]) => [key, convertValue(value)]);

return Object.fromEntries(converted);
};
7 changes: 4 additions & 3 deletions packages/web-domains/src/result/common/types/BaseParams.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface BaseParams {
questionId: number;
}
export type BaseParams = {
questionId: string;
meetingId: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import { Fragment } from 'react';

import { ActionBar } from '@/common/components/ActionBar/ActionBar';
import { useGetAnswers } from '@/result/common/apis/queries/useGetAnswers';
import { useGetMeetings } from '@/result/common/apis/queries/useGetMeetings';
import { useConvertTypeParams } from '@/result/common/hooks/useConvertTypeParams';
import { BaseParams } from '@/result/common/types/BaseParams';

import { MemberList } from '../components';

export const AnswerListContainer = (params: BaseParams) => {
const { questionId } = params;
const { data: meetingsIdsData } = useGetMeetings();
// NOTE: 현재 스팩에서는 하나의 모임에만 가입할 수 있습니다.
const meetingId = meetingsIdsData?.meetings[0]?.meetingId || -1;
export const AnswerListContainer = () => {
const { meetingId, questionId } = useConvertTypeParams<BaseParams>();
const { data } = useGetAnswers({ meetingId, questionId });

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
import { Fragment } from 'react';

import { ActionBar } from '@/common/components/ActionBar/ActionBar';
import { useGetMeetings } from '@/result/common/apis/queries/useGetMeetings';
import { useGetMostSelected } from '@/result/common/apis/queries/useGetMostSelected';
import { useConvertTypeParams } from '@/result/common/hooks/useConvertTypeParams';
import { BaseParams } from '@/result/common/types/BaseParams';

import { MemberList } from '../components';

export const MostSelectedListContainer = (params: BaseParams) => {
const { questionId } = params;
const { data: meetingsIdsData } = useGetMeetings();
// NOTE: 현재 스팩에서는 하나의 모임에만 가입할 수 있습니다.
const meetingId = meetingsIdsData?.meetings[0]?.meetingId || -1;
export const MostSelectedListContainer = () => {
const { meetingId, questionId } = useConvertTypeParams<BaseParams>();
const { data } = useGetMostSelected({ meetingId, questionId });

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
import { Fragment } from 'react';

import { ActionBar } from '@/common/components/ActionBar/ActionBar';
import { useGetMeetings } from '@/result/common/apis/queries/useGetMeetings';
import { useGetSameSelected } from '@/result/common/apis/queries/useGetSameSelected';
import { useConvertTypeParams } from '@/result/common/hooks/useConvertTypeParams';
import { BaseParams } from '@/result/common/types/BaseParams';

import { MemberList } from '../components';

export const SelectedSameListContainer = (params: BaseParams) => {
const { questionId } = params;
const { data: meetingsIdsData } = useGetMeetings();
// NOTE: 현재 스팩에서는 하나의 모임에만 가입할 수 있습니다.
const meetingId = meetingsIdsData?.meetings[0]?.meetingId || -1;
export const SelectedSameListContainer = () => {
const { meetingId, questionId } = useConvertTypeParams<BaseParams>();
const { data } = useGetSameSelected({ meetingId, questionId });

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { dehydrate, HydrationBoundary, QueryClient } from '@tanstack/react-query';

import { getAnswersPrefetch } from '@/result/common/apis/queries/useGetAnswers';
import { getMeetingsPrefetch, MEETINGS_QUERY_KEY } from '@/result/common/apis/queries/useGetMeetings';
import { MeetingResponse } from '@/result/common/apis/schema/MeetingResponse';
import { BaseParams } from '@/result/common/types/BaseParams';

import { AnswerListContainer } from '../containers/AnswerListContainer';
Expand All @@ -12,7 +10,7 @@ export const AnswerListScreen = async (params: BaseParams) => {

return (
<HydrationBoundary state={dehydrate(queryClient)}>
<AnswerListContainer {...params} />
<AnswerListContainer />
</HydrationBoundary>
);
};
Expand All @@ -21,15 +19,11 @@ const getServerSideProps = async (params: BaseParams) => {
const queryClient = new QueryClient();

try {
await getMeetingsPrefetch({ queryClient });
const data = queryClient.getQueryData<MeetingResponse>([MEETINGS_QUERY_KEY]);
const meetingId = data?.meetings[0]?.meetingId;

if (!meetingId) {
throw new Error('No meetingId found');
}

await getAnswersPrefetch({ queryClient, meetingId, questionId: params.questionId });
await getAnswersPrefetch({
queryClient,
meetingId: Number(params.meetingId),
questionId: Number(params.questionId),
});
} catch (error) {
console.error(error);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { dehydrate, HydrationBoundary, QueryClient } from '@tanstack/react-query';

import { getMeetingsPrefetch, MEETINGS_QUERY_KEY } from '@/result/common/apis/queries/useGetMeetings';
import { getMostSelectedPrefetch } from '@/result/common/apis/queries/useGetMostSelected';
import { MeetingResponse } from '@/result/common/apis/schema/MeetingResponse';
import { BaseParams } from '@/result/common/types/BaseParams';

import { MostSelectedListContainer } from '../containers/MostSelectedListContainer';
Expand All @@ -12,7 +10,7 @@ export const MostSelectedListScreen = async (params: BaseParams) => {

return (
<HydrationBoundary state={dehydrate(queryClient)}>
<MostSelectedListContainer {...params} />
<MostSelectedListContainer />
</HydrationBoundary>
);
};
Expand All @@ -21,17 +19,11 @@ const getServerSideProps = async (params: BaseParams) => {
const queryClient = new QueryClient();

try {
await getMeetingsPrefetch({ queryClient });
const data = queryClient.getQueryData<MeetingResponse>([MEETINGS_QUERY_KEY]);
const meetingId = data?.meetings[0]?.meetingId;

if (!meetingId) {
throw new Error('No meetingId found');
}

const prefetchParams = { queryClient, meetingId, questionId: params.questionId };

await getMostSelectedPrefetch(prefetchParams);
await getMostSelectedPrefetch({
queryClient,
meetingId: Number(params.meetingId),
questionId: Number(params.questionId),
});
} catch (error) {
console.error(error);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { dehydrate, HydrationBoundary, QueryClient } from '@tanstack/react-query';

import { getMeetingsPrefetch, MEETINGS_QUERY_KEY } from '@/result/common/apis/queries/useGetMeetings';
import { getSameSelectedPrefetch } from '@/result/common/apis/queries/useGetSameSelected';
import { MeetingResponse } from '@/result/common/apis/schema/MeetingResponse';
import { BaseParams } from '@/result/common/types/BaseParams';

import { SelectedSameListContainer } from '../containers/SelectedSameListContainer';
Expand All @@ -12,7 +10,7 @@ export const SelectedSameListScreen = async (params: BaseParams) => {

return (
<HydrationBoundary state={dehydrate(queryClient)}>
<SelectedSameListContainer {...params} />
<SelectedSameListContainer />
</HydrationBoundary>
);
};
Expand All @@ -21,17 +19,11 @@ const getServerSideProps = async (params: BaseParams) => {
const queryClient = new QueryClient();

try {
await getMeetingsPrefetch({ queryClient });
const data = queryClient.getQueryData<MeetingResponse>([MEETINGS_QUERY_KEY]);
const meetingId = data?.meetings[0]?.meetingId;

if (!meetingId) {
throw new Error('No meetingId found');
}

const prefetchParams = { queryClient, meetingId, questionId: params.questionId };

await getSameSelectedPrefetch(prefetchParams);
await getSameSelectedPrefetch({
queryClient,
meetingId: Number(params.meetingId),
questionId: Number(params.questionId),
});
} catch (error) {
console.error(error);
}
Expand Down
Loading
Loading