Guest User

Untitled

a guest
Jul 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.83 KB | None | 0 0
  1. import React, { Component } from 'react'
  2.  
  3. import { withTracker } from 'meteor/react-meteor-data'
  4. import { Link } from 'react-router-dom'
  5.  
  6. import { ModulesCollection } from '/imports/api/ModulesCollection'
  7. import ModulesCreate from '/client/imports/admin/ModulesCreate'
  8.  
  9. import { CompanyCollection } from '/imports/api/CompanyCollection'
  10.  
  11. import { Template } from 'meteor/templating';
  12.  
  13. import classnames from 'classnames';
  14.  
  15. import Utils from '/client/utils/utils';
  16.  
  17. class Modules extends Component {
  18. constructor(props) {
  19. super(props)
  20. this.state = {
  21. isCreatingModules: false,
  22. itemToDelete: '',
  23. itemToDeleteId: '',
  24. itemToEdit: {},
  25. itemToView: {},
  26. _id: '',
  27. companyId: '',
  28. companyName: '',
  29. moduleBelongTo: '',
  30. moduleBelongToId: '',
  31. name: '',
  32. link: '',
  33. icon: '',
  34. description: '',
  35. order: '',
  36. }
  37. this.itemToEdit = {
  38. name: 'no'
  39. };
  40. this.toggleCreateState = this.toggleCreateState.bind(this);
  41. this.txtClass = 'inputNormal';
  42. this.emailValidator = false;
  43. }
  44.  
  45. renderModulesTable(modules, padreId) {
  46.  
  47. _.forEach(modules, function (item, index) {
  48. //console.log('index', index);
  49. // console.log('entre', item);
  50. // console.log('pad', padreId);
  51.  
  52. if (item.moduleBelongToId == padreId){
  53.  
  54. console.log('entre2', item.companyName);
  55. return (<tr><td>hl</td></tr>)
  56. return ( <tr key={index}>
  57. <th scope="row">{index + 1}</th>
  58. <td>{item.companyName}</td>
  59. <td>{item.name}</td>
  60. <td>{item.link}</td>
  61. <td scope="col">
  62. <button className="btn-close" type="button"
  63. onClick={(e) =>{
  64. this.setState({itemToView: item});
  65. $('#modalView').modal('show');
  66.  
  67. }}>
  68. <i className="fa fa-eye"></i>
  69. </button>
  70. <button className="btn-close" type="button"
  71. onClick={(e) =>{
  72. this.itemToEdit = item;
  73. this.setState({_id: item._id});
  74. this.setState({name: item.name});
  75. this.setState({link: item.link});
  76. this.setState({icon: item.icon});
  77.  
  78. $('#modalEdit').modal('show');
  79.  
  80. }}>
  81. <i className="fa fa-pencil"></i>
  82. </button>
  83. <button className="btn-close" type="button" onClick={(e) =>{
  84. this.state.itemToDelete = item.name;
  85. this.state.itemToDeleteId = item._id;
  86. this.setState({itemToDelete: this.state.itemToDelete});
  87. $('#modalDelete').modal('show');
  88.  
  89. }}><i className="fa fa-times"></i>
  90. </button>
  91. </td>
  92. </tr>)
  93. }
  94. })
  95. }
  96.  
  97. renderModulesTable2(modules, padreId) {
  98.  
  99. return modules.map( (item, index) => (
  100. <tr key={index}>
  101. <th scope="row">{index + 1}</th>
  102. <td>{item.companyName}</td>
  103. <td>{item.name}</td>
  104. <td>{item.link}</td>
  105. <td scope="col">
  106. <button className="btn-close" type="button"
  107. onClick={(e) =>{
  108. this.setState({itemToView: item});
  109. $('#modalView').modal('show');
  110.  
  111. }}>
  112. <i className="fa fa-eye"></i>
  113. </button>
  114. <button className="btn-close" type="button"
  115. onClick={(e) =>{
  116. this.itemToEdit = item;
  117. this.setState({_id: item._id});
  118. this.setState({name: item.name});
  119. this.setState({link: item.link});
  120. this.setState({icon: item.icon});
  121.  
  122. $('#modalEdit').modal('show');
  123.  
  124. }}>
  125. <i className="fa fa-pencil"></i>
  126. </button>
  127. <button className="btn-close" type="button" onClick={(e) =>{
  128. this.state.itemToDelete = item.name;
  129. this.state.itemToDeleteId = item._id;
  130. this.setState({itemToDelete: this.state.itemToDelete});
  131. $('#modalDelete').modal('show');
  132.  
  133. }}><i className="fa fa-times"></i>
  134. </button>
  135. </td>
  136. </tr>
  137.  
  138. ))
  139. }
  140.  
  141. toggleCreateState(e) {
  142.  
  143. let toggle = !this.state.isCreatingModules
  144. this.setState({isCreatingModules: toggle})
  145. }
  146.  
  147. renderCreateModulesArea(){
  148. if (!this.state.isCreatingModules) {
  149. return (
  150. <button className="btn btn-primary btn-create" onClick={(e)=>{e.preventDefault(); this.setState({isCreatingModules:true})}}>Crear Modulo <i className="fa fa-plus"></i></button>
  151. )
  152. } else {
  153. return <ModulesCreate
  154. handler={this.toggleCreateState}
  155. company = { this.props.company }
  156. modules = { this.props.modules }
  157. roles ={this.props.roles}
  158. />
  159. }
  160. }
  161.  
  162. handleModulesSubmit(e) {
  163. e.preventDefault()
  164. let companyData = this.state
  165. console.log('send', companyData);
  166.  
  167. Meteor.call("company.update", companyData, function(error){
  168. if(error){
  169. Bert.alert({
  170. title: 'Error',
  171. message: error.reason,
  172. type: 'danger',
  173. style: 'growl-top-right',
  174. icon: 'fa-times'
  175. });
  176. } else {
  177. Bert.alert({
  178. title: 'Modulo editada',
  179. message: companyData.name + ' fue editada!',
  180. type: 'info',
  181. style: 'growl-top-right',
  182. icon: 'fa-users'
  183. });
  184. }
  185. });
  186. // this.props.handler()
  187. $('#modalEdit').modal('hide');
  188. }
  189.  
  190. handleModulesSettingsSubmit(e) {
  191. e.preventDefault()
  192. let companyData = this.state
  193. console.log('send', companyData);
  194.  
  195. Meteor.call("company.updateSettings", companyData, function(error){
  196. if(error){
  197. Bert.alert({
  198. title: 'Error',
  199. message: error.reason,
  200. type: 'danger',
  201. style: 'growl-top-right',
  202. icon: 'fa-times'
  203. });
  204. } else {
  205. Bert.alert({
  206. title: 'Configuración de empresa editada',
  207. message: companyData.name + ' fue editada!',
  208. type: 'info',
  209. style: 'growl-top-right',
  210. icon: 'fa-users'
  211. });
  212. }
  213. });
  214. // this.props.handler()
  215. $('#modalSettings').modal('hide');
  216. }
  217.  
  218. render() {
  219. let txtClass = 'inputNormal';
  220. let modules = this.props.modules
  221. return (
  222. <div role="main" className="col-sm-9 ml-sm-auto col-md-10 pt-3">
  223. <h1>Modulos</h1>
  224. <nav aria-label="breadcrumb" role="navigation">
  225. <ol className="breadcrumb">
  226. <li className="breadcrumb-item"><Link to="/admin">Administración</Link></li>
  227. <li className="breadcrumb-item active" aria-current="page">Modulos</li>
  228. </ol>
  229. </nav>
  230. {this.renderCreateModulesArea()}
  231. <table className="table">
  232. <thead>
  233. <tr>
  234. <th scope="col">#</th>
  235. <th scope="col">Empresa</th>
  236. <th scope="col">Nombre</th>
  237. <th scope="col">Enlace principal</th>
  238. <th scope="col"></th>
  239. </tr>
  240. </thead>
  241. <tbody>
  242. {
  243.  
  244. _.forEach(modules, function(item, index) {
  245. console.log('itemes', item)
  246. if(item.moduleBelongToId == 0){
  247. console.log('item', item);
  248. // this.renderModulesTable(modules, item._Id)
  249.  
  250. }
  251. })
  252. }
  253. {
  254. modules.forEach((item)=>{
  255. if(item.moduleBelongToId == 0){
  256. console.log('i1', this.renderModulesTable(modules, 0));
  257. console.log('i2', this.renderModulesTable2(modules, item._id));
  258. //this.renderModulesTable2(modules, item._id);
  259. this.renderModulesTable(modules, 0)
  260. }
  261. })
  262. }
  263. {
  264. this.renderModulesTable(modules, 0)
  265.  
  266. }
  267. </tbody>
  268. </table>
  269.  
  270. <div id="modalView" className="modal fade">
  271. <div className="modal-dialog modal-md">
  272. <div className="modal-content">
  273.  
  274. <div className="modal-header">
  275. <h4 className="modal-title">Modulo</h4>
  276. </div>
  277.  
  278. <div className="modal-body">
  279. <p><strong>Empresa:</strong> { this.state.itemToView.companyName }</p>
  280. <p><strong>Nombre:</strong> { this.state.itemToView.name }</p>
  281. <p><strong>Enlace:</strong> { this.state.itemToView.link }</p>
  282. <p><strong>Módulo padre:</strong> { this.state.itemToView.moduleBelongTo }</p>
  283. <p><strong>Icono:</strong> { this.state.itemToView.icon }</p>
  284. <p><strong>Orden:</strong> { this.state.itemToView.order }</p>
  285. <p><strong>Descripción:</strong> { this.state.itemToView.description }</p>
  286.  
  287. </div>
  288. <div className="modal-footer">
  289. <button type="button" className="btn btn-default" data-dismiss="modal">Cerrar</button>
  290. </div>
  291.  
  292. </div>
  293. </div>
  294. </div>
  295.  
  296.  
  297. <div id="modalEdit" className="modal fade">
  298. <div className="modal-dialog modal-lg">
  299. <div className="modal-content">
  300.  
  301. <div className="modal-header">
  302. <h4 className="modal-title">Editar módulo </h4>
  303. </div>
  304.  
  305. <div className="modal-body">
  306.  
  307. <form onSubmit={this.handleModulesSubmit.bind(this)}>
  308. <div className="row">
  309. <div className="col-5">
  310. <div className="form-group">
  311. <label>Nombre</label>
  312. <input maxLength="270" type="text" className="form-control" required value={this.state.name}
  313. onChange={(e)=>{e.preventDefault(); this.setState({name: e.target.value})}}
  314. />
  315. </div>
  316. </div>
  317. <div className="col-7">
  318. <div className="form-group">
  319. <label>Enlace</label>
  320. <input maxLength="270" type="text" className="form-control" required value={this.state.link}
  321. onChange={(e)=>{e.preventDefault(); this.setState({link: e.target.value})}}/>
  322. </div>
  323. </div>
  324. </div>
  325.  
  326. <div className="modal-footer">
  327. <button type="submit" className="btn btn-primary">Editar</button>
  328. <button type="button" className="btn btn-default" data-dismiss="modal">Cancelar</button>
  329. </div>
  330. </form>
  331.  
  332. </div>
  333.  
  334. </div>
  335. </div>
  336. </div>
  337.  
  338. <div id="modalDelete" className="modal fade">
  339. <div className="modal-dialog modal-lg">
  340. <div className="modal-content">
  341.  
  342. <div className="modal-header">
  343. <h4 className="modal-title">Eliminar</h4>
  344. </div>
  345.  
  346. <div className="modal-body">
  347. <label htmlFor="name">Esta seguro que desea eliminar { this.state.itemToDelete }</label>
  348. </div>
  349.  
  350. <div className="modal-footer">
  351. <button type="button" className="btn btn-primary" id="save" onClick={(e) =>{
  352. Meteor.call("company.delete", this.state.itemToDeleteId, function(error){
  353. if(error){
  354. Bert.alert({
  355. title: 'Error',
  356. message: error.reason,
  357. type: 'danger',
  358. style: 'growl-top-right',
  359. icon: 'fa-times'
  360. });
  361. } else {
  362. Bert.alert({
  363. title: 'Modulo Desactivada',
  364. message: 'La empresa se desactivó con exito!',
  365. type: 'info',
  366. style: 'growl-top-right',
  367. icon: 'fa-users'
  368. });
  369. }
  370. });
  371. // this.props.handler();
  372. //
  373. $('#modalDelete').modal('hide');
  374.  
  375. }}>Aceptar</button>
  376. <button type="button" className="btn btn-default" data-dismiss="modal">Cancel</button>
  377. </div>
  378.  
  379. </div>
  380. </div>
  381. </div>
  382.  
  383. </div>
  384.  
  385.  
  386. )
  387. }
  388. }
  389.  
  390. export default withTracker(props => {
  391. const modulesSubscription = Meteor.subscribe("ModulesCollection")
  392. const empSubscription = Meteor.subscribe('CompanyCollection')
  393. const rolesSubscription = Meteor.subscribe("allRoles")
  394. const allReady = modulesSubscription.ready() && empSubscription.ready() && rolesSubscription.ready()
  395. const loading = modulesSubscription ? !allReady : true
  396.  
  397. return {
  398. loading,
  399. modules: ModulesCollection.find().fetch(),
  400. company: CompanyCollection.find().fetch(),
  401. roles: Meteor.roles.find().fetch(),
  402. }
  403. })(Modules)
Add Comment
Please, Sign In to add comment