Web Interfaces of PROSUME

set_price.ctp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <script>
  2. (function($) {
  3. var overlay = $("#eprosume_overlay");
  4. var errors = $("#setPriceErrors");
  5. var outcome = $("#setPriceOutcome");
  6. var prices = <?= json_encode($prices); ?>;
  7. var prosumerSelect = $("#prosumerSelect");
  8. var buyPriceInput = $("#buyPriceInput");
  9. var sellPriceInput = $("#sellPriceInput");
  10. var submitButton = $("#submitButton");
  11. if(prosumerSelect) {
  12. prosumerSelect.change(function() {
  13. var prosumer = this.value;
  14. var buyPrice;
  15. var sellPrice;
  16. if(prosumer && prices[prosumer] != null) {
  17. buyPrice = prices[prosumer].buyPrice;
  18. sellPrice = prices[prosumer].sellPrice;
  19. } else {
  20. buyPrice = buyPriceInput.attr('data-value');
  21. sellPrice = sellPriceInput.attr('data-value');
  22. }
  23. buyPriceInput.val(buyPrice);
  24. sellPriceInput.val(sellPrice);
  25. });
  26. }
  27. if(submitButton) {
  28. submitButton.click(function(e) {
  29. outcome.html("");
  30. outcome.addClass("hidden");
  31. e.preventDefault();
  32. if(prosumerSelect.val() != 0) {
  33. errors.html("");
  34. errors.addClass("hidden");
  35. overlay.show();
  36. $.ajax({
  37. url : "/xhr/setprice",
  38. type: "POST",
  39. contentType: "application/json",
  40. headers: {
  41. "X-CSRF-Token": <?= json_encode($this->request->getParam('_csrfToken')) ?>
  42. },
  43. dataType : "json",
  44. data: JSON.stringify({
  45. prosumer: prosumerSelect.val(),
  46. buyPrice: buyPriceInput.val(),
  47. sellPrice: sellPriceInput.val(),
  48. }),
  49. complete:function(xhr) {
  50. overlay.hide();
  51. if(xhr.status == 200) {
  52. outcome.html("Prices have been successfully set. It may take some time to register this change. Redirecting...");
  53. $('#submitButton').addClass("hidden");
  54. outcome.removeClass("hidden");
  55. var meter = prosumerSelect.val();
  56. prices[meter].buyPrice = buyPriceInput.val();
  57. prices[meter].sellPrice = sellPriceInput.val();
  58. setTimeout(
  59. () => { window.location.reload(); },
  60. 5000
  61. );
  62. } else {
  63. outcome.html("Sorry, an error occured while processing the price change");
  64. }
  65. },
  66. error: function() { }
  67. });
  68. } else {
  69. errors.html("Select a Meter");
  70. errors.removeClass("hidden");
  71. }
  72. });
  73. }
  74. })(jQuery);
  75. </script>