Skip to content

Commit

Permalink
๐Ÿ”จ Refactor(#191): ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ์ƒ์ˆ˜ ์ถ”๊ฐ€ ํ•จ์ˆ˜ ๊ธฐ๋Šฅ ๋ถ„๋ฆฌ
Browse files Browse the repository at this point in the history
  • Loading branch information
rhehfl committed Feb 28, 2025
1 parent b7c524d commit c251281
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
7 changes: 7 additions & 0 deletions src/features/user/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ export const OPINIONS_OPTIONS = [
{ label: '๊ธฐ๋Šฅ ์ถ”๊ฐ€', id: 2 },
{ label: '์ง์ ‘ ์ž…๋ ฅ', id: 3 },
] as const;

export const ERROR_MESSAGES = {
REQUIRED_CONTENT: '๋‚ด์šฉ์€ ํ•„์ˆ˜ ์ž…๋ ฅ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค.',
CONTENT_TOO_LONG: '๋‚ด์šฉ์€ 255์ž ์ดํ•˜๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.',
REQUIRED_TITLE: '์ œ๋ชฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.',
REQUIRED_QUIZ: 'ํ€ด์ฆˆ๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”.',
} as const;
60 changes: 37 additions & 23 deletions src/features/user/ui/OpinionsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SortDropdown from '@/common/layout/SortDropdown';
import Select from '@/features/intro/ui/Select';
import { useLocationQuizState } from '@/features/quiz/hooks';
import { Quiz } from '@/features/quiz/types';
import { OPINIONS_OPTIONS } from '@/features/user/constants';
import { ERROR_MESSAGES, OPINIONS_OPTIONS } from '@/features/user/constants';
import { useUserOpinionsQuery } from '@/features/user/queries';
import {
ContentWrapper,
Expand Down Expand Up @@ -37,35 +37,49 @@ export default function OpinionsModal({

const { mutate: createOpinions } = useUserOpinionsQuery.createOpinions();

const handleSubmit = () => {
const getTitle = () => {
const titleMap: Record<string, string> = {
'์ง์ ‘ ์ž…๋ ฅ': customTitle,
ํ€ด์ฆˆ: quizInfo,
};

return titleMap[selectedOption] || selectedOption;
};

const getErrorMessage = () => {
if (!content) {
setError('๋‚ด์šฉ์€ ํ•„์ˆ˜ ์ž…๋ ฅ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค.');
return;
return ERROR_MESSAGES.REQUIRED_CONTENT;
}
if (content.length >= 255) {
setError('๋‚ด์šฉ์€ 255์ž ์ดํ•˜๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
return;
return ERROR_MESSAGES.CONTENT_TOO_LONG;
}

if (selectedOption === '์ง์ ‘ ์ž…๋ ฅ') {
if (!customTitle) {
setError('์ œ๋ชฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
return;
}
} else if (selectedOption === 'ํ€ด์ฆˆ') {
if (!quizInfo) {
setError('ํ€ด์ฆˆ๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”.');
return;
}
if (selectedOption === '์ง์ ‘ ์ž…๋ ฅ' && !customTitle) {
return ERROR_MESSAGES.REQUIRED_TITLE;
}

if (selectedOption === 'ํ€ด์ฆˆ' && !quizInfo) {
return ERROR_MESSAGES.REQUIRED_QUIZ;
}
let title = '';
if (selectedOption === '์ง์ ‘ ์ž…๋ ฅ') {
title = customTitle;
} else if (selectedOption === 'ํ€ด์ฆˆ') {
title = quizInfo;
} else {
title = selectedOption;

return null;
};

const validation = () => {
const errorMessage = getErrorMessage();

if (errorMessage) {
setError(errorMessage);
return false;
}
return true;
};

const handleSubmit = () => {
if (!validation()) {
return;
}
const title = getTitle();

createOpinions(
{
Expand Down

0 comments on commit c251281

Please sign in to comment.