Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Change modal content to react component
Browse files Browse the repository at this point in the history
  • Loading branch information
Chao Li (MSRA) committed May 11, 2019
1 parent 0f9d57a commit 4bc196f
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 44 deletions.
109 changes: 109 additions & 0 deletions src/webportal/src/app/user/fabric/userView/InfoEditor.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import React, {useRef} from 'react';

import PropTypes from 'prop-types';

export default function InfoEditor({user: {username = '', admin = '', virtualCluster = '', hasGithubPAT = false}, updateUserAccount, updateUserVC, updateUserGithubPAT, hideEditUser}) {
const inputPassword = useRef(null);
const chkAdmin = useRef(null);
const chkVc = useRef(null);
const chkGithubPAT = useRef(null);

const handleUpdateAccount = (event) => {
const password = inputPassword.current.value;
const isAdmin = chkAdmin.current.checked;
updateUserAccount(username, password, isAdmin);
event.preventDefault();
};

const handleUpdateUserVC = (event) => {
const vcList = chkVc.current.value;
updateUserVC(username, vcList);
event.preventDefault();
};

const handleUpdateGithubPAT = (event) => {
const githubPAT = chkGithubPAT.current.value;
updateUserGithubPAT(username, githubPAT);
event.preventDefault();
};

// TODO: Remove bootstrap style when we have a new design
return (
<div className="modal-content">
<div className="modal-header">
<h4 className="modal-title">Edit information of {username}</h4>
</div>
<div className="modal-body">
<div className="box-header with-border user-edit-border">
<h3 className="box-title">Change Userinfo</h3>
</div>
<form id="form-update-account" className="form-register" onSubmit={handleUpdateAccount} >
<label htmlFor="inputPassword" className="sr-only">Password</label>
<input type="password" name="password" ref={inputPassword} id="update-account-input-password" className="form-control" placeholder="******" />
<div className="checkbox">
<label>
<input type="checkbox" name="admin" ref={chkAdmin} defaultChecked={admin === 'true' ? true : false} />
Admin user
</label>
</div>
<button className="btn btn-lg btn-primary btn-block" type="button" onClick={handleUpdateAccount}>Change Userinfo</button>
</form>
{admin !== 'true' &&
<React.Fragment>
<div className="box-header with-border user-edit-border">
<h3 className="box-title">Update Virtual Clusters</h3>
</div>
<form id="form-update-virtual-cluster" className="form-register" onSubmit={handleUpdateUserVC}>
<label htmlFor="inputVirtualCluster" className="sr-only">VirtualCluster</label>
<input type="text" name="virtualCluster" ref={chkVc} id="update-virtual-cluster-input-virtualCluster" className="form-control"
placeholder="Virtual Clusters (e.g. vc1,vc2)" defaultValue={virtualCluster} />
<button className="btn btn-lg btn-primary btn-block" type="button" onClick={handleUpdateUserVC}> Update Virtual Clusters</button>
</form>
</React.Fragment>
}
<div className="box-header with-border user-edit-border">
<h3 className="box-title">Update Github PAT</h3>
</div>
<form id="form-update-github-token" className="form-register" onSubmit={handleUpdateGithubPAT}>
<label htmlFor="inputGithubPAT" className="sr-only">GithubPAT</label>
<input type="text" name="githubPAT" ref={chkGithubPAT} id="update-github-token-input-githubPAT" className="form-control"
placeholder={hasGithubPAT ? '******' : 'N/A'} />
<button className="btn btn-lg btn-primary btn-block" type="button" onClick={handleUpdateGithubPAT}>Update Github PAT</button>
</form>
</div>
<div className="modal-footer">
<button type="button" className="btn btn-default" onClick={hideEditUser}>Close</button>
</div>
</div>
);
}

InfoEditor.propTypes = {
user: PropTypes.shape({
username: PropTypes.string,
admin: PropTypes.string,
virtualCluster: PropTypes.string,
hasGithubPAT: PropTypes.bool,
}),
updateUserAccount: PropTypes.func,
updateUserVC: PropTypes.func,
updateUserGithubPAT: PropTypes.func,
hideEditUser: PropTypes.func,
};
3 changes: 2 additions & 1 deletion src/webportal/src/app/user/fabric/userView/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export default function Table() {
name: 'Actions',
headerClassName: FontClassNames.medium,
columnActionsMode: ColumnActionsMode.disabled,
className: c([t.pa0, t.flex]),
onRender(user) {
/**
* @param {React.MouseEvent} event
Expand All @@ -127,7 +128,7 @@ export default function Table() {
editUser(user);
}
return (
<div className={c([t.dib, t.vMid, t.w100])} style={{margin: '-11px -8px -11px -12px'}} data-selection-disabled>
<div className={c([t.itemsCenter, t.flex])} data-selection-disabled>
<DefaultButton
onClick={onClick}
>
Expand Down
4 changes: 2 additions & 2 deletions src/webportal/src/app/user/fabric/userView/TopBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import React, {useContext, useMemo, useState} from 'react';

import {CommandBarButton, SearchBox, CommandBar, ContextualMenuItemType} from 'office-ui-fabric-react';
import {CommandBarButton, SearchBox, CommandBar, ContextualMenuItemType, ColorClassNames} from 'office-ui-fabric-react';
import {PropTypes} from 'prop-types';
import {findIndex} from 'lodash';

Expand Down Expand Up @@ -349,7 +349,7 @@ function TopBar() {
{active ? <CommandBar
items={filterBarItems}
farItems={filterBarFarItems}
styles={{root: {backgroundColor: '#ECECEC'}}}
styles={{root: [ColorClassNames.neutralLightBackground]}}
/> : null}
</React.Fragment>
);
Expand Down
61 changes: 20 additions & 41 deletions src/webportal/src/app/user/fabric/userView/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import Ordering from './Ordering';
import Filter from './Filter';
import Pagination from './Pagination';
import Paginator from './Paginator';
import InfoEditor from './InfoEditor';
import {getAllUsersRequest, removeUserRequest, updateUserVcRequest, updateUserAccountRequest, updateUserGithubPATRequest} from '../conn';

const userEditModalComponent = require('./user-edit-modal-component.ejs');
require('./user-edit-modal-component.scss');

initTheme();
Expand Down Expand Up @@ -71,6 +71,7 @@ export default function UserView() {

const [allUsers, setAllUsers] = useState([]);
const refreshAllUsers = () => {
setAllUsers([]);
getAllUsersRequest().then((data) => {
setAllUsers(data);
}).catch((err) => {
Expand Down Expand Up @@ -149,7 +150,6 @@ export default function UserView() {
showMessageBox({
text: message,
dismissedCallback: () => {
setAllUsers([]);
refreshAllUsers();
},
});
Expand All @@ -159,22 +159,14 @@ export default function UserView() {
});
};

const [showEditInfo, setShowEditInfo] = useState({isOpen: false, innerHtml: ''});
const [showEditInfo, setShowEditInfo] = useState({isOpen: false, user: {}});

const editUser = (user) => {
setShowEditInfo({
isOpen: true,
innerHtml: userEditModalComponent({
'username': user.username,
'isAdmin': user.admin,
'vcList': user.virtualCluster,
'hasGithubPAT': String(user.hasGithubPAT),
}),
});
setShowEditInfo({isOpen: true, user});
};

const hideEditUser = () => {
setShowEditInfo({isOpen: false, innerHtml: ''});
setShowEditInfo({isOpen: false, user: {}});
};

const updateUserInfoCallback = (data) => {
Expand All @@ -184,50 +176,31 @@ export default function UserView() {
showMessageBox({
text: 'Update user information successfully',
dismissedCallback: () => {
setShowEditInfo({isOpen: false, innerHtml: ''});
setAllUsers([]);
hideEditUser();
refreshAllUsers();
},
});
}
};

const updateUserVc = (username) => {
const virtualCluster = $('#form-update-virtual-cluster :input[name=virtualCluster]').val();
const updateUserVC = (username, virtualCluster) => {
updateUserVcRequest(username, virtualCluster)
.then(updateUserInfoCallback)
.catch((err) => {
$('#form-update-virtual-cluster').trigger('reset');
showMessageBox(err);
});
.catch(showMessageBox);
};

const updateUserAccount = (username) => {
const password = $('#form-update-account :input[name=password]').val();
const admin = $('#form-update-account :input[name=admin]').is(':checked') ? true : false;
const updateUserAccount = (username, password, admin) => {
updateUserAccountRequest(username, password, admin)
.then(updateUserInfoCallback)
.catch((err) => {
$('#form-update-account').trigger('reset');
showMessageBox(err);
});
.catch(showMessageBox);
};

const updateUserGithubPAT = (username) => {
const githubPAT = $('#form-update-github-token :input[name=githubPAT]').val();
const updateUserGithubPAT = (username, githubPAT) => {
updateUserGithubPATRequest(username, githubPAT)
.then(updateUserInfoCallback)
.catch((err) => {
$('#form-update-github-token').trigger('reset');
showMessageBox(err);
});
.catch(showMessageBox);
};

window.updateUserVc = updateUserVc;
window.updateUserAccount = updateUserAccount;
window.updateUserGithubPAT = updateUserGithubPAT;
window.hideEditUser = hideEditUser;

const context = {
allUsers,
refreshAllUsers,
Expand Down Expand Up @@ -266,8 +239,14 @@ export default function UserView() {
</Fabric>
<Modal
isOpen={showEditInfo.isOpen}
styles={{main: [t.mw8, t.w90]}}>
<div dangerouslySetInnerHTML={{__html: showEditInfo.innerHtml}} />
styles={{main: [{maxWidth: '600px'}, t.w90]}}>
{showEditInfo.isOpen &&
<InfoEditor
user={showEditInfo.user}
updateUserAccount={updateUserAccount}
updateUserVC={updateUserVC}
updateUserGithubPAT={updateUserGithubPAT}
hideEditUser={hideEditUser} />}
</Modal>
{loading.show && <MaskSpinnerLoading label={loading.text} />}
{messageBox.text && <MessageBox text={messageBox.text} onDismiss={hideMessageBox} confirm={messageBox.confirm} onOK={messageBox.okCallback} onCancel={messageBox.cancelCallback} />}
Expand Down

0 comments on commit 4bc196f

Please sign in to comment.