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

feat: 설문이 있을 때 핸들링 #491

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
Binary file added public/images/result/empty.webp
Binary file not shown.
14 changes: 9 additions & 5 deletions src/components/header/MobileHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ import { HEAD_1_BOLD } from '~/styles/typo';
// NOTE: MobileHeader 임시 네이밍, 추후 수정 필요
interface Props {
title: string;
hasMenu?: boolean;
}

function MobileHeader(props: Props) {
function MobileHeader({ title, hasMenu = true }: Props) {
const [isSideMenuOpen, toggleSideMenu] = useBoolean(false);

return (
<>
<header css={headerCss}>
<h1>{props.title}</h1>
<button type="button" css={menuButtonCss} onClick={toggleSideMenu}>
<MenuBarIcon color="#394258" />
</button>
<h1>{title}</h1>

{hasMenu && (
<button type="button" css={menuButtonCss} onClick={toggleSideMenu}>
<MenuBarIcon color="#394258" />
</button>
)}
</header>
<div css={blankCss} />
<SideMenu isOpen={isSideMenuOpen} onClose={toggleSideMenu} />
Expand Down
74 changes: 74 additions & 0 deletions src/pages/result/base.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { type FC, useEffect } from 'react';
import Image from 'next/image';
import { useRouter } from 'next/router';
import { css, type Theme } from '@emotion/react';

import BottomBar from '~/components/bottomBar/BottomBar';
import Button from '~/components/button/Button';
import MobileHeader from '~/components/header/MobileHeader';
import FixedSpinner from '~/components/loading/FixedSpinner';
import LoadingHandler from '~/components/loading/LoadingHandler';
import useGetFeedbackSummaryBySurveyId from '~/hooks/api/feedbacks/useGetFeedbackSummaryBySurveyId';
import useGetSurveyIdByUserStatus from '~/hooks/api/surveys/useGetSurveyIdByUserStatus';
import { HEAD_2_BOLD } from '~/styles/typo';

const ResultBasePage: FC = () => {
const { data, isLoading } = useGetSurveyIdByUserStatus();

return (
<LoadingHandler isLoading={isLoading} fallback={<FixedSpinner />}>
{data && <WhenSurveyIdLoaded surveyId={data.survey_id} />}
</LoadingHandler>
);
};

export default ResultBasePage;

interface WhenSurveyIdLoadedProps {
surveyId: string;
}

const WhenSurveyIdLoaded: FC<WhenSurveyIdLoadedProps> = ({ surveyId }) => {
const router = useRouter();
const { data } = useGetFeedbackSummaryBySurveyId(surveyId);

useEffect(
function replaceWhenHasFeedback() {
if (!data) return;
if (data.all_feedback_count > 0) router.replace('/result');
},
[data, router],
);

return (
<>
<main css={mainCss}>
<MobileHeader title="연구 결과" hasMenu={false} />
<Image css={imageCss} alt="빈 폴더" src="/images/result/empty.webp" width={212} height={162} />
<span css={spanCss}>아직 도착한 피드백이 없어요</span>
<Button color="blue">질문 폼 공유하기</Button>
</main>
<BottomBar />
</>
);
};

const mainCss = css`
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;

width: 100%;
height: 100dvh;
`;

const imageCss = css`
margin-bottom: 30px;
`;

const spanCss = (theme: Theme) => css`
${HEAD_2_BOLD};
margin-bottom: 32px;
color: ${theme.colors.gray_400};
`;
21 changes: 19 additions & 2 deletions src/pages/survey/base.page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { useEffect } from 'react';
import { type NextPage } from 'next';
import Image from 'next/image';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { css, type Theme } from '@emotion/react';

import BottomBar from '~/components/bottomBar/BottomBar';
import Button from '~/components/button/Button';
import FixedSpinner from '~/components/loading/FixedSpinner';
import LoadingHandler from '~/components/loading/LoadingHandler';
import useGetSurveyIdByUserStatus from '~/hooks/api/surveys/useGetSurveyIdByUserStatus';
import { HEAD_1 } from '~/styles/typo';

const SurveyBasePage: NextPage = () => {
const router = useRouter();
const { data, isLoading } = useGetSurveyIdByUserStatus();
const hasSurvey = Boolean(data?.survey_id);

useEffect(
function replaceWhenHasSurvey() {
if (isLoading) return;
if (hasSurvey) router.replace('/result');
},
[hasSurvey, isLoading, router],
Comment on lines +21 to +25
Copy link
Member

Choose a reason for hiding this comment

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

�useEffect 안의 함수에 이름을 지정해둔 이유가 있을까요? 👀

Copy link
Member Author

Choose a reason for hiding this comment

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

오호호 이건 모던 리액트 딥다이브에 나온 내용이기도 한데요

제 생각과 함께 설명드리자면

이펙트에 기명함수를 쓰면 좋은 점은

  1. 이펙트의 역할 명시 (그냥 이펙트만 있으면 해당 이펙트의 역할을 코드를 읽어야 알 수 있음)
  2. 메모리 디버깅 시 이점 (익명 함수는 찾기가 어려워 어느 함수인지 알지못함)

요런 이유가 있습니다 ~

그래서 간단한 이펙트가 아니면 전 ㅁ쓰려고 하곤해용

);

return (
<>
<LoadingHandler isLoading={isLoading} fallback={<FixedSpinner />}>
<main css={mainCss}>
<h1 css={h1Css}>
질문 폼을 생성하고
Expand All @@ -27,7 +44,7 @@ const SurveyBasePage: NextPage = () => {
</main>

<BottomBar />
</>
</LoadingHandler>
);
};

Expand Down
Loading