Skip to content

Commit 7495c72

Browse files
authored
Merge pull request #167 from sebanimm/main
fix: if문을 객체 방식으로 수정
2 parents d96e0ca + 6f348f7 commit 7495c72

File tree

2 files changed

+86
-54
lines changed

2 files changed

+86
-54
lines changed

src/templates/post/layouts/edit/index.tsx

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import { CATEGORY } from "../../constants";
1515
import { usePostWritable } from "../../hooks";
1616
import { PostDetailParamsProps } from "../../types/@props";
1717

18+
type PostCategoryType = {
19+
[key in keyof typeof CATEGORY]?: JSX.Element;
20+
};
21+
1822
const PostEditPage = ({ id }: PostDetailParamsProps) => {
1923
const { post } = usePostQuery(id);
2024
const {
@@ -24,6 +28,44 @@ const PostEditPage = ({ id }: PostDetailParamsProps) => {
2428
handlePostEditButtonClick,
2529
handleInputPostDataChange,
2630
} = usePostWritable(post);
31+
32+
const PostCategory: PostCategoryType = {
33+
PROJECT: (
34+
<ProjectInputBox
35+
handleChange={handleInputPostDataChange}
36+
postData={postData}
37+
/>
38+
),
39+
CODE_REVIEW: (
40+
<CodeReviewInputBox
41+
handleChange={handleInputPostDataChange}
42+
postData={postData}
43+
/>
44+
),
45+
LOST: (
46+
<LostFoundInputBox
47+
handleChange={handleInputPostDataChange}
48+
handleFileSelect={handleImageFileSelect}
49+
postData={postData}
50+
lostImageUrl={lostImageUrl}
51+
/>
52+
),
53+
FOUND: (
54+
<>
55+
<LostFoundInputBox
56+
handleChange={handleInputPostDataChange}
57+
handleFileSelect={handleImageFileSelect}
58+
postData={postData}
59+
lostImageUrl={lostImageUrl}
60+
/>
61+
<FoundInputBox
62+
handleChange={handleInputPostDataChange}
63+
postData={postData}
64+
/>
65+
</>
66+
),
67+
};
68+
2769
return (
2870
<>
2971
<Layout>
@@ -34,33 +76,7 @@ const PostEditPage = ({ id }: PostDetailParamsProps) => {
3476
handleChange={handleInputPostDataChange}
3577
postData={postData}
3678
/>
37-
{postData.category === CATEGORY.PROJECT && (
38-
<ProjectInputBox
39-
handleChange={handleInputPostDataChange}
40-
postData={postData}
41-
/>
42-
)}
43-
{postData.category === CATEGORY.CODE_REVIEW && (
44-
<CodeReviewInputBox
45-
handleChange={handleInputPostDataChange}
46-
postData={postData}
47-
/>
48-
)}
49-
{(postData.category === CATEGORY.LOST ||
50-
postData.category === CATEGORY.FOUND) && (
51-
<LostFoundInputBox
52-
handleChange={handleInputPostDataChange}
53-
handleFileSelect={handleImageFileSelect}
54-
postData={postData}
55-
lostImageUrl={lostImageUrl}
56-
/>
57-
)}
58-
{postData.category === CATEGORY.FOUND && (
59-
<FoundInputBox
60-
handleChange={handleInputPostDataChange}
61-
postData={postData}
62-
/>
63-
)}
79+
{PostCategory[postData.category]}
6480
<ContentInputBox
6581
handleChange={handleInputPostDataChange}
6682
postData={postData}

src/templates/post/layouts/write/index.tsx

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import { CATEGORY } from "../../constants";
1414
import { usePostWritable } from "../../hooks";
1515
import PostCategoryBox from "./CategoryBox";
1616

17+
type PostCategoryType = {
18+
[key in keyof typeof CATEGORY]?: JSX.Element;
19+
};
20+
1721
const PostWritePage = () => {
1822
const {
1923
postData,
@@ -23,6 +27,44 @@ const PostWritePage = () => {
2327
handlePostWriteButtonClick,
2428
handleInputPostDataChange,
2529
} = usePostWritable();
30+
31+
const PostCategory: PostCategoryType = {
32+
PROJECT: (
33+
<ProjectInputBox
34+
handleChange={handleInputPostDataChange}
35+
postData={postData}
36+
/>
37+
),
38+
CODE_REVIEW: (
39+
<CodeReviewInputBox
40+
handleChange={handleInputPostDataChange}
41+
postData={postData}
42+
/>
43+
),
44+
LOST: (
45+
<LostFoundInputBox
46+
handleChange={handleInputPostDataChange}
47+
handleFileSelect={handleImageFileSelect}
48+
postData={postData}
49+
lostImageUrl={lostImageUrl}
50+
/>
51+
),
52+
FOUND: (
53+
<>
54+
<LostFoundInputBox
55+
handleChange={handleInputPostDataChange}
56+
handleFileSelect={handleImageFileSelect}
57+
postData={postData}
58+
lostImageUrl={lostImageUrl}
59+
/>
60+
<FoundInputBox
61+
handleChange={handleInputPostDataChange}
62+
postData={postData}
63+
/>
64+
</>
65+
),
66+
};
67+
2668
return (
2769
<>
2870
<Layout>
@@ -37,33 +79,7 @@ const PostWritePage = () => {
3779
handleChange={handleInputPostDataChange}
3880
postData={postData}
3981
/>
40-
{postData.category === CATEGORY.PROJECT && (
41-
<ProjectInputBox
42-
handleChange={handleInputPostDataChange}
43-
postData={postData}
44-
/>
45-
)}
46-
{postData.category === CATEGORY.CODE_REVIEW && (
47-
<CodeReviewInputBox
48-
handleChange={handleInputPostDataChange}
49-
postData={postData}
50-
/>
51-
)}
52-
{(postData.category === CATEGORY.LOST ||
53-
postData.category === CATEGORY.FOUND) && (
54-
<LostFoundInputBox
55-
handleChange={handleInputPostDataChange}
56-
handleFileSelect={handleImageFileSelect}
57-
postData={postData}
58-
lostImageUrl={lostImageUrl}
59-
/>
60-
)}
61-
{postData.category === CATEGORY.FOUND && (
62-
<FoundInputBox
63-
handleChange={handleInputPostDataChange}
64-
postData={postData}
65-
/>
66-
)}
82+
{PostCategory[postData.category]}
6783
<ContentInputBox
6884
handleChange={handleInputPostDataChange}
6985
postData={postData}

0 commit comments

Comments
 (0)