-
Notifications
You must be signed in to change notification settings - Fork 3
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
[fix/#58]: 과목 편집 문제 해결 #89
Changes from 5 commits
e11bc78
a3b1281
a72befc
123923a
c1a3ebf
003d396
e084485
5bb93ff
fde4c5c
4e15b54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ import React, { useState, useEffect } from 'react'; | |
import { Text } from '@radix-ui/themes'; | ||
import styles from './SubjectForm.module.css'; | ||
import { useSubjectStore } from '@/store/subjectStore'; | ||
import { subjectFormApi } from '@/api/subjectFormApi'; // API 함수 임포트 | ||
import { subjectFormApi } from '@/api/subjectFormApi'; | ||
import { fetchSubjects } from '@/api/subjectFormApi'; | ||
import useSWR from 'swr'; | ||
|
||
|
@@ -36,36 +36,55 @@ export default function SubjectEditForm({ | |
setSubjects, | ||
} = useSubjectStore(); | ||
|
||
// SWR을 사용하여 과목 데이터를 가져오고 동기화 | ||
// SWR을 사용하여 과목 데이터를 가져오고 동기화 - 마이페이지 새로 고침 시 저장 | ||
const { data, error } = useSWR('subjects', fetchSubjects, { | ||
onSuccess: (data) => { | ||
setSubjects(data.subjects); | ||
if (addedSubjects.length === 0 && deletedSubjects.length === 0) { | ||
setSubjects(data.subjects); | ||
} | ||
}, | ||
}); | ||
|
||
if (error) { | ||
console.error('Failed to load subjects:', error); | ||
} | ||
|
||
// 과목 편집 저장 버튼 | ||
const handleAddSubject = () => { | ||
if (newSubjectName.trim() !== '') { | ||
const newSubject = { id: Date.now(), name: newSubjectName }; // 예시로 새로운 ID 생성 | ||
addSubject(newSubject); | ||
setNewSubjectName(''); | ||
if (newSubjectName.trim() === '') { | ||
return; | ||
} | ||
|
||
// 중복 체크 | ||
const isDuplicate = subjects.some((subject) => subject.name === newSubjectName.trim()); | ||
if (isDuplicate) { | ||
alert('이 과목은 이미 존재합니다.'); | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 중복 체크 해주는거 좋은것 같습니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 굳굳 |
||
} | ||
|
||
const newSubject = { id: Date.now(), name: newSubjectName }; // 예시로 새로운 ID 생성 | ||
addSubject(newSubject); | ||
setNewSubjectName(''); | ||
}; | ||
|
||
const handleCancelEditing = () => { | ||
revertChanges(); // 취소 시 초기 상태로 되돌리기 | ||
setEditing(false); // 편집 모드 종료 | ||
revertChanges(); | ||
setEditing(false); | ||
}; | ||
|
||
const handleSaveEditing = async () => { | ||
const payload = { | ||
addedSubjects: addedSubjects, | ||
deletedSubjects: deletedSubjects, | ||
addedSubjects: addedSubjects.map((subject) => subject.name), // 이름만 추출 | ||
deletedSubjects: deletedSubjects.map((subject) => subject.id), // ID만 추출 | ||
}; | ||
|
||
// 추가된 과목과 삭제된 과목이 모두 비어있는 경우 | ||
if (payload.addedSubjects.length === 0 && payload.deletedSubjects.length === 0) { | ||
alert('변경된 과목이 없습니다.'); | ||
setEditing(false); | ||
return; | ||
} | ||
|
||
try { | ||
const response = await subjectFormApi(payload); | ||
if (response.success) { | ||
|
@@ -76,19 +95,18 @@ export default function SubjectEditForm({ | |
|
||
// 삭제된 ID와 추가된 과목명을 알림으로 표시 | ||
alert( | ||
`삭제한 과목 ID는 ${deletedSubjects.join(', ')} 입니다\n` + | ||
`추가된 과목은 ${addedSubjects.join(', ')} 입니다`, | ||
`삭제한 과목 ID는 ${deletedSubjects.map((subject) => subject.id).join(', ')} 입니다\n` + | ||
`추가된 과목은 ${addedSubjects.map((subject) => subject.name).join(', ')} 입니다`, | ||
); | ||
|
||
// 부모 컴포넌트에 저장 완료를 알림 | ||
onSaveEditing(); | ||
} else { | ||
console.error('Failed to update subjects:', response.message); | ||
alert(`업데이트 실패: ${response.message}`); // 오류 메시지를 사용자에게 표시 | ||
alert(`업데이트 실패: ${response.message}`); | ||
} | ||
} catch (error) { | ||
console.error('Error sending request:', error); | ||
alert('요청을 처리하는 중 오류가 발생했습니다.'); // 오류 처리 | ||
alert('요청을 처리하는 중 오류가 발생했습니다.'); | ||
} | ||
}; | ||
|
||
|
@@ -114,10 +132,12 @@ export default function SubjectEditForm({ | |
</div> | ||
<div className={styles.subject_choice_box} style={subjectChoiceBoxStyle}> | ||
{subjects?.map((subject) => ( | ||
<div key={subject.id} className={styles.subject_item}> | ||
<Text as="p" size="3" className={styles.subject_item_text}> | ||
{subject.name} | ||
</Text> | ||
<div key={subject.id} className={styles.subject_item_wrapper}> | ||
<div className={styles.subject_item}> | ||
<Text as="p" size="3" className={styles.subject_item_text}> | ||
{subject.name} | ||
</Text> | ||
</div> | ||
<button className={styles.delete_button} onClick={() => deleteSubject(subject.id)}> | ||
- | ||
</button> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,9 +123,14 @@ | |
} | ||
|
||
/* 과목 박스 전체 */ | ||
.subject_select_choice_box { | ||
max-height: 400px !important; | ||
} | ||
|
||
.subject_choice_box { | ||
position: relative; | ||
background-color: white; | ||
max-height: 350px; | ||
max-height: 330px; | ||
display: flex; | ||
align-items: flex-start; | ||
flex-wrap: wrap; | ||
|
@@ -135,6 +140,13 @@ | |
row-gap: 15px; | ||
overflow-y: auto; | ||
overflow-x: hidden; | ||
padding-right: 10px; | ||
} | ||
|
||
.subject_item_wrapper { | ||
position: relative; | ||
display: inline-block; | ||
max-width: 99%; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 왜 최대 넓이를 99%만 줬는지 궁금합니다~! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 100%로 하면 (-)삭제 버튼이 살짝 짤리던데 width값을 줄이는 거 말고 다른 방법이 있을까요?! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. overflow-x 때문인데 전에도 overflow-y 때문에 감싸고 있는 박스에 padding-top:5px을 줬었죠~ 마찬가지로 하면 됩니다^_^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거 다시 해봤을 때 max-width:100%로 해도 큰 문제가 없는 것 같아서 100%로 수정했습니다,,! |
||
} | ||
|
||
.subject_item { | ||
|
@@ -150,10 +162,41 @@ | |
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
overflow: hidden; | ||
white-space: nowrap; | ||
flex: 0 1 auto; | ||
} | ||
|
||
.subject_item_text { | ||
text-align: center; | ||
text-overflow: ellipsis; | ||
overflow: hidden; | ||
} | ||
|
||
/* 삭제버튼 */ | ||
|
||
.delete_button { | ||
position: absolute; | ||
top: -5px; | ||
right: -5px; | ||
background: rgb(247, 83, 83); | ||
color: white; | ||
border: none; | ||
border-radius: 50%; | ||
width: 20px; | ||
height: 20px; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
cursor: pointer; | ||
font-size: 20px; | ||
text-align: center; | ||
z-index: 2; | ||
/* 버튼이 항상 위에 표시되도록 설정 */ | ||
} | ||
|
||
.delete_button:hover { | ||
background-color: darkred; | ||
} | ||
|
||
.subject_item.selected { | ||
|
@@ -233,30 +276,6 @@ | |
font-weight: 500; | ||
} | ||
|
||
/* 삭제버튼 */ | ||
|
||
.delete_button { | ||
position: absolute; | ||
top: -5px; | ||
right: -5px; | ||
background: rgb(247, 83, 83); | ||
color: white; | ||
border: none; | ||
border-radius: 50%; | ||
width: 20px; | ||
height: 20px; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
cursor: pointer; | ||
font-size: 20px; | ||
text-align: center; | ||
} | ||
|
||
.delete_button:hover { | ||
background-color: darkred; | ||
} | ||
|
||
/* 브라우저 화면일 때 숨기기 */ | ||
@media (min-width: 769px) { | ||
.backButton { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error code에 따라 메시지를 다르게 전달했으면 좋겠어요~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
반영하겠습니다!!