11import React , { Component } from 'react' ;
2- import { Modal , Input , message } from 'antd ' ;
3- import { createGroup } from '../../util/APIUtils ' ;
2+ import { createGroup , getAllUsers } from '../../util/APIUtils ' ;
3+ import { Modal , Input , message , Checkbox , Spin } from 'antd ' ;
44
55
66class GroupCreateModal extends Component {
@@ -9,19 +9,42 @@ class GroupCreateModal extends Component {
99 this . state = {
1010 name : '' ,
1111 description : '' ,
12- imageUrl : ''
12+ imageUrl : '' ,
13+ users : [ ] ,
14+ selectedUserIds : [ ] ,
15+ loading : false
1316 } ;
1417 }
1518
19+ componentDidMount ( ) {
20+ this . setState ( { loading : true } ) ;
21+ getAllUsers ( )
22+ . then ( users => {
23+ this . setState ( { users, loading : false } ) ;
24+ } )
25+ . catch ( ( ) => {
26+ message . error ( '유저 목록을 불러오는 데 실패했습니다.' ) ;
27+ this . setState ( { loading : false } ) ;
28+ } ) ;
29+ }
30+
1631 handleChange = ( field , value ) => {
1732 this . setState ( { [ field ] : value } ) ;
1833 }
1934
35+ handleUserSelect = userId => {
36+ const { selectedUserIds } = this . state ;
37+ const newList = selectedUserIds . includes ( userId )
38+ ? selectedUserIds . filter ( id => id !== userId )
39+ : [ ...selectedUserIds , userId ] ;
40+ this . setState ( { selectedUserIds : newList } ) ;
41+ }
42+
2043 handleSubmit = ( ) => {
2144
22- const { name, description, imageUrl } = this . state ;
45+ const { name, description, imageUrl, selectedUserIds } = this . state ;
2346
24- createGroup ( { name, description, imageUrl } )
47+ createGroup ( { name, description, imageUrl, memberIds : selectedUserIds } )
2548 . then ( data => {
2649 if ( ! data || ! data . name ) {
2750 throw new Error ( "응답에 그룹 이름이 없습니다." ) ;
@@ -46,7 +69,7 @@ class GroupCreateModal extends Component {
4669
4770 render ( ) {
4871 const { visible, onClose } = this . props ;
49- const { name, description, imageUrl } = this . state ;
72+ const { name, description, imageUrl, users , selectedUserIds , loading } = this . state ;
5073
5174 return (
5275 < Modal
@@ -75,6 +98,20 @@ class GroupCreateModal extends Component {
7598 value = { imageUrl }
7699 onChange = { e => this . handleChange ( 'imageUrl' , e . target . value ) }
77100 />
101+ < div style = { { maxHeight : 150 , overflowY : 'auto' , border : '1px solid #eee' , padding : 8 } } >
102+ { loading ? (
103+ < Spin />
104+ ) : users . map ( user => (
105+ < Checkbox
106+ key = { user . id }
107+ checked = { selectedUserIds . includes ( user . id ) }
108+ onChange = { ( ) => this . handleUserSelect ( user . id ) }
109+ style = { { display : 'block' , marginBottom : 6 } }
110+ >
111+ { user . name } ({ user . username } )
112+ </ Checkbox >
113+ ) ) }
114+ </ div >
78115 </ Modal >
79116 ) ;
80117 }
0 commit comments