CrudListPackage.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import React from 'react';
  2. import Modularity from 'modularity-front';
  3. import {Row,CardTitle,Button,Col,Card} from 'react-materialize';
  4. import ListModule from '../HigherModules/ListModule.js';
  5. import CrudService from '../Services/CrudService.js';
  6. import './CrudListPackage.css';
  7. let Module = Modularity.Module;
  8. class CrudListPackage extends Modularity.Module {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. service: null
  13. }
  14. this.headers = this.TableHeaders.value;
  15. let table = this.TableName.value;
  16. if (this.RouterDependable.value) { // Search route for table
  17. let route = this.RoutingSystem.getRoute();
  18. let regex = /\/[A-Za-z]+\/?/g;
  19. let found = route.url.match(regex);
  20. if(found.length){
  21. table = found[0].replace('/', '');
  22. }
  23. }
  24. let propertiesService = {
  25. Table: table,
  26. Token: this.Token.value,
  27. Service: (service) => this.updateService(service)
  28. }
  29. this.back_service = new CrudService(propertiesService);
  30. this.TableName.onUpdate((v) => { this.back_service.Table.update(v);this.forceUpdate(); });
  31. this.Token.onUpdate((v) => { this.back_service.Token.update(v);this.forceUpdate(); });
  32. }
  33. updateService(service) {
  34. if(service)
  35. this.calculateHeaders(service);
  36. }
  37. setFilters(filters) {
  38. if(filters) {
  39. this.back_service.Filters.update(filters);
  40. }
  41. this.filters = filters;
  42. this.forceUpdate();
  43. }
  44. buildFilters(headers) {
  45. let filters = [];
  46. for (var i in headers) {
  47. if( headers[i].filter ) {
  48. filters.push(headers[i].filter);
  49. }
  50. }
  51. return filters;
  52. }
  53. calculateHeaders(service, test) {
  54. service.model().then((model) => {
  55. let headers = {};
  56. let formHeaders = {};
  57. for (var name in model) {
  58. if(model[name].disabled) continue;
  59. let f = {
  60. Name: model[name].Name,
  61. type: model[name].type,
  62. required: !model[name].nullable,
  63. fieldName: name,
  64. filter: model[name].Filter
  65. };
  66. if(!name.includes("_id")
  67. && !(
  68. (model[name].Filter && model[name].Filter.type === "editor")
  69. || (model[name].type === "timestamp")))
  70. headers[model[name].Name || name] = f;
  71. formHeaders[model[name].Name || name] = f;
  72. }
  73. this.formHeaders = formHeaders;
  74. this.headers = headers;
  75. return headers;
  76. }).then((headers) => {
  77. if(!this.filters)
  78. this.filters = this.buildFilters(headers);
  79. this.forceUpdate();
  80. })
  81. }
  82. render() {
  83. let service = this.back_service.Service.value;
  84. return <div>
  85. <ListModule TableHeaders={this.headers} TableClass={"striped centered"} LiveEdit={this.LiveEdit.value}
  86. FormHeaders={this.formHeaders}
  87. perPage={this.perPage.value} service={service}
  88. filterStyle={{ }} filterClassName={"grey lighten-5"}
  89. FilterInput={this.filters} onFilters={(filters) => this.setFilters(filters)}/>
  90. {super.render()}
  91. </div>;
  92. }
  93. }
  94. CrudListPackage.Inputs = {
  95. RouterDependable: {
  96. type: Module.Types.Boolean,
  97. defaultValue: false
  98. },
  99. TableName: {
  100. type: Module.Types.Text,
  101. defaultValue: "products"
  102. },
  103. perPage: {
  104. type: Module.Types.Number,
  105. defaultValue: 2
  106. },
  107. TableHeaders:{
  108. type: Module.Types.Object,
  109. defaultValue:{
  110. "Id" : "id",
  111. "Name" : "name",
  112. "Value": "price"
  113. }
  114. },
  115. LiveEdit:{
  116. type: Module.Types.Boolean,
  117. defaultValue: false
  118. },
  119. Token : {
  120. type: Module.Types.Text,
  121. defaultValue: '123123'
  122. }
  123. }
  124. CrudListPackage.Outputs = {
  125. }
  126. CrudListPackage.Category = "NikatlasPackages";
  127. CrudListPackage.EditPosition = "center";
  128. export default CrudListPackage;