2 * STANDARD_RIFT_IO_COPYRIGHT
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';
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';
23 class UserManagementDashboard extends React.Component {
26 this.Store = this.props.flux.stores.hasOwnProperty('UserManagementStore') ? this.props.flux.stores.UserManagementStore : this.props.flux.createStore(UserManagementStore);
27 this.state = this.Store.getState();
28 this.actions = this.state.actions;
31 componentDidUpdate() {
33 ReactDOM.findDOMNode(this.UserList).addEventListener('transitionend', this.onTransitionEnd, false);
34 setTimeout(function() {
35 let element = self[`user-ref-${self.state.activeIndex}`]
36 element && !isElementInView(element) && element.scrollIntoView({block: 'end', behavior: 'smooth'});
39 componentWillMount() {
40 this.Store.listen(this.updateState);
41 this.Store.getUsers();
43 componentWillUnmount() {
44 this.Store.unlisten(this.updateState);
46 updateState = (state) => {
49 updateInput = (key, e) => {
51 this.actions.handleUpdateInput({
52 [property]:e.target.value
55 disabledChange = (e) => {
56 this.actions.handleDisabledChange(e.target.checked);
58 platformChange = (platformRole, e) => {
59 this.actions.handlePlatformRoleUpdate(platformRole, e.currentTarget.checked);
61 addProjectRole = (e) => {
62 this.actions.handleAddProjectItem();
64 removeProjectRole = (i, e) => {
65 this.actions.handleRemoveProjectItem(i);
67 updateProjectRole = (i, e) => {
68 this.actions.handleUpdateProjectRole(i, e)
71 this.actions.handleAddUser();
73 viewUser = (un, index) => {
74 this.actions.viewUser(un, index);
77 this.actions.editUser(false);
79 cancelEditUser = () => {
80 this.actions.editUser(true)
83 this.actions.handleCloseUserPanel();
85 // updateUser = (e) => {
86 // e.preventDefault();
87 // e.stopPropagation();
89 // this.Store.updateUser();
94 this.Store.deleteUser({
95 'user-name': this.state['user-name'],
96 'user-domain': this.state['user-domain']
102 if(this.state['new-password'] != this.state['confirm-password']) {
103 this.props.actions.showNotification('Passwords do not match')
105 this.Store.createUser({
106 'user-name': this.state['user-name'],
107 'user-domain': this.state['user-domain'],
108 'password': this.state['new-password']
109 // 'confirm-password': this.state['confirm-password']
113 updateUser = (e) => {
117 let validatedPasswords = validatePasswordFields(this.state);
118 if(validatedPasswords) {
119 this.Store.updateUser(_.merge({
120 'user-name': this.state['user-name'],
121 'user-domain': this.state['user-domain'],
122 'password': this.state['new-password']
125 function validatePasswordFields(state) {
126 let oldOne = state['old-password'];
127 let newOne = state['new-password'];
128 let confirmOne = state['confirm-password'];
130 if(oldOne == newOne) {
131 self.props.actions.showNotification('Your new password must not match your old one');
134 if(newOne != confirmOne) {
135 self.props.actions.showNotification('Passwords do not match');
139 // 'old-password': oldOne,
140 'new-password': newOne,
141 'confirm-password': confirmOne
148 evaluateSubmit = (e) => {
149 if (e.keyCode == 13) {
150 if (this.props.isEdit) {
159 onTransitionEnd = (e) => {
160 this.actions.handleHideColumns(e);
161 console.log('transition end')
163 disableChange = (e) => {
164 let value = e.target.value;
165 value = value.toUpperCase();
176 let props = this.props;
177 let state = this.state;
178 let passwordSectionHTML = null;
179 let formButtonsHTML = (
180 <ButtonGroup className="buttonGroup">
181 <Button label="EDIT" type="submit" onClick={this.editUser} />
184 if(!this.state.isReadOnly) {
185 passwordSectionHTML = ( this.state.isEdit ?
187 <FormSection title="PASSWORD CHANGE">
188 <Input label="NEW PASSWORD" type="password" value={state['new-password']} onChange={this.updateInput.bind(null, 'new-password')}/>
189 <Input label="REPEAT NEW PASSWORD" type="password" value={state['confirm-password']} onChange={this.updateInput.bind(null, 'confirm-password')}/>
193 <FormSection title="CREATE PASSWORD">
194 <Input label="CREATE PASSWORD" type="password" value={state.newPassword} onChange={this.updateInput.bind(null, 'new-password')}/>
195 <Input label="REPEAT PASSWORD" type="password" value={state.repeatNewPassword} onChange={this.updateInput.bind(null, 'confirm-password')}/>
202 <ButtonGroup className="buttonGroup">
203 <Button label="Update" type="submit" onClick={this.updateUser} />
204 <Button label="Delete" onClick={this.deleteUser} />
205 <Button label="Cancel" onClick={this.cancelEditUser} />
209 <ButtonGroup className="buttonGroup">
210 <Button label="Create" type="submit" onClick={this.createUser} />
217 <PanelWrapper column>
218 <AppHeader nav={[{name: 'PLATFORM ROLE MANAGEMENT', onClick: this.context.router.push.bind(this, {pathname: '/platform'})}]}/>
219 <PanelWrapper className={`row userManagement ${!this.state.userOpen ? 'userList-open' : ''}`} style={{'flexDirection': 'row'}} >
220 <PanelWrapper ref={(div) => { this.UserList = div}} className={`column userList expanded ${this.state.userOpen ? 'collapsed ' : ' '} ${this.state.hideColumns ? 'hideColumns ' : ' '}`}>
221 <Panel title="User List" style={{marginBottom: 0}} no-corners>
222 <div className="tableRow tableRow--header">
223 <div className="userName">
230 {state.users && state.users.map((u, k) => {
231 let platformRoles = [];
232 for(let role in u.platformRoles) {
233 platformRoles.push(<div>{`${role}: ${u.platformRoles[role]}`}</div>)
236 <div ref={(el) => this[`user-ref-${k}`] = el} className={`tableRow tableRow--data ${((self.state.activeIndex == k) && self.state.userOpen) ? 'tableRow--data-active' : ''}`}
238 onClick={self.viewUser.bind(null, u, k)}>
240 className={`userName userName-header ${((self.state.activeIndex == k) && self.state.userOpen) ? 'activeUser' : ''}`}
253 <ButtonGroup className="buttonGroup" style={{margin: '0 0.5rem 0.5rem', background: '#ddd', paddingBottom: '0.5rem'}}>
254 <Button label="Add User" onClick={this.addUser} />
257 <PanelWrapper onKeyUp={this.evaluateSubmit}
258 className={`userAdmin column`}>
260 title={state.isEdit ? state['user-name'] : 'Create User'}
261 style={{marginBottom: 0}}
262 hasCloseButton={this.closePanel}
264 <FormSection title="USER INFO">
268 : <Input readonly={state.isReadOnly} label="Username" value={state['user-name']} onChange={this.updateInput.bind(null, 'user-name')} />
270 <Input readonly={true} label="Domain" value={state['user-domain']} onChange={this.updateInput.bind(null, 'user-domain')}></Input>
272 <Input type="radiogroup" readonly={state.isReadOnly} label="Disabled" value={state.disabled} options={[{value: true, label: 'YES'}, {value: false, label: 'NO'}]} onChange={this.disableChange} />
274 <FormSection title="PLATFORM ROLES" style={{display:'none'}}>
275 <Input label="Super Admin" onChange={this.platformChange.bind(null, 'super_admin')} checked={state.platformRoles.super_admin} type="checkbox" />
276 <Input label="Platform Admin" onChange={this.platformChange.bind(null, 'platform_admin')} checked={state.platformRoles.platform_admin} type="checkbox" />
277 <Input label="Platform Oper" onChange={this.platformChange.bind(null, 'platform_oper')} checked={state.platformRoles.platform_oper} type="checkbox" />
279 <FormSection title="PROJECT ROLES" style={{display:'none'}}>
283 readonly={state.isReadOnly}
284 options={state.projectRolesOptions}
285 collection={state.projectRoles}
286 onChange={this.updateProjectRole}
287 AddItemFn={this.addProjectRole}
288 RemoveItemFn={this.removeProjectRole}
291 {passwordSectionHTML}
302 // onClick={this.Store.update.bind(null, Account)}
303 UserManagementDashboard.contextTypes = {
304 router: React.PropTypes.object
307 UserManagementDashboard.defaultProps = {
312 export default SkyquakeComponent(UserManagementDashboard);
315 function isElementInView(el) {
316 var rect = el && el.getBoundingClientRect() || {};
321 rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
322 rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
327 // isReadOnly={state.isReadOnly} disabled={state.disabled} onChange={this.disableChange}
329 class isDisabled extends React.Component {
334 let props = this.props;
342 class InputCollection extends React.Component {
345 this.collection = props.collection;
347 buildTextInput(onChange, v, i) {
350 readonly={this.props.readonly}
351 style={{flex: '1 1'}}
354 onChange= {onChange.bind(null, i)}
358 buildSelectOption(initial, options, onChange, v, i) {
361 readonly={this.props.readonly}
362 key={`${i}-${v.replace(' ', '_')}`}
366 onChange={onChange.bind(null, i)}
374 const props = this.props;
376 let className = "InputCollection";
377 if (props.className) {
378 className = `${className} ${props.className}`;
380 if (props.type == 'select') {
381 inputType = this.buildSelectOption.bind(this, props.initial, props.options, props.onChange);
383 inputType = this.buildTextInput.bind(this, props.onChange)
386 <div className="InputCollection-wrapper">
387 {props.collection.map((v,i) => {
389 <div key={i} className={className} >
392 props.readonly ? null : <span onClick={props.RemoveItemFn.bind(null, i)} className="removeInput"><img src={imgRemove} />Remove</span>}
396 { props.readonly ? null : <span onClick={props.AddItemFn} className="addInput"><img src={imgAdd} />Add</span>}
403 InputCollection.defaultProps = {
406 onChange: function(i, e) {
408 Updating with: ${e.target.value}
412 AddItemFn: function(e) {
413 console.log(`Adding a new item to collection`)
415 RemoveItemFn: function(i, e) {
416 console.log(`Removing item from collection at index of: ${i}`)
420 class FormSection extends React.Component {
422 let className = 'FormSection ' + this.props.className;
425 style={this.props.style}
426 className={className}
428 <div className="FormSection-title">
431 <div className="FormSection-body">
432 {this.props.children}
440 FormSection.defaultProps = {