Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ScarAlarm {
- constructor(action, alarmId, $modal) {
- // Default Props
- this.requestType = 'POST';
- this.url = alarmData.ajax_url;
- this.backendCallback = 'editAlarm';
- // Dynamic Props
- this.action = action;
- this.alarmId = alarmId;
- this.$modal = $modal;
- // Alarm
- this.id_centralina = null;
- this.date_from = null;
- this.date_to = null;
- this.channel = null;
- this.delta_eur = null;
- this.alarm_duration = null;
- this.alarm_state = null;
- // Call router
- this.router(this.action);
- }
- router(action) {
- switch (action) {
- case 'EDIT_ALARM':
- this.getAlarmById(this.alarmId);
- break;
- }
- }
- getAlarmById() {
- let AlarmClass = this;
- jQuery.ajax({
- url: this.url,
- type: this.requestType,
- data: {
- alarmId: this.alarmId,
- action: this.backendCallback
- },
- success: function (response) {
- let dataSet = JSON.parse(response);
- AlarmClass.$modal.modal('show');
- this.setAlarmData(dataSet);
- this.populateAlarmForm(dataSet);
- },
- populateAlarmForm: function (dataSet) {
- let today = new Date().toJSON().slice(0, 10).replace(/-/g, '-');
- AlarmClass.$modal.find('#alarm-status').val(dataSet.alarm_status);
- AlarmClass.$modal.find('#date-to').val(today);
- let ajaxScope = this;
- AlarmClass.$modal.find('#submit-alarm-modification').on('click', (e) => {
- e.preventDefault();
- AlarmClass.backendCallback = 'editAlarm';
- AlarmClass.alarm_state = AlarmClass.$modal.find('#alarm-status').find(":selected").text();
- AlarmClass.date_to = AlarmClass.$modal.find('#date-to').val();
- AlarmClass.editAlarm();
- ajaxScope.setAlarmData(dataSet);
- });
- },
- setAlarmData: function (dataSet) {
- AlarmClass.alarm_state = dataSet.alarm_status;
- },
- fail: function (response) {
- console.log('error', response);
- }
- })
- }
- editAlarm() {
- jQuery.ajax({
- url: this.url,
- type: this.requestType,
- data: {
- alarmId: this.alarmId,
- date_to: this.date_to,
- alarm_state: this.alarm_state,
- action: this.backendCallback
- },
- success: function (response) {
- /*
- console.log(response);
- */
- let data = JSON.parse(response);
- this.redirectToAlarmList(data);
- },
- redirectToAlarmList: function (data) {
- // TODO Add message!
- setTimeout(
- function () {
- window.location.href = `${data.redirectTo}`;
- }, 1000);
- },
- fail: function (response) {
- console.log('error', response);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement