Advertisement
Guest User

scar alarm

a guest
Mar 25th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.31 KB | None | 0 0
  1. class ScarAlarm {
  2.     constructor(action, alarmId, $modal) {
  3.         // Default Props
  4.         this.requestType = 'POST';
  5.         this.url = alarmData.ajax_url;
  6.         this.backendCallback = 'editAlarm';
  7.         // Dynamic Props
  8.         this.action = action;
  9.         this.alarmId = alarmId;
  10.         this.$modal = $modal;
  11.         // Alarm
  12.         this.id_centralina = null;
  13.         this.date_from = null;
  14.         this.date_to = null;
  15.         this.channel = null;
  16.         this.delta_eur = null;
  17.         this.alarm_duration = null;
  18.         this.alarm_state = null;
  19.         // Call router
  20.         this.router(this.action);
  21.     }
  22.  
  23.     router(action) {
  24.         switch (action) {
  25.             case 'EDIT_ALARM':
  26.                 this.getAlarmById(this.alarmId);
  27.                 break;
  28.  
  29.         }
  30.     }
  31.  
  32.     getAlarmById() {
  33.         let AlarmClass = this;
  34.         jQuery.ajax({
  35.             url: this.url,
  36.             type: this.requestType,
  37.             data: {
  38.                 alarmId: this.alarmId,
  39.                 action: this.backendCallback
  40.             },
  41.             success: function (response) {
  42.                 let dataSet = JSON.parse(response);
  43.                 AlarmClass.$modal.modal('show');
  44.                 this.setAlarmData(dataSet);
  45.                 this.populateAlarmForm(dataSet);
  46.             },
  47.             populateAlarmForm: function (dataSet) {
  48.                 let today = new Date().toJSON().slice(0, 10).replace(/-/g, '-');
  49.                 AlarmClass.$modal.find('#alarm-status').val(dataSet.alarm_status);
  50.                 AlarmClass.$modal.find('#date-to').val(today);
  51.  
  52.                 let ajaxScope = this;
  53.                 AlarmClass.$modal.find('#submit-alarm-modification').on('click', (e) => {
  54.                     e.preventDefault();
  55.                     AlarmClass.backendCallback = 'editAlarm';
  56.                     AlarmClass.alarm_state = AlarmClass.$modal.find('#alarm-status').find(":selected").text();
  57.                     AlarmClass.date_to = AlarmClass.$modal.find('#date-to').val();
  58.                     AlarmClass.editAlarm();
  59.                     ajaxScope.setAlarmData(dataSet);
  60.                 });
  61.             },
  62.             setAlarmData: function (dataSet) {
  63.  
  64.                 AlarmClass.alarm_state = dataSet.alarm_status;
  65.  
  66.             },
  67.             fail: function (response) {
  68.                 console.log('error', response);
  69.             }
  70.         })
  71.     }
  72.  
  73.     editAlarm() {
  74.         jQuery.ajax({
  75.             url: this.url,
  76.             type: this.requestType,
  77.             data: {
  78.                 alarmId: this.alarmId,
  79.                 date_to: this.date_to,
  80.                 alarm_state: this.alarm_state,
  81.                 action: this.backendCallback
  82.             },
  83.             success: function (response) {
  84.  
  85. /*
  86.                 console.log(response);
  87. */
  88.  
  89.                  let data = JSON.parse(response);
  90.                  this.redirectToAlarmList(data);
  91.             },
  92.             redirectToAlarmList: function (data) {
  93.                 // TODO Add message!
  94.                 setTimeout(
  95.                     function () {
  96.                         window.location.href = `${data.redirectTo}`;
  97.                     }, 1000);
  98.             },
  99.             fail: function (response) {
  100.                 console.log('error', response);
  101.             }
  102.         });
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement