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: 설문 질문 생성 버그 수정 #78

Merged
merged 1 commit into from
Aug 27, 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
10 changes: 7 additions & 3 deletions web/src/components/Survey/choice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,26 @@ export const Choice = ({
);

useEffect(() => {
if (type === 'MULTIPLE_CHOICE' && choices.length < 2) {
if (choices.length < 2) {
const newChoices = [...choices, { id: uuidv4(), value: '' }];
setChoices(newChoices);
onOptionsChange(newChoices.map(choice => choice.value));
}
}, [type, choices, onOptionsChange]);

const handleAddChoice = () => {
if (choices.length === 10) {
toast.error('질문 선택지는 최대 10개입니다.');
return;
}
const newChoices = [...choices, { id: uuidv4(), value: '' }];
setChoices(newChoices);
onOptionsChange(newChoices.map(choice => choice.value));
};

const handleRemoveChoice = (id: string) => {
if (type === 'MULTIPLE_CHOICE' && choices.length <= 2) {
toast.error('복수선택 질문 유형에서는 최소 2개의 선택지가 필요합니다.');
if (choices.length <= 2) {
toast.error('질문 유형에서는 최소 2개의 선택지가 필요합니다.');
return;
}
const newChoices = choices.filter(choice => choice.id !== id);
Expand Down
24 changes: 24 additions & 0 deletions web/src/components/Survey/create-survey-page3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { QuestionType } from '@/types/survey';
import { useState } from 'react';
import { useSelector } from 'react-redux';
import { useNavigate } from 'react-router-dom';
import { toast } from 'sonner';
import { v4 as uuidv4 } from 'uuid';
import { Box } from '../common/box';
import { Button } from '../common/button';
Expand Down Expand Up @@ -50,12 +51,20 @@ export const CreateSurveyPageStep3 = () => {
]);

const addQuestion = () => {
if (questions.length === 10) {
toast.error('질문은 최대 10개입니다.');
return;
}
setQuestions([
...questions,
{ id: uuidv4(), type: 'SUBJECTIVE', text: '', options: [] },
]);
};

const deleteQuestion = (id: string) => {
setQuestions(questions.filter(q => q.id !== id));
};

const changeQuestionType = (
id: string,
type: 'SUBJECTIVE' | 'SINGLE_CHOICE' | 'MULTIPLE_CHOICE'
Expand All @@ -74,6 +83,20 @@ export const CreateSurveyPageStep3 = () => {
};

const handleSubmit = async () => {
if (questions.filter(q => q.text.trim().length === 0).length > 0) {
toast.error('질문을 입력해주세요.');
return;
}
if (
questions.filter(
q =>
q.type !== 'SUBJECTIVE' && q.options.map(c => c.trim().length === 0)
).length > 0
) {
toast.error('객관식 항목은 빈 값일 수 없습니다.');
return;
}

const formattedData = {
eventId,
consentFormHtml: policy,
Expand Down Expand Up @@ -117,6 +140,7 @@ export const CreateSurveyPageStep3 = () => {
<QuestionSelect
value={question.type}
onChange={newType => changeQuestionType(question.id, newType)}
onDelete={() => deleteQuestion(question.id)}
/>
{question.type === 'SUBJECTIVE' && (
<Subjective
Expand Down
10 changes: 8 additions & 2 deletions web/src/components/Survey/question-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ type ValueType = keyof typeof valueMap;
type QuestionSelectProps = {
value: ValueType;
onChange: (value: ValueType) => void;
onDelete: () => void;
};

export const QuestionSelect = ({ value, onChange }: QuestionSelectProps) => {
export const QuestionSelect = ({
value,
onChange,
onDelete,
}: QuestionSelectProps) => {
return (
<QuestionSelectWrapper>
<Select value={value} onValueChange={onChange}>
Expand All @@ -49,7 +54,8 @@ export const QuestionSelect = ({ value, onChange }: QuestionSelectProps) => {
</SelectGroup>
</SelectContent>
</Select>
<X />
<X onClick={onDelete} style={{ cursor: 'pointer' }} />{' '}
{/* Add the onClick handler */}
</QuestionSelectWrapper>
);
};
Loading