Skip to content

Commit

Permalink
refactor(pages/form): move MyProfilePage, extract meta from MyProfile…
Browse files Browse the repository at this point in the history
…Page
  • Loading branch information
ooooorobo committed Jul 3, 2024
1 parent a90875a commit 24ee563
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta, StoryObj } from '@storybook/react';
import { MyProfilePage } from 'src/pages/my_profile/MyProfilePage';
import { MyProfilePage } from 'src/pages/form/my_profile/MyProfilePage';
import { useMyProfileStore } from 'src/entities/profile/model/myProfileStore';
import { useProfileFirstName } from 'src/entities/profile/lib/useProfileFirstName';

Expand Down
44 changes: 44 additions & 0 deletions src/pages/form/my_profile/MyProfilePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useState } from 'react';
import { Button } from 'src/shared/ui/Button/Button';
import { ArrowLeft } from 'src/shared/ui/icons';
import styles from 'src/pages/form/my_profile/MyProfilePage.module.css';
import { useProfileFirstName } from 'src/entities/profile/lib/useProfileFirstName';
import { useMyProfileStore } from 'src/entities/profile/model/myProfileStore';
import { MyProfileStepMeta } from 'src/pages/form/my_profile/MyProfileStepMeta';

const Steps = Object.values(MyProfileStepMeta);

export const MyProfilePage = () => {
const [currentStepIdx, setCurrentStep] = useState(0);
const name = useProfileFirstName();

const currentStep = Steps[currentStepIdx];
const canGoNext = useMyProfileStore(currentStep.canGoNext);

return (
<div className={styles.Container}>
<header className={styles.Header}>
<div className={styles.HeaderBar}>
<ArrowLeft type={'button'} onClick={() => setCurrentStep((prev) => Math.max(0, prev - 1))} />
<span>
{currentStepIdx + 1}/{Steps.length}
</span>
</div>
<h2>{currentStep.title({ name })}</h2>
{currentStep.description && <small className={styles.Description}>{currentStep.description()}</small>}
</header>
<main className={styles.Main}>{currentStep.form}</main>
<footer className={styles.Footer}>
<Button
variant={'filled'}
widthType={'fill'}
color={'primary'}
disabled={!canGoNext}
onClick={() => setCurrentStep((prev) => Math.min(prev + 1, Steps.length - 1))}
>
다음
</Button>
</footer>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ReactElement, ReactNode, useState } from 'react';
import { Button } from 'src/shared/ui/Button/Button';
import { ArrowLeft } from 'src/shared/ui/icons';
import { StepMeta } from 'src/shared/types/FormStepMeta';
import { PersonalInfoForm } from 'src/processes/my_profile/PersonalInfoForm/PersonalInfoForm';
import { isValidDate } from 'src/shared/vo/date';
import { MyImageForm } from 'src/processes/my_profile/MyImageForm/MyImageForm';
import { MbtiForm } from 'src/processes/my_profile/MbtiForm/MbtiForm';
import { JobForm } from 'src/processes/my_profile/JobForm/JobForm';
Expand All @@ -10,19 +9,8 @@ import { ReligionForm } from 'src/processes/my_profile/ReligionForm/ReligionForm
import { HobbyForm } from 'src/processes/my_profile/HobbyForm/HobbyForm';
import { SmokeAlcoholForm } from 'src/processes/my_profile/SmokeAlcoholForm/SmokeAlcoholForm';
import { IntroduceForm } from 'src/processes/my_profile/IntroduceForm/IntroduceForm';
import styles from './MyProfilePage.module.css';
import { useProfileFirstName } from 'src/entities/profile/lib/useProfileFirstName';
import { MyProfile, useMyProfileStore } from 'src/entities/profile/model/myProfileStore';
import { isValidDate } from 'src/shared/vo/date';

type StepMeta = {
title: ({ name }: { name: string }) => ReactNode;
description?: () => ReactNode;
form: ReactElement;
canGoNext: (state: MyProfile) => boolean;
};

const Step: Record<string, StepMeta> = {
export const MyProfileStepMeta: Record<string, StepMeta> = {
PERSONAL_INFO: {
title: () => (
<>
Expand Down Expand Up @@ -110,40 +98,3 @@ const Step: Record<string, StepMeta> = {
canGoNext: () => true,
},
};

const Steps = Object.values(Step);

export const MyProfilePage = () => {
const [currentStepIdx, setCurrentStep] = useState(0);
const name = useProfileFirstName();

const currentStep = Steps[currentStepIdx];
const canGoNext = useMyProfileStore(currentStep.canGoNext);

return (
<div className={styles.Container}>
<header className={styles.Header}>
<div className={styles.HeaderBar}>
<ArrowLeft type={'button'} onClick={() => setCurrentStep((prev) => Math.max(0, prev - 1))} />
<span>
{currentStepIdx + 1}/{Steps.length}
</span>
</div>
<h2>{currentStep.title({ name })}</h2>
{currentStep.description && <small className={styles.Description}>{currentStep.description()}</small>}
</header>
<main className={styles.Main}>{currentStep.form}</main>
<footer className={styles.Footer}>
<Button
variant={'filled'}
widthType={'fill'}
color={'primary'}
disabled={!canGoNext}
onClick={() => setCurrentStep((prev) => Math.min(prev + 1, Steps.length - 1))}
>
다음
</Button>
</footer>
</div>
);
};
9 changes: 9 additions & 0 deletions src/shared/types/FormStepMeta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ReactElement, ReactNode } from 'react';
import { MyProfile } from 'src/entities/profile/model/myProfileStore';

export type StepMeta = {
title: ({ name }: { name: string }) => ReactNode;
description?: () => ReactNode;
form: ReactElement;
canGoNext: (state: MyProfile) => boolean;
};

0 comments on commit 24ee563

Please sign in to comment.