User Management: Create and delete. Initial pass.
[osm/UI.git] / skyquake / plugins / user-management / src / dashboard / dashboard.jsx
1 /*
2  * STANDARD_RIFT_IO_COPYRIGHT
3  */
4
5 import React from 'react';
6 import ReactDOM from 'react-dom';
7 import AppHeader from 'widgets/header/header.jsx';
8 import UserManagementStore from './userMgmtStore.js';
9 import SkyquakeComponent from 'widgets/skyquake_container/skyquakeComponent.jsx';
10 import 'style/layout.scss';
11 import './userMgmt.scss';
12 import {Panel, PanelWrapper} from 'widgets/panel/panel';
13
14
15 import TextInput from 'widgets/form_controls/textInput.jsx';
16 import Input from 'widgets/form_controls/input.jsx';
17 import Button, {ButtonGroup} from 'widgets/button/sq-button.jsx';
18 import SelectOption from 'widgets/form_controls/selectOption.jsx';
19 import 'widgets/form_controls/formControls.scss';
20 import imgAdd from '../../node_modules/open-iconic/svg/plus.svg'
21 import imgRemove from '../../node_modules/open-iconic/svg/trash.svg'
22
23 class UserManagementDashboard extends React.Component {
24     constructor(props) {
25         super(props);
26         this.Store = this.props.flux.stores.hasOwnProperty('UserManagementStore') ? this.props.flux.stores.UserManagementStore : this.props.flux.createStore(UserManagementStore);
27         this.Store.getUsers();
28         this.state = this.Store.getState();
29         this.actions = this.state.actions;
30     }
31     componentDidUpdate() {
32
33       ReactDOM.findDOMNode(this.UserList).addEventListener('transitionend', this.onTransitionEnd, false);
34     }
35     componentWillMount() {
36         this.Store.listen(this.updateState);
37     }
38     componentWillUnmount() {
39         this.Store.unlisten(this.updateState);
40     }
41     updateState = (state) => {
42         this.setState(state);
43     }
44     updateInput = (key, e) => {
45         let property = key;
46         this.actions.handleUpdateInput({
47             [property]:e.target.value
48         })
49     }
50     disabledChange = (e) => {
51         this.actions.handleDisabledChange(e.target.checked);
52     }
53     platformChange = (platformRole, e) => {
54         this.actions.handlePlatformRoleUpdate(platformRole, e.currentTarget.checked);
55     }
56     addProjectRole = (e) => {
57         this.actions.handleAddProjectItem();
58     }
59     removeProjectRole = (i, e) => {
60         this.actions.handleRemoveProjectItem(i);
61     }
62     updateProjectRole = (i, e) => {
63         this.actions.handleUpdateProjectRole(i, e)
64     }
65     addUser = () => {
66         this.actions.handleAddUser();
67     }
68     viewUser = (un, index) => {
69         console.log(un)
70         this.actions.viewUser(un, index);
71     }
72     closePanel = () => {
73         this.actions.handleCloseUserPanel();
74     }
75     updateUser = (e) => {
76         e.preventDefault();
77         e.stopPropagation();
78
79         this.Store.updateUser();
80     }
81     deleteUser = (e) => {
82         e.preventDefault();
83         e.stopPropagation();
84         this.Store.deleteUser({
85                 'user-name': this.state['user-name'],
86                 'user-domain': this.state['user-domain']
87             });
88     }
89     createUser = (e) => {
90         e.preventDefault();
91         e.stopPropagation();
92         if(this.state['new-password'] != this.state['confirm-password']) {
93             this.props.actions.showNotification('Passwords do not match')
94         } else {
95             this.Store.createUser({
96                 'user-name': this.state['user-name'],
97                 'user-domain': this.state['user-domain'],
98                 'password': this.state['new-password']
99                 // 'confirm-password': this.state['confirm-password']
100             });
101         }
102     }
103
104     onTransitionEnd = (e) => {
105         this.actions.handleHideColumns(e);
106         console.log('transition end')
107     }
108     render() {
109         let self = this;
110         let html;
111         let props = this.props;
112         let state = this.state;
113         html = (
114             <PanelWrapper className={`row userManagement ${!this.state.userOpen ? 'userList-open' : ''}`} style={{'alignContent': 'center', 'flexDirection': 'row'}} >
115                 <PanelWrapper ref={(div) => { this.UserList = div}} className={`column userList expanded ${this.state.userOpen ? 'collapsed ' : ' '} ${this.state.hideColumns ? 'hideColumns ' : ' '}`}>
116                     <Panel title="User List" style={{marginBottom: 0}} no-corners>
117                         <div className="tableRow tableRow--header">
118                             <div className="userName">
119                                 Username
120                             </div>
121                             <div>
122                                 Domain
123                             </div>
124                         </div>
125                         {state.users.map((u, k) => {
126                             let platformRoles = [];
127                             for(let role in u.platformRoles) {
128                                 platformRoles.push(<div>{`${role}: ${u.platformRoles[role]}`}</div>)
129                             }
130                             return (
131                                 <div className={`tableRow tableRow--data ${((self.state.activeIndex == k) && !self.state.userOpen) ? 'tableRow--data-active' : ''}`} key={k}>
132                                     <div
133                                         className={`userName userName-header ${((self.state.activeIndex == k) && self.state.userOpen) ? 'activeUser' : ''}`}
134                                         onClick={self.viewUser.bind(null, u, k)}>
135                                         {u['user-name']}
136                                     </div>
137                                     <div>
138                                         {u['user-domain']}
139                                     </div>
140
141
142                                 </div>
143                             )
144                         })}
145                     </Panel>
146                     <ButtonGroup  className="buttonGroup" style={{margin: '0 0.5rem 0.5rem', background: '#ddd', paddingBottom: '0.5rem'}}>
147                         <Button label="Add User" onClick={this.addUser} />
148                     </ButtonGroup>
149                 </PanelWrapper>
150                 <PanelWrapper
151                     className={`userAdmin column`}>
152                     <Panel
153                         title={state.isEdit ? state['user-name'] : 'Create User'}
154                         style={{marginBottom: 0}}
155                         hasCloseButton={this.closePanel}
156                         no-corners>
157                         <FormSection title="USER INFO">
158                         {
159                             this.state.isEdit ?
160                                 null
161                                 : <Input label="Username" value={state['user-name']} onChange={this.updateInput.bind(null, 'user-name')} />
162                         }
163                             <Input label="Domain" value={state['user-domain']}  onChange={this.updateInput.bind(null, 'user-domain')}></Input>
164                             <Input label="Disabled" onChange={this.disabledChange} checked={state.disabled} type="checkbox" />
165                         </FormSection>
166                         <FormSection title="PLATFORM ROLES">
167                             <Input label="Super Admin" onChange={this.platformChange.bind(null, 'super_admin')} checked={state.platformRoles.super_admin} type="checkbox" />
168                             <Input label="Platform Admin" onChange={this.platformChange.bind(null, 'platform_admin')}  checked={state.platformRoles.platform_admin} type="checkbox" />
169                             <Input label="Platform Oper" onChange={this.platformChange.bind(null, 'platform_oper')}  checked={state.platformRoles.platform_oper} type="checkbox" />
170                         </FormSection>
171                         <FormSection title="PROJECT ROLES">
172                             <InputCollection
173                                 inital={true}
174                                 type='select'
175                                 options={state.projectRolesOptions}
176                                 collection={state.projectRoles}
177                                 onChange={this.updateProjectRole}
178                                 AddItemFn={this.addProjectRole}
179                                 RemoveItemFn={this.removeProjectRole}
180                                 />
181                         </FormSection>
182                         { this.state.isEdit ?
183                             (
184                                 <FormSection title="PASSWORD CHANGE">
185                                     <Input label="OLD PASSWORD" type="password" value={state['old-password']} onChange={this.updateInput.bind(null, 'old-password')} />
186                                     <Input label="NEW PASSWORD" type="password" value={state['new-password']}  onChange={this.updateInput.bind(null, 'new-password')}/>
187                                     <Input label="REPEAT NEW PASSWORD" type="password"  value={state['confirm-password']}  onChange={this.updateInput.bind(null, 'confirm-password')}/>
188                                 </FormSection>
189                             ) :
190                             (
191                                 <FormSection title="CREATE PASSWORD">
192                                     <Input label="CREATE PASSWORD" type="password" value={state.newPassword}  onChange={this.updateInput.bind(null, 'new-password')}/>
193                                     <Input label="REPEAT PASSWORD" type="password"  value={state.repeatNewPassword}  onChange={this.updateInput.bind(null, 'confirm-password')}/>
194                                 </FormSection>
195                             )
196                         }
197
198                     </Panel>
199
200                         {
201                             state.isEdit ?
202                             (
203                                 <ButtonGroup className="buttonGroup">
204                                     <Button label="Update" onClick={this.updateUser} />
205                                     <Button label="Delete" onClick={this.deleteUser} />
206                                 </ButtonGroup>
207                             )
208                             : (
209                                 <ButtonGroup className="buttonGroup">
210                                     <Button label="Create" onClick={this.createUser}  />
211                                 </ButtonGroup>
212                             )
213                         }
214
215                 </PanelWrapper>
216
217             </PanelWrapper>
218         );
219         return html;
220     }
221 }
222 // onClick={this.Store.update.bind(null, Account)}
223 UserManagementDashboard.contextTypes = {
224     router: React.PropTypes.object
225 };
226
227 UserManagementDashboard.defaultProps = {
228     userList: [],
229     selectedUser: {}
230 }
231
232 export default SkyquakeComponent(UserManagementDashboard);
233
234
235
236
237 /**
238  * AddItemFn:
239  */
240 class InputCollection extends React.Component {
241     constructor(props) {
242         super(props);
243         this.collection = props.collection;
244     }
245     buildTextInput(onChange, v, i) {
246         return (
247             <Input
248                 style={{flex: '1 1'}}
249                 key={i}
250                 value={v}
251                 onChange= {onChange.bind(null, i)}
252             />
253         )
254     }
255     buildSelectOption(initial, options, onChange, v, i) {
256         return (
257             <SelectOption
258                 key={`${i}-${v.replace(' ', '_')}`}
259                 intial={initial}
260                 defaultValue={v}
261                 options={options}
262                 onChange={onChange.bind(null, i)}
263             />
264         );
265     }
266     showInput() {
267
268     }
269     render() {
270         const props = this.props;
271         let inputType;
272         let className = "InputCollection";
273         if (props.className) {
274             className = `${className} ${props.className}`;
275         }
276         if (props.type == 'select') {
277             inputType = this.buildSelectOption.bind(this, props.initial, props.options, props.onChange);
278         } else {
279             inputType = this.buildTextInput.bind(this, props.onChange)
280         }
281         let html = (
282             <div className="InputCollection-wrapper">
283                 {props.collection.map((v,i) => {
284                     return (
285                         <div key={i} className={className} >
286                             {inputType(v, i)}
287                             <span onClick={props.RemoveItemFn.bind(null, i)} className="removeInput"><img src={imgRemove} />Remove</span>
288                         </div>
289                     )
290                 })}
291                 <span onClick={props.AddItemFn} className="addInput"><img src={imgAdd} />Add</span>
292             </div>
293         );
294         return html;
295     }
296 }
297
298 InputCollection.defaultProps = {
299     input: Input,
300     collection: [],
301     onChange: function(i, e) {
302         console.log(`
303                         Updating with: ${e.target.value}
304                         At index of: ${i}
305                     `)
306     },
307     AddItemFn: function(e) {
308         console.log(`Adding a new item to collection`)
309     },
310     RemoveItemFn: function(i, e) {
311         console.log(`Removing item from collection at index of: ${i}`)
312     }
313 }
314
315 class FormSection extends React.Component {
316     render() {
317         let className = 'FormSection ' + this.props.className;
318         let html = (
319             <div
320                 style={this.props.style}
321                 className={className}
322             >
323                 <div className="FormSection-title">
324                     {this.props.title}
325                 </div>
326                 <div className="FormSection-body">
327                     {this.props.children}
328                 </div>
329             </div>
330         );
331         return html;
332     }
333 }
334
335 FormSection.defaultProps = {
336     className: ''
337 }