User Management: Create and delete. Styling and enter key
[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      evaluateSubmit = (e) => {
104         if (e.keyCode == 13) {
105             if (this.props.isEdit) {
106                 this.updateUser(e);
107             } else {
108                 this.createUser(e);
109             }
110             e.preventDefault();
111             e.stopPropagation();
112         }
113     }
114     onTransitionEnd = (e) => {
115         this.actions.handleHideColumns(e);
116         console.log('transition end')
117     }
118     render() {
119         let self = this;
120         let html;
121         let props = this.props;
122         let state = this.state;
123         html = (
124             <PanelWrapper className={`row userManagement ${!this.state.userOpen ? 'userList-open' : ''}`} style={{'alignContent': 'center', 'flexDirection': 'row'}} >
125                 <PanelWrapper ref={(div) => { this.UserList = div}} className={`column userList expanded ${this.state.userOpen ? 'collapsed ' : ' '} ${this.state.hideColumns ? 'hideColumns ' : ' '}`}>
126                     <Panel title="User List" style={{marginBottom: 0}} no-corners>
127                         <div className="tableRow tableRow--header">
128                             <div className="userName">
129                                 Username
130                             </div>
131                             <div>
132                                 Domain
133                             </div>
134                         </div>
135                         {state.users && state.users.map((u, k) => {
136                             let platformRoles = [];
137                             for(let role in u.platformRoles) {
138                                 platformRoles.push(<div>{`${role}: ${u.platformRoles[role]}`}</div>)
139                             }
140                             return (
141                                 <div className={`tableRow tableRow--data ${((self.state.activeIndex == k) && self.state.userOpen) ? 'tableRow--data-active' : ''}`} key={k}>
142                                     <div
143                                         className={`userName userName-header ${((self.state.activeIndex == k) && self.state.userOpen) ? 'activeUser' : ''}`}
144                                         onClick={self.viewUser.bind(null, u, k)}>
145                                         {u['user-name']}
146                                     </div>
147                                     <div>
148                                         {u['user-domain']}
149                                     </div>
150
151
152                                 </div>
153                             )
154                         })}
155                     </Panel>
156                     <ButtonGroup  className="buttonGroup" style={{margin: '0 0.5rem 0.5rem', background: '#ddd', paddingBottom: '0.5rem'}}>
157                         <Button label="Add User" onClick={this.addUser} />
158                     </ButtonGroup>
159                 </PanelWrapper>
160                 <PanelWrapper onKeyUp={this.evaluateSubmit}
161                     className={`userAdmin column`}>
162                     <Panel
163                         title={state.isEdit ? state['user-name'] : 'Create User'}
164                         style={{marginBottom: 0}}
165                         hasCloseButton={this.closePanel}
166                         no-corners>
167                         <FormSection title="USER INFO">
168                         {
169                             this.state.isEdit ?
170                                 null
171                                 : <Input label="Username" value={state['user-name']} onChange={this.updateInput.bind(null, 'user-name')} />
172                         }
173                             <Input label="Domain" value={state['user-domain']}  onChange={this.updateInput.bind(null, 'user-domain')}></Input>
174                             <Input label="Disabled" onChange={this.disabledChange} checked={state.disabled} type="checkbox" />
175                         </FormSection>
176                         <FormSection title="PLATFORM ROLES">
177                             <Input label="Super Admin" onChange={this.platformChange.bind(null, 'super_admin')} checked={state.platformRoles.super_admin} type="checkbox" />
178                             <Input label="Platform Admin" onChange={this.platformChange.bind(null, 'platform_admin')}  checked={state.platformRoles.platform_admin} type="checkbox" />
179                             <Input label="Platform Oper" onChange={this.platformChange.bind(null, 'platform_oper')}  checked={state.platformRoles.platform_oper} type="checkbox" />
180                         </FormSection>
181                         <FormSection title="PROJECT ROLES">
182                             <InputCollection
183                                 inital={true}
184                                 type='select'
185                                 options={state.projectRolesOptions}
186                                 collection={state.projectRoles}
187                                 onChange={this.updateProjectRole}
188                                 AddItemFn={this.addProjectRole}
189                                 RemoveItemFn={this.removeProjectRole}
190                                 />
191                         </FormSection>
192                         { this.state.isEdit ?
193                             (
194                                 <FormSection title="PASSWORD CHANGE">
195                                     <Input label="OLD PASSWORD" type="password" value={state['old-password']} onChange={this.updateInput.bind(null, 'old-password')} />
196                                     <Input label="NEW PASSWORD" type="password" value={state['new-password']}  onChange={this.updateInput.bind(null, 'new-password')}/>
197                                     <Input label="REPEAT NEW PASSWORD" type="password"  value={state['confirm-password']}  onChange={this.updateInput.bind(null, 'confirm-password')}/>
198                                 </FormSection>
199                             ) :
200                             (
201                                 <FormSection title="CREATE PASSWORD">
202                                     <Input label="CREATE PASSWORD" type="password" value={state.newPassword}  onChange={this.updateInput.bind(null, 'new-password')}/>
203                                     <Input label="REPEAT PASSWORD" type="password"  value={state.repeatNewPassword}  onChange={this.updateInput.bind(null, 'confirm-password')}/>
204                                 </FormSection>
205                             )
206                         }
207
208                     </Panel>
209
210                         {
211                             state.isEdit ?
212                             (
213                                 <ButtonGroup className="buttonGroup">
214                                     <Button label="Update" type="submit" onClick={this.updateUser} />
215                                     <Button label="Delete" onClick={this.deleteUser} />
216                                 </ButtonGroup>
217                             )
218                             : (
219                                 <ButtonGroup className="buttonGroup">
220                                     <Button label="Create" type="submit" onClick={this.createUser}  />
221                                 </ButtonGroup>
222                             )
223                         }
224                 </PanelWrapper>
225
226             </PanelWrapper>
227         );
228         return html;
229     }
230 }
231 // onClick={this.Store.update.bind(null, Account)}
232 UserManagementDashboard.contextTypes = {
233     router: React.PropTypes.object
234 };
235
236 UserManagementDashboard.defaultProps = {
237     userList: [],
238     selectedUser: {}
239 }
240
241 export default SkyquakeComponent(UserManagementDashboard);
242
243
244
245
246 /**
247  * AddItemFn:
248  */
249 class InputCollection extends React.Component {
250     constructor(props) {
251         super(props);
252         this.collection = props.collection;
253     }
254     buildTextInput(onChange, v, i) {
255         return (
256             <Input
257                 style={{flex: '1 1'}}
258                 key={i}
259                 value={v}
260                 onChange= {onChange.bind(null, i)}
261             />
262         )
263     }
264     buildSelectOption(initial, options, onChange, v, i) {
265         return (
266             <SelectOption
267                 key={`${i}-${v.replace(' ', '_')}`}
268                 intial={initial}
269                 defaultValue={v}
270                 options={options}
271                 onChange={onChange.bind(null, i)}
272             />
273         );
274     }
275     showInput() {
276
277     }
278     render() {
279         const props = this.props;
280         let inputType;
281         let className = "InputCollection";
282         if (props.className) {
283             className = `${className} ${props.className}`;
284         }
285         if (props.type == 'select') {
286             inputType = this.buildSelectOption.bind(this, props.initial, props.options, props.onChange);
287         } else {
288             inputType = this.buildTextInput.bind(this, props.onChange)
289         }
290         let html = (
291             <div className="InputCollection-wrapper">
292                 {props.collection.map((v,i) => {
293                     return (
294                         <div key={i} className={className} >
295                             {inputType(v, i)}
296                             <span onClick={props.RemoveItemFn.bind(null, i)} className="removeInput"><img src={imgRemove} />Remove</span>
297                         </div>
298                     )
299                 })}
300                 <span onClick={props.AddItemFn} className="addInput"><img src={imgAdd} />Add</span>
301             </div>
302         );
303         return html;
304     }
305 }
306
307 InputCollection.defaultProps = {
308     input: Input,
309     collection: [],
310     onChange: function(i, e) {
311         console.log(`
312                         Updating with: ${e.target.value}
313                         At index of: ${i}
314                     `)
315     },
316     AddItemFn: function(e) {
317         console.log(`Adding a new item to collection`)
318     },
319     RemoveItemFn: function(i, e) {
320         console.log(`Removing item from collection at index of: ${i}`)
321     }
322 }
323
324 class FormSection extends React.Component {
325     render() {
326         let className = 'FormSection ' + this.props.className;
327         let html = (
328             <div
329                 style={this.props.style}
330                 className={className}
331             >
332                 <div className="FormSection-title">
333                     {this.props.title}
334                 </div>
335                 <div className="FormSection-body">
336                     {this.props.children}
337                 </div>
338             </div>
339         );
340         return html;
341     }
342 }
343
344 FormSection.defaultProps = {
345     className: ''
346 }