11var React = require ( 'react/addons' )
2+ var request = require ( 'superagent' )
23var Select = require ( 'react-select' )
34
45var Catalyst = require ( '../Mixins/Catalyst' )
56
67var PlanetActions = require ( '../Actions/PlanetActions' )
78
9+ var getOptions = function ( input , callback ) {
10+ request
11+ . get ( 'http://localhost:8000/users/search' )
12+ . query ( { name : input } )
13+ . send ( )
14+ . end ( function ( err , res ) {
15+ if ( err ) {
16+ callback ( err )
17+ return
18+ }
19+ callback ( null , {
20+ options : res . body . map ( function ( user ) {
21+ return {
22+ label : user . name ,
23+ value : user . name
24+ }
25+ } ) ,
26+ complete : false
27+ } )
28+ } )
29+ }
30+
831module . exports = React . createClass ( {
932 mixins : [ Catalyst . LinkedStateMixin ] ,
1033 propTypes : {
@@ -15,7 +38,8 @@ module.exports = React.createClass({
1538 return {
1639 currentTab : 'planetProfile' ,
1740 planetName : this . props . currentPlanet . name ,
18- isDeletePlanetChecked : false
41+ isDeletePlanetChecked : false ,
42+ userName : ''
1943 }
2044 } ,
2145 activePlanetProfile : function ( ) {
@@ -29,6 +53,17 @@ module.exports = React.createClass({
2953 var currentPlanet = this . props . currentPlanet
3054 PlanetActions . changeName ( currentPlanet . userName , currentPlanet . name , this . state . planetName )
3155 } ,
56+ handleChange : function ( value ) {
57+ this . setState ( { userName : value } )
58+ } ,
59+ addUser : function ( ) {
60+ PlanetActions . addUser ( this . props . currentPlanet . userName + '/' + this . props . currentPlanet . name , this . state . userName )
61+ } ,
62+ removeUser : function ( userName ) {
63+ return function ( ) {
64+ PlanetActions . removeUser ( this . props . currentPlanet . userName + '/' + this . props . currentPlanet . name , userName )
65+ } . bind ( this )
66+ } ,
3267 doubleCheckDeletePlanet : function ( ) {
3368 if ( this . state . isDeletePlanetChecked ) {
3469 console . log ( 'delete it' )
@@ -72,7 +107,7 @@ module.exports = React.createClass({
72107 < img className = 'userPhoto' width = '44' height = '44' src = '../vendor/dummy.jpg' />
73108 < div className = 'userName' > { user . name } </ div >
74109 < div className = 'userControl' >
75- { this . props . currentPlanet . OwnerId !== user . id ? < button className = 'btn-default' > Delete</ button > : < span className = 'ownerLabel' > Owner</ span > }
110+ { this . props . currentPlanet . OwnerId !== user . id ? < button onClick = { this . removeUser ( user . name ) } className = 'btn-default' > Delete</ button > : < span className = 'ownerLabel' > Owner</ span > }
76111 </ div >
77112 </ li >
78113 )
@@ -86,8 +121,15 @@ module.exports = React.createClass({
86121 < div className = 'addUserForm' >
87122 < div className = 'addUserLabel' > Invite user</ div >
88123 < div className = 'addUserControl' >
89- < Select className = 'addUserSelect' />
90- < button className = 'addUserSubmit btn-primary' > Invite</ button >
124+ < Select
125+ name = 'userName'
126+ value = { this . state . userName }
127+ placeholder = 'Username'
128+ asyncOptions = { getOptions }
129+ onChange = { this . handleChange }
130+ className = 'addUserSelect'
131+ />
132+ < button onClick = { this . addUser } className = 'addUserSubmit btn-primary' > Invite</ button >
91133 </ div >
92134 </ div >
93135 </ div >
0 commit comments