Advertisement
Guest User

Untitled

a guest
Oct 25th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.60 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import {connect} from "react-redux";
  3.  
  4. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
  5.  
  6. import BootstrapTable from 'react-bootstrap-table-next';
  7. import paginationFactory from 'react-bootstrap-table2-paginator';
  8. import ToolkitProvider, { Search } from 'react-bootstrap-table2-toolkit';
  9. import { Tree } from "@blueprintjs/core";
  10. import * as Classes from "@blueprintjs/core/lib/esm/common/classes";
  11. import {Col, Grid, Row} from "react-bootstrap";
  12. import FormUsers from "../Users/formUser";
  13.  
  14. import TablesGroupMembers from './tableGroupMembers';
  15.  
  16. import {
  17. getAllGroups,
  18. selectionGroup,
  19. getGroupMembers
  20. } from "../../actions/groups";
  21.  
  22. import {
  23. selectionUser,
  24. getAllUsers
  25. } from "../../actions/users";
  26.  
  27. import './ldapDir.css';
  28. import PropTypes from "prop-types";
  29.  
  30.  
  31. let expandedNodes = [];
  32. let selectedNode = "";
  33.  
  34. function getGroupNode(group){
  35. return {
  36. id: group.DN,
  37. hasCaret: (group.Children && group.Children.length > 0) ? true: false,
  38. isExpanded: expandedNodes.indexOf(group.DN)!==-1,
  39. isSelected: group.DN === selectedNode,
  40. icon: "folder-close",
  41. label: group.DN.split(",")[0].substring(3,group.DN.length-3),
  42. childNodes: (group.Children||[]).map((val,i) => {return getGroupNode(val)})
  43. }
  44. }
  45.  
  46. const mapStateToProps = state => {
  47.  
  48. return {
  49.  
  50. nodes : state.groups.map((val, i) => {
  51. return getGroupNode(val)
  52. }),
  53.  
  54. selectedUserId: state.selectedUserId,
  55. selectedUser: state.selectedUser,
  56.  
  57. selectedGroupId: state.selectedGroupId,
  58. selectedGroup: state.selectedGroup,
  59.  
  60. allUsers: state.users,
  61.  
  62. allMembers: state.members,
  63.  
  64. hasErrored: state.groupHasErrored
  65. };
  66.  
  67. };
  68.  
  69. const mapDispatchToProps = dispatch => {
  70.  
  71. return {
  72.  
  73. fetchAllGroups: () => dispatch(getAllGroups()), // getAllGroups
  74.  
  75. selectedGroup: groupDN => dispatch(selectionGroup(groupDN)),
  76.  
  77. fetchAllUsers: () => dispatch(getAllUsers()),
  78.  
  79. selectedUser: user => dispatch(selectionUser(user)), //getInfo relative ad un utente
  80.  
  81. //fetchMembers: groupDN => dispatch(getGroupMembers(groupDN))
  82.  
  83. fetchAllMembers: (groupDN) => dispatch(getGroupMembers(groupDN))
  84.  
  85.  
  86. }
  87.  
  88. };
  89.  
  90. class manageGroup extends Component {
  91.  
  92. constructor(props) {
  93.  
  94. super(props);
  95.  
  96. this.state = {...this.props};
  97.  
  98. this.state = {
  99. // selectedUserId: null,
  100. // selectedUser: null,
  101. selectedGroup: false,
  102. selectedGroupId: null,
  103.  
  104. members: [],
  105.  
  106. manageGroupInfoAlert: [
  107. "Per accedere alle operazioni consentite per la gestione del gruppo, bisogna prima selezionarne uno " +
  108. "dalla lista dei gruppi a sinistra."
  109. ],
  110.  
  111. };
  112.  
  113. this.handleNodeClick = this.handleNodeClick.bind(this);
  114.  
  115. }
  116.  
  117. forEachNode = (nodes, callback) => {
  118. if (nodes == null) {
  119. return;
  120. }
  121.  
  122. for (const node of nodes) {
  123. callback(node);
  124. this.forEachNode(node.childNodes== null ? []: node.childNodes, callback);
  125. }
  126. };
  127.  
  128. handleNodeCollapse = (nodeData) => {
  129. nodeData.isExpanded = false;
  130. nodeData.icon = "folder-close";
  131. let index =expandedNodes.indexOf(nodeData.id)
  132. if (index !== -1){
  133. expandedNodes.splice(index,1)
  134. }
  135.  
  136. this.setState(this.state)
  137. };
  138.  
  139. handleNodeExpand = (nodeData) => {
  140. nodeData.isExpanded = true;
  141. nodeData.icon = "folder-open";
  142. expandedNodes.push(nodeData.id)
  143. this.setState(this.state)
  144. };
  145.  
  146. handleSearch = (props,e) => {
  147.  
  148. let key = e.target.value.toLowerCase().trim();
  149. let nodes = this.props.nodes;
  150. let setVisible = function(node,li){
  151. let index = 0;
  152. delete node.visible;
  153. for(let n of node.childNodes){
  154.  
  155. let _li = function(){
  156. let ul = li.querySelector("ul");
  157. let liEle = ul?ul.children : null;
  158. if(liEle && liEle[index]){
  159. return liEle[index];
  160. }
  161. }();
  162. n.visible = setVisible(n,_li);
  163.  
  164. if(!node.visible){
  165. node.visible = n.visible;
  166. }
  167. index++;
  168. }
  169. if(!node.visible){
  170. node.visible = node.label.toLowerCase().match(key) != null;
  171. if(li)
  172. li.style.display = node.visible?"block":"none";
  173. }
  174.  
  175. return node.label.toLowerCase().match(key) != null;
  176. };
  177.  
  178. let liElements = document.getElementById("ldap-entry").querySelector("ul").children;
  179. let index = 0;
  180. for(let n of nodes){
  181. let li = liElements[index];
  182. setVisible(n,li);
  183. li.style.display = n.visible?"block":"none";
  184. index++;
  185. }
  186.  
  187. };
  188.  
  189. comparer = (otherArray) => {
  190. return function(current){
  191. return otherArray.filter(function(other){
  192. return other.Uid == current.Uid && other.display == current.display
  193. }).length == 0;
  194. }
  195. };
  196.  
  197. handleNodeClick = (nodeData, nodePath, e) => {
  198.  
  199. const originallySelected = nodeData.isSelected;
  200. if (!e.shiftKey) {
  201. this.forEachNode(this.props.nodes, n => (n.isSelected = false));
  202. }
  203. nodeData.isSelected = originallySelected == null ? true : !originallySelected;
  204. if (nodeData.isSelected){
  205. selectedNode = nodeData.id
  206. } else {
  207. selectedNode = ""
  208. }
  209.  
  210. // non so se servono altre info per ora gli passo solamente selectedNode così dall'altro componente è possibile fare le chiamate
  211. this.props.selectedGroup(selectedNode);
  212.  
  213. this.state.selectedGroup = true;
  214. this.state.selectedGroupId = selectedNode;
  215.  
  216. //this.setState(this.state);
  217. //this.props.fetchAllMembers(this.state.selectedGroupId);
  218.  
  219.  
  220.  
  221. console.log('STATE: ', this.state);
  222. console.log('PROPS: ', this.props);
  223.  
  224. debugger;
  225.  
  226. console.log(this.state);
  227.  
  228. // chiamata getMemebers => groupMembers
  229.  
  230. // notMember => allUsers - groupMembers
  231.  
  232. //console.log(this.state.selectedGroupId);
  233. //
  234. // return (
  235. // <Asd />
  236. // );
  237.  
  238.  
  239. // console.log('this.props: ', this.props);
  240. //
  241. // debugger;
  242. //
  243. // console.log('this.props.fetchMembers(this.state.selectedGroupId)', this.props.fetchMembers(this.state.selectedGroupId));
  244. //
  245. // console.log(this.props);
  246. //
  247. //
  248. // if ( this.props.fetchMembers(this.state.selectedGroupId) === undefined ) {
  249. // console.log('undefined');
  250. //
  251. // const members = [{
  252. // Cn: "dsdf",
  253. // DN: "cn=dsdf,ou=people,dc=ticketag,dc=it",
  254. // DisplayName: "",
  255. // Groups: null,
  256. // Mail: "nicholdngelucci@gmail.com",
  257. // Sn: "dsdf",
  258. // Uid: "nicholdngelucci@gmail.com",
  259. // }, {
  260. // Cn: "nicholasAngelucci",
  261. // DN: "cn=nicholasAngelucci,ou=people,dc=ticketag,dc=it",
  262. // DisplayName: "",
  263. // Groups: null,
  264. // Mail: "nissdgeludcci@ail.com",
  265. // Sn: "nicd",
  266. // Uid: "nissdgeludcci@ail.com"
  267. // },{
  268. // Cn: "nicholas",
  269. // DN: "cn=nicholas,ou=people,dc=ticketag,dc=it",
  270. // DisplayName: "",
  271. // Groups: null,
  272. // Mail: "nicholas.angelucci@gmail.com",
  273. // Sn: "nicholas",
  274. // Uid: "nicholas.angelucci@gmail.com"
  275. // }];
  276. //
  277. // let onlyInA = this.props.allUsers.filter(this.comparer(members));
  278. // let onlyInB = members.filter(this.comparer(this.props.allUsers));
  279. //
  280. // const result = onlyInA.concat(onlyInB);
  281. // console.log(result);
  282. //
  283. // this.state.addMember = result; // membri da aggiungere al gruppo
  284. // this.state.removeMember = members; // membi da rimuovere dal gruppo
  285. //
  286. // }
  287. //
  288. // debugger;
  289.  
  290. };
  291.  
  292. componentDidMount() {
  293. console.log('START');
  294. this.props.fetchAllGroups();
  295. this.props.fetchAllMembers('cn=chievo%20verona,cn=Milan,ou=groups,dc=ticketag,dc=it');
  296. this.props.fetchAllUsers();
  297. };
  298.  
  299. render() {
  300.  
  301. return (
  302.  
  303. <Row className="show-grid">
  304.  
  305. <Col xs={6} md={4} >
  306. <div key="menu">
  307. <code>{'<Col xs={6} md={4} />'};</code>
  308. <div className="Sidebar">
  309.  
  310. <h2>Lista gruppi</h2>
  311.  
  312. {console.log('Lista gruppi', this.props)}
  313.  
  314. <div className="sb-content">
  315. <div className="form-group">
  316. <input className="form-control" type="text" onKeyUp={this.handleSearch.bind(this,this.props.nodes)} placeholder="Cerca gruppo..." />
  317. </div>
  318. <div id="ldap-entry" className="ldap-dir">
  319. <Tree
  320. contents={this.props.nodes}
  321. onNodeClick={this.handleNodeClick}
  322. onNodeCollapse={this.handleNodeCollapse}
  323. onNodeExpand={this.handleNodeExpand}
  324. className={Classes.ELEVATION_0}
  325. />
  326. </div>
  327. </div>
  328. </div>
  329. </div>
  330. </Col>
  331.  
  332. <Col xs={6} md={4} >
  333. <code>{'<Col xs={6} md={4} />'};</code>
  334. <div key="manageGroup">
  335.  
  336. <h2>Gestione gruppo</h2>
  337. <p>Modifica ed elimina gruppo</p>
  338.  
  339. <span>
  340. <h2>Gestione utenti</h2>
  341. <div>
  342. {this.state.selectedGroup === false ?
  343. <div className="alert alert-info" role="alert">
  344. {this.state.manageGroupInfoAlert.map((p, index) => <p key={index}>{p}</p>)}
  345. </div> : <TablesGroupMembers groupDN={this.state.selectedGroupId}/>
  346. }
  347. </div>
  348. </span>
  349.  
  350. </div>
  351. </Col>
  352.  
  353. <Col xs={6} md={4}>
  354. <code>{'<Col xs={6} md={4} />'}</code>
  355. <div key="adduser">
  356.  
  357. </div>
  358. </Col>
  359.  
  360. </Row>
  361.  
  362. );
  363.  
  364. }
  365.  
  366. }
  367.  
  368. manageGroup.propTypes = {
  369. //fetchData: PropTypes.func.isRequired,
  370. groups: PropTypes.array,
  371. members: PropTypes.array,
  372. hasErrored: PropTypes.bool.isRequired,
  373. //isLoading: PropTypes.bool.isRequired
  374. };
  375.  
  376. const ManageUsers = connect(mapStateToProps, mapDispatchToProps)(manageGroup);
  377. export default ManageUsers;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement