1
- import { Button , Form , Icon , Modal } from 'semantic-ui-react' ;
1
+ import { Button , Form , Icon , Modal , Radio } from 'semantic-ui-react' ;
2
2
import { useState } from 'react' ;
3
3
import DeleteConfirmModal from '../../common/delete.confirm.modal' ;
4
4
import { PoPoAxios } from '@/utils/axios.instance' ;
@@ -8,28 +8,70 @@ const WhitebookUpdateModal = ({ trigger, whitebook }) => {
8
8
const [ deleteModalOpen , setDeleteModalOpen ] = useState ( false ) ;
9
9
10
10
const [ title , setTitle ] = useState ( whitebook . title ) ;
11
- const [ link , setLink ] = useState ( whitebook . link ) ;
12
11
const [ content , setContent ] = useState ( whitebook . content ) ;
13
12
const [ showOnlyLogin , setShowOnlyLogin ] = useState ( whitebook . show_only_login ) ;
13
+ const [ inputType , setInputType ] = useState ( whitebook . link ? 'link' : 'pdf' ) ;
14
+ const [ link , setLink ] = useState ( whitebook . link || '' ) ;
15
+ const [ uploadedPDFLink , setUploadedPDFLink ] = useState ( null ) ;
16
+ const [ pdfFile , setPdfFile ] = useState ( null ) ;
17
+
18
+ const handleModalOpen = ( ) => {
19
+ setOpen ( true ) ;
20
+ // 링크에 whitebook이 들어간다면 S3에 있는 pdf 파일임
21
+ if ( whitebook . link && whitebook . link . includes ( 'whitebook' ) ) {
22
+ const link = whitebook . link ;
23
+ setLink ( '' ) ;
24
+ setUploadedPDFLink ( link ) ;
25
+ setInputType ( 'pdf' ) ;
26
+ }
27
+ } ;
28
+
29
+ const handleFileChange = ( file ) => {
30
+ if ( ! file || file . type !== 'application/pdf' ) {
31
+ alert ( 'PDF 파일만 업로드 가능합니다.' ) ;
32
+ setPdfFile ( null ) ;
33
+ return ;
34
+ }
35
+ setPdfFile ( file ) ;
36
+ } ;
14
37
15
38
const handleSubmit = async ( ) => {
16
- try {
17
- await PoPoAxios . put (
18
- `/whitebook/${ whitebook . uuid } ` ,
19
- {
20
- title : title ,
21
- link : link ,
22
- content : content ,
23
- show_only_login : showOnlyLogin ,
24
- } ,
25
- { withCredentials : true } ,
26
- ) ;
27
- setOpen ( false ) ;
28
- window . location . reload ( ) ;
29
- } catch ( e ) {
30
- alert ( '생활백서 수정에 실패했습니다.' ) ;
31
- console . log ( e ) ;
39
+ const formData = new FormData ( ) ;
40
+ formData . append ( 'title' , title ) ;
41
+ formData . append ( 'content' , content ) ;
42
+ formData . append ( 'show_only_login' , showOnlyLogin ) ;
43
+
44
+ if ( inputType === 'link' && link ) {
45
+ formData . append ( 'link' , link ) ;
46
+ } else if ( inputType === 'pdf' && pdfFile ) {
47
+ formData . append ( 'pdf_file' , pdfFile ) ;
48
+ } else {
49
+ // title, content, show_only_login 중 하나라도 변경되었다면 수정 가능
50
+ if (
51
+ title !== whitebook . title ||
52
+ content !== whitebook . cotent ||
53
+ showOnlyLogin !== whitebook . show_only_login
54
+ ) {
55
+ const link = inputType === 'link' ? link : uploadedPDFLink ;
56
+ formData . append ( 'link' , link ) ;
57
+ } else {
58
+ alert ( '링크 또는 PDF 파일을 입력해주세요.' ) ;
59
+ return ;
60
+ }
32
61
}
62
+
63
+ await PoPoAxios . put ( `/whitebook/${ whitebook . uuid } ` , formData , {
64
+ withCredentials : true ,
65
+ headers : { 'Content-Type' : 'multipart/form-data' } ,
66
+ } )
67
+ . then ( ( ) => {
68
+ alert ( '생활백서 정보를 수정 했습니다.' ) ;
69
+ window . location . reload ( ) ;
70
+ } )
71
+ . catch ( ( err ) => {
72
+ alert ( '생활백서 수정에 실패했습니다.' ) ;
73
+ console . error ( err ) ;
74
+ } ) ;
33
75
} ;
34
76
35
77
return (
@@ -38,7 +80,7 @@ const WhitebookUpdateModal = ({ trigger, whitebook }) => {
38
80
open = { open }
39
81
trigger = { trigger }
40
82
onClose = { ( ) => setOpen ( false ) }
41
- onOpen = { ( ) => setOpen ( true ) }
83
+ onOpen = { handleModalOpen }
42
84
>
43
85
< Modal . Header > 생활백서 수정</ Modal . Header >
44
86
< Modal . Content >
@@ -49,19 +91,81 @@ const WhitebookUpdateModal = ({ trigger, whitebook }) => {
49
91
value = { title }
50
92
onChange = { ( e ) => setTitle ( e . target . value ) }
51
93
/>
52
- < Form . Input
53
- required
54
- label = { '생활백서 링크' }
55
- value = { link }
56
- placeholder = { 'https://xxxx.postech.ac.kr' }
57
- onChange = { ( e ) => setLink ( e . target . value ) }
58
- />
94
+
95
+ { /* PDF 또는 링크 선택 */ }
96
+ < Form . Group inline >
97
+ < label > 파일 타입 선택:</ label >
98
+ < Form . Field >
99
+ < Radio
100
+ label = "링크 입력"
101
+ name = "inputType"
102
+ value = "link"
103
+ checked = { inputType === 'link' }
104
+ onChange = { ( ) => {
105
+ setInputType ( 'link' ) ;
106
+ } }
107
+ />
108
+ </ Form . Field >
109
+ < Form . Field >
110
+ < Radio
111
+ label = "PDF 업로드"
112
+ name = "inputType"
113
+ value = "pdf"
114
+ checked = { inputType === 'pdf' }
115
+ onChange = { ( ) => {
116
+ setInputType ( 'pdf' ) ;
117
+ } }
118
+ />
119
+ </ Form . Field >
120
+ </ Form . Group >
121
+
122
+ { /* 조건부 렌더링: 링크 입력 필드 */ }
123
+ { inputType === 'link' && (
124
+ < Form . Input
125
+ required
126
+ label = { '생활백서 링크' }
127
+ placeholder = { 'https://xxxx.postech.ac.kr' }
128
+ value = { link }
129
+ onChange = { ( e ) => setLink ( e . target . value ) }
130
+ />
131
+ ) }
132
+
133
+ { /* 조건부 렌더링: PDF 업로드 필드 */ }
134
+ { inputType === 'pdf' && (
135
+ < Form . Field >
136
+ < Form . Input
137
+ required
138
+ label = { '생활백서 PDF' }
139
+ type = "file"
140
+ accept = "application/pdf"
141
+ onChange = { ( e ) => handleFileChange ( e . target . files [ 0 ] ) }
142
+ />
143
+ < label >
144
+ { ( pdfFile || uploadedPDFLink ) && (
145
+ < a
146
+ href = {
147
+ pdfFile ? URL . createObjectURL ( pdfFile ) : uploadedPDFLink
148
+ }
149
+ target = "_blank"
150
+ rel = "noopener noreferrer"
151
+ style = { {
152
+ textDecoration : 'underline' ,
153
+ } }
154
+ >
155
+ { pdfFile ? pdfFile . name : '기존 PDF 확인' }
156
+ </ a >
157
+ ) }
158
+ </ label >
159
+ </ Form . Field >
160
+ ) }
161
+
59
162
< Form . TextArea
60
163
required
61
164
label = { '생활백서 설명글' }
62
165
value = { content }
63
166
onChange = { ( e ) => setContent ( e . target . value ) }
64
167
/>
168
+
65
169
< Form . Checkbox
66
170
required
67
171
label = { '로그인 유저에게만 보이기' }
@@ -76,7 +180,7 @@ const WhitebookUpdateModal = ({ trigger, whitebook }) => {
76
180
</ Form . Button >
77
181
< DeleteConfirmModal
78
182
open = { deleteModalOpen }
79
- target = { name }
183
+ target = { whitebook . title }
80
184
deleteURI = { `whitebook/${ whitebook . uuid } ` }
81
185
trigger = {
82
186
< Button negative onClick = { ( ) => setDeleteModalOpen ( true ) } >
0 commit comments