Skip to content

Commit

Permalink
[Chore/#33] 하나의 기수에 2개 이상의 파트를 선택했을 시 alert 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jaminleee committed Sep 6, 2024
1 parent 3698839 commit da591da
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/components/information/Information.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default function Information() {
updatedSelections[index].selectedSeason = season === '선택 안함' ? '선택' : season;
updatedSelections[index].selectedPart = '선택'; // 기수 변경 시 파트 초기화
setSelections(updatedSelections);
checkForDuplicateSeasons(updatedSelections);

if (season !== '선택' && updatedSelections[index].selectedPart !== '선택') {
setFormAlertMessage(null);
Expand All @@ -64,6 +65,7 @@ export default function Information() {
const updatedSelections = [...selections];
updatedSelections[index].selectedPart = part === '선택 안함' ? '선택' : part;
setSelections(updatedSelections);
checkForDuplicateSeasons(updatedSelections);

if (part !== '선택' && updatedSelections[index].selectedSeason !== '선택') {
setFormAlertMessage(null);
Expand All @@ -88,7 +90,9 @@ export default function Information() {
const removeSeasonPartSelection = (index: number) => {
const updatedSelections = selections.filter((_, i) => i !== index);
setSelections(updatedSelections);
checkForDuplicateSeasons(updatedSelections);
};

const handleUnivSearch = (e: ChangeEvent<HTMLInputElement>) => {
setSearchValue(e.target.value);
setUniv(e.target.value);
Expand Down Expand Up @@ -129,11 +133,43 @@ export default function Information() {
return false;
}

// 중복된 기수 체크
const seasonCounts = selections.reduce((acc, selection) => {
if (selection.selectedSeason !== '선택') {
acc[selection.selectedSeason] = (acc[selection.selectedSeason] || 0) + 1;
}
return acc;
}, {} as Record<string, number>);

const hasDuplicateSeasons = Object.values(seasonCounts).some((count) => count > 1);

if (hasDuplicateSeasons) {
setFormAlertMessage('한 기수당 한 파트만 선택할 수 있습니다.');
return false;
}

// 모든 필드 채워짐
setFormAlertMessage(null);
return true;
};

const checkForDuplicateSeasons = (selections: SeasonPartSelection[]) => {
const seasonCounts = selections.reduce((acc, selection) => {
if (selection.selectedSeason !== '선택') {
acc[selection.selectedSeason] = (acc[selection.selectedSeason] || 0) + 1;
}
return acc;
}, {} as Record<string, number>);

const hasDuplicateSeasons = Object.values(seasonCounts).some((count) => count > 1);

if (hasDuplicateSeasons) {
setFormAlertMessage('한 기수당 한 파트만 선택할 수 있습니다.');
} else {
setFormAlertMessage(null); // 중복이 해결되면 경고 메시지 제거
}
};

const handleSubmit = () => {
if (validateForm()) {
console.log('Form submitted');
Expand Down

0 comments on commit da591da

Please sign in to comment.