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 className={`row userManagement ${!this.state.userOpen ? 'userList-open' : ''}`} style={{'flexDirection': 'row'}} >
218 <PanelWrapper ref={(div) => { this.UserList = div}} className={`column userList expanded ${this.state.userOpen ? 'collapsed ' : ' '} ${this.state.hideColumns ? 'hideColumns ' : ' '}`}>
219 <Panel title="User List" style={{marginBottom: 0}} no-corners>
220 <div className="tableRow tableRow--header">
221 <div className="userName">
228 {state.users && state.users.map((u, k) => {
229 let platformRoles = [];
230 for(let role in u.platformRoles) {
231 platformRoles.push(<div>{`${role}: ${u.platformRoles[role]}`}</div>)
234 <div ref={(el) => this[`user-ref-${k}`] = el} className={`tableRow tableRow--data ${((self.state.activeIndex == k) && self.state.userOpen) ? 'tableRow--data-active' : ''}`} key={k}>
236 className={`userName userName-header ${((self.state.activeIndex == k) && self.state.userOpen) ? 'activeUser' : ''}`}
237 onClick={self.viewUser.bind(null, u, k)}>
249 <ButtonGroup className="buttonGroup" style={{margin: '0 0.5rem 0.5rem', background: '#ddd', paddingBottom: '0.5rem'}}>
250 <Button label="Add User" onClick={this.addUser} />
253 <PanelWrapper onKeyUp={this.evaluateSubmit}
254 className={`userAdmin column`}>
256 title={state.isEdit ? state['user-name'] : 'Create User'}
257 style={{marginBottom: 0}}
258 hasCloseButton={this.closePanel}
260 <FormSection title="USER INFO">
264 : <Input readonly={state.isReadOnly} label="Username" value={state['user-name']} onChange={this.updateInput.bind(null, 'user-name')} />
266 <Input readonly={true} label="Domain" value={state['user-domain']} onChange={this.updateInput.bind(null, 'user-domain')}></Input>
268 <Input type="radiogroup" readonly={state.isReadOnly} label="Disabled" value={state.disabled} options={[{value: true, label: 'YES'}, {value: false, label: 'NO'}]} onChange={this.disableChange} />
270 <FormSection title="PLATFORM ROLES" style={{display:'none'}}>
271 <Input label="Super Admin" onChange={this.platformChange.bind(null, 'super_admin')} checked={state.platformRoles.super_admin} type="checkbox" />
272 <Input label="Platform Admin" onChange={this.platformChange.bind(null, 'platform_admin')} checked={state.platformRoles.platform_admin} type="checkbox" />
273 <Input label="Platform Oper" onChange={this.platformChange.bind(null, 'platform_oper')} checked={state.platformRoles.platform_oper} type="checkbox" />
275 <FormSection title="PROJECT ROLES" style={{display:'none'}}>
279 readonly={state.isReadOnly}
280 options={state.projectRolesOptions}
281 collection={state.projectRoles}
282 onChange={this.updateProjectRole}
283 AddItemFn={this.addProjectRole}
284 RemoveItemFn={this.removeProjectRole}
287 {passwordSectionHTML}
300 // onClick={this.Store.update.bind(null, Account)}
301 UserManagementDashboard.contextTypes = {
302 router: React.PropTypes.object
305 UserManagementDashboard.defaultProps = {
310 export default SkyquakeComponent(UserManagementDashboard);
313 function isElementInView(el) {
314 var rect = el && el.getBoundingClientRect() || {};
319 rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
320 rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
325 // isReadOnly={state.isReadOnly} disabled={state.disabled} onChange={this.disableChange}
327 class isDisabled extends React.Component {
332 let props = this.props;
340 class InputCollection extends React.Component {
343 this.collection = props.collection;
345 buildTextInput(onChange, v, i) {
348 readonly={this.props.readonly}
349 style={{flex: '1 1'}}
352 onChange= {onChange.bind(null, i)}
356 buildSelectOption(initial, options, onChange, v, i) {
359 readonly={this.props.readonly}
360 key={`${i}-${v.replace(' ', '_')}`}
364 onChange={onChange.bind(null, i)}
372 const props = this.props;
374 let className = "InputCollection";
375 if (props.className) {
376 className = `${className} ${props.className}`;
378 if (props.type == 'select') {
379 inputType = this.buildSelectOption.bind(this, props.initial, props.options, props.onChange);
381 inputType = this.buildTextInput.bind(this, props.onChange)
384 <div className="InputCollection-wrapper">
385 {props.collection.map((v,i) => {
387 <div key={i} className={className} >
390 props.readonly ? null : <span onClick={props.RemoveItemFn.bind(null, i)} className="removeInput"><img src={imgRemove} />Remove</span>}
394 { props.readonly ? null : <span onClick={props.AddItemFn} className="addInput"><img src={imgAdd} />Add</span>}
401 InputCollection.defaultProps = {
404 onChange: function(i, e) {
406 Updating with: ${e.target.value}
410 AddItemFn: function(e) {
411 console.log(`Adding a new item to collection`)
413 RemoveItemFn: function(i, e) {
414 console.log(`Removing item from collection at index of: ${i}`)
418 class FormSection extends React.Component {
420 let className = 'FormSection ' + this.props.className;
423 style={this.props.style}
424 className={className}
426 <div className="FormSection-title">
429 <div className="FormSection-body">
430 {this.props.children}
438 FormSection.defaultProps = {