Proyecto en colaboración con OPASO

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Authors : Carlos C. Corrada-Bravo
  2. David J. Ortiz-Rivera
  3. Organization : Centro de Desarrollo y Consultoria Computacional
  4. Project : OPASO Material Registry
  5. File : requests.js
  6. Description : Generate requests table */
  7. /* global values */
  8. var i;
  9. var person_id;
  10. var requested = {};
  11. next = 0;
  12. /* fetch requested materials */
  13. $(document).ready(fetch_requests);
  14. /* set_listeners() - listen for events */
  15. function set_listeners(){
  16. /* confirm request */
  17. $(".action").click(function(){
  18. i = $(this).attr("value");
  19. type = $(this).attr("id");
  20. $.post("/scripts/opaso",{query: 30,type: type,material: JSON.stringify(requested[i])},function(data){
  21. // console.log(data);
  22. /* extract response */
  23. let response = JSON.parse(data);
  24. let status = response["status"];
  25. /* handle by status */
  26. switch(status){
  27. case "expired": /* on expired, redirect to index */
  28. window.location.href = "/?error=session_expired";
  29. break;
  30. case "success": /* on success, delete row */
  31. $(`#${i}`).remove();
  32. next -= 1;
  33. $(".table-total").text(`Total: ${next}`);
  34. default: /* on success/error, display message */
  35. set_alert(status,response["message"]);
  36. break;
  37. }
  38. });
  39. });
  40. }
  41. /* table_gen(data: dictionary) - generate table body */
  42. function table_gen(data){
  43. for(var d in data){
  44. let tr = element_gen("tr",{id: d});
  45. let fs = data[d];
  46. for(var f in fs){
  47. switch(f){
  48. case "person": /* extract id */
  49. var attrs = {person_id: fs[f]["person_id"],value: "person_name", text: fs[f]["person_name"]};
  50. break;
  51. case "manufacturer": /* extract id */
  52. var attrs = {man_id: fs[f]["man_id"],value: "man_name", text: fs[f]["man_name"]};
  53. break;
  54. case "material": /* extract id */
  55. var href = `/materials?mat_id=${fs[f]["mat_id"]}`;
  56. var link = element_gen("a",{class: "mat-link",text: fs[f]["mat_name"],target: "_blank",rel: "noopener noreferrer",href: href});
  57. var attrs = {value: "mat_name",childs: {link}};
  58. break;
  59. case "prev_lab": /* extract id */
  60. case "new_lab": /* extract id */
  61. var href = `/laboratory?lab_id=${fs[f]["lab_id"]}`;
  62. var link = element_gen("a",{class: "lab-link",target: "_blank",rel: "noopener noreferrer",text: fs[f]["lab_room"],href: href});
  63. var attrs = {value: f,childs: {link}};
  64. break;
  65. default: /* default */
  66. var attrs = {text: fs[f],value: f};
  67. break;
  68. }
  69. var td = element_gen("td",attrs);
  70. tr.append(td);
  71. }
  72. let ai =element_gen("i",{class: "request-icon material-icons", text: "check"});
  73. let ri =element_gen("i",{class: "request-icon material-icons", text: "close"});
  74. let ab = element_gen("button",{class: "action request",value: d,id: "accept",title: "request",childs: {ai}});
  75. let rb = element_gen("button",{class: "action request",value: d,id: "reject",title: "request",childs: {ri}});
  76. var td = element_gen("td",{value: "action",childs:{ab,rb}});
  77. tr.append(td);
  78. $("tbody").append(tr);
  79. }
  80. $(".processing").hide();
  81. setTimeout(function(){
  82. set_listeners();
  83. $(".main-wrapper").show();
  84. },250);
  85. }
  86. /* fetch_requests() - fetch material requests */
  87. function fetch_requests(){
  88. $.post("/scripts/opaso",{query: 29},function(data){
  89. // console.log(data);
  90. /* extract response */
  91. let response = JSON.parse(data);
  92. let status = response["status"];
  93. /* handle by status */
  94. switch(status){
  95. case "success": /* on success, generate table */
  96. var date = new Date();
  97. date = date.toString().split(" ").splice(1,4).join(" ");
  98. $(".sub-header").text(`As of: ${date}`);
  99. requested = response["requested"];
  100. next = Object.keys(requested).length;
  101. $(".table-total").text(`Total: ${next}`);
  102. table_gen(requested);
  103. break;
  104. case "expired": /* on expired, redirect to index */
  105. window.location.href = "/?error=session_expired";
  106. break;
  107. default: /* on error, display message */
  108. set_alert(status,response["message"]);
  109. break;
  110. }
  111. });
  112. }