2019-06-22 19:28:03 +00:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// AJ Query
|
|
|
|
var $ = window.$;
|
|
|
|
var $$ = window.$$;
|
|
|
|
|
2019-06-22 23:11:14 +00:00
|
|
|
var state = { account: { schedules: [] } };
|
2019-06-22 19:28:03 +00:00
|
|
|
|
2019-06-24 01:02:31 +00:00
|
|
|
var $schedTpl;
|
2019-06-22 19:28:03 +00:00
|
|
|
var $headerTpl;
|
|
|
|
var $webhookTpl;
|
|
|
|
var $webhookHeaderTpl;
|
|
|
|
|
2019-06-22 23:11:14 +00:00
|
|
|
function pad(i) {
|
|
|
|
i = String(i);
|
|
|
|
while (i.length < 2) {
|
|
|
|
i = '0' + i;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2019-06-22 19:28:03 +00:00
|
|
|
|
|
|
|
function run() {
|
|
|
|
$headerTpl = $('.js-new-webhook .js-header').outerHTML;
|
|
|
|
|
|
|
|
$webhookHeaderTpl = $('.js-schedule .js-webhook .js-header').outerHTML;
|
|
|
|
$('.js-schedule .js-webhooks .js-headers').innerHTML = '';
|
|
|
|
$webhookTpl = $('.js-schedule .js-webhook').outerHTML;
|
|
|
|
$('.js-schedule .js-webhooks').innerHTML = '';
|
|
|
|
|
|
|
|
// after blanking all inner templates
|
2019-06-24 01:02:31 +00:00
|
|
|
$schedTpl = $('.js-schedule').outerHTML;
|
2019-06-22 19:28:03 +00:00
|
|
|
|
2019-06-23 06:44:50 +00:00
|
|
|
var $form = $('.js-new-schedule');
|
2019-06-22 23:11:14 +00:00
|
|
|
// Pick a date and time on an even number
|
|
|
|
// between 10 and 15 minutes in the future
|
|
|
|
var d = new Date(Date.now() + 10 * 60 * 1000);
|
|
|
|
var minutes = d.getMinutes() + (5 - (d.getMinutes() % 5)) - d.getMinutes();
|
|
|
|
d = new Date(d.valueOf() + minutes * 60 * 1000);
|
2019-06-23 06:44:50 +00:00
|
|
|
$('.js-date', $form).value = d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate());
|
|
|
|
$('.js-time', $form).value = pad(d.getHours()) + ':' + pad(d.getMinutes());
|
|
|
|
$('.js-url', $form).value = 'https://enfqtbjh5ghw.x.pipedream.net';
|
2019-06-22 23:11:14 +00:00
|
|
|
|
2019-06-22 19:28:03 +00:00
|
|
|
console.log('hello');
|
|
|
|
|
|
|
|
$('body').addEventListener('click', function(ev) {
|
|
|
|
if (ev.target.matches('.js-new-header')) {
|
|
|
|
newWebhookHeader(ev.target);
|
|
|
|
} else if (ev.target.matches('.js-rm-header')) {
|
|
|
|
rmWebhookHeader(ev.target);
|
2019-06-23 09:01:24 +00:00
|
|
|
} else if (ev.target.matches('.js-delete') && ev.target.closest('.js-schedule')) {
|
|
|
|
deleteSchedule(ev.target.closest('.js-schedule'));
|
2019-06-22 19:28:03 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
});
|
|
|
|
|
2019-06-22 23:11:14 +00:00
|
|
|
$('body').addEventListener('change', function(ev) {
|
|
|
|
var $hook = ev.target.closest('.js-new-webhook');
|
|
|
|
if (ev.target.matches('.js-url') && $hook) {
|
|
|
|
if (!$('.js-comment', $hook).value) {
|
|
|
|
$('.js-comment', $hook).value = ev.target.value.replace(/https:\/\//, '').replace(/\/.*/, '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-22 19:28:03 +00:00
|
|
|
$('body').addEventListener('submit', function(ev) {
|
|
|
|
if (ev.target.matches('.js-new-schedule')) {
|
2019-06-24 01:02:31 +00:00
|
|
|
console.log('new schedule');
|
2019-06-22 19:28:03 +00:00
|
|
|
newSchedule(ev.target);
|
2019-06-22 23:11:14 +00:00
|
|
|
} else if (ev.target.matches('.js-schedules-list')) {
|
|
|
|
doLogin();
|
2019-06-22 19:28:03 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-22 23:11:14 +00:00
|
|
|
|
2019-06-22 19:28:03 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function newSchedule() {
|
2019-06-23 06:44:50 +00:00
|
|
|
var $hook = $('.js-new-schedule');
|
|
|
|
//var deviceId = $hook.closest('.js-new-schedule').querySelector('.js-id').value;
|
2019-06-23 03:50:17 +00:00
|
|
|
var schedule = {
|
2019-06-23 06:08:05 +00:00
|
|
|
date: $('.js-date', $hook).value,
|
|
|
|
time: $('.js-time', $hook).value,
|
|
|
|
tz: $('.js-tz', $hook).value,
|
2019-06-23 03:50:17 +00:00
|
|
|
webhooks: []
|
|
|
|
};
|
2019-06-22 19:28:03 +00:00
|
|
|
var hook = {
|
|
|
|
comment: $('.js-comment', $hook).value,
|
|
|
|
method: $('.js-method', $hook).value,
|
|
|
|
url: $('.js-url', $hook).value,
|
|
|
|
headers: {}
|
|
|
|
};
|
2019-06-23 03:50:17 +00:00
|
|
|
schedule.webhooks.push(hook);
|
|
|
|
console.log('schedule:', schedule);
|
2019-06-22 19:28:03 +00:00
|
|
|
$$('.js-header', $hook).forEach(function($head) {
|
|
|
|
var key = $('.js-key', $head).value;
|
|
|
|
var val = $('.js-value', $head).value;
|
|
|
|
if (key && val) {
|
|
|
|
hook.headers[key] = val;
|
|
|
|
}
|
|
|
|
});
|
2019-06-24 01:02:31 +00:00
|
|
|
|
|
|
|
var auth = {
|
|
|
|
user: $('.js-http-user', $hook).value || '',
|
|
|
|
pass: $('.js-http-pass', $hook).value || ''
|
|
|
|
};
|
|
|
|
if (auth.user || auth.pass) {
|
|
|
|
hook.auth = auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
var body = $('.js-body-template', $hook).value;
|
|
|
|
if ('json' === $('.js-body-type:checked', $hook).value) {
|
|
|
|
hook.json = (body && JSON.parse(body)) || undefined;
|
|
|
|
} else {
|
|
|
|
// TODO try query parse
|
|
|
|
hook.form = (body && JSON.parse(body)) || undefined;
|
|
|
|
// TODO raw string as well
|
|
|
|
}
|
2019-06-22 19:28:03 +00:00
|
|
|
// TODO update on template change and show preview
|
|
|
|
|
|
|
|
var opts = {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
2019-06-22 23:11:14 +00:00
|
|
|
Authorization: getToken(),
|
2019-06-22 19:28:03 +00:00
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
2019-06-23 03:50:17 +00:00
|
|
|
body: JSON.stringify(schedule),
|
2019-06-22 19:28:03 +00:00
|
|
|
cors: true
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2019-06-22 23:11:14 +00:00
|
|
|
state.account.devices
|
|
|
|
.filter(function(d) {
|
|
|
|
return d.accessToken == deviceId;
|
|
|
|
})[0]
|
|
|
|
.webhooks.push(hook);
|
2019-06-22 19:28:03 +00:00
|
|
|
|
2019-06-22 23:11:14 +00:00
|
|
|
displayAccount(state.account);
|
|
|
|
return;
|
2019-06-22 19:28:03 +00:00
|
|
|
*/
|
|
|
|
|
2019-06-22 23:11:14 +00:00
|
|
|
window.fetch('/api/v0/schedules', opts).then(function(resp) {
|
|
|
|
return resp
|
|
|
|
.json()
|
|
|
|
.then(function(data) {
|
2019-06-23 06:08:05 +00:00
|
|
|
if (!data.date || !data.webhooks) {
|
|
|
|
console.error(data);
|
2019-06-22 23:11:14 +00:00
|
|
|
throw new Error('something bad happened');
|
|
|
|
}
|
2019-06-22 19:28:03 +00:00
|
|
|
|
2019-06-23 09:01:24 +00:00
|
|
|
state.account.schedules.push(data);
|
2019-06-22 19:28:03 +00:00
|
|
|
|
2019-06-22 23:11:14 +00:00
|
|
|
displayAccount(state.account);
|
|
|
|
})
|
|
|
|
.catch(function(e) {
|
2019-06-23 09:01:24 +00:00
|
|
|
console.error(e);
|
2019-06-22 23:11:14 +00:00
|
|
|
window.alert(e.message);
|
|
|
|
});
|
|
|
|
});
|
2019-06-22 19:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function newWebhookHeader($newHeader) {
|
|
|
|
var $hs = $newHeader.closest('.js-headers');
|
|
|
|
var $h = $newHeader.closest('.js-header');
|
|
|
|
var $div = document.createElement('div');
|
|
|
|
$div.innerHTML = $headerTpl;
|
|
|
|
$hs.append($('.js-header', $div));
|
|
|
|
$newHeader.hidden = true;
|
|
|
|
$('.js-rm-header', $h).hidden = false;
|
|
|
|
$('.js-key', $h).required = 'required';
|
|
|
|
$('.js-value', $h).required = 'required';
|
|
|
|
}
|
|
|
|
|
|
|
|
function rmWebhookHeader($rmHeader) {
|
|
|
|
var $h = $rmHeader.closest('.js-header');
|
|
|
|
$h.parentElement.removeChild($h);
|
|
|
|
}
|
2019-06-23 09:01:24 +00:00
|
|
|
function deleteSchedule($sched) {
|
|
|
|
var schedId = $('.js-id', $sched).value;
|
2019-06-22 19:28:03 +00:00
|
|
|
var opts = {
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
2019-06-22 23:11:14 +00:00
|
|
|
Authorization: getToken()
|
2019-06-22 19:28:03 +00:00
|
|
|
},
|
|
|
|
cors: true
|
|
|
|
};
|
2019-06-23 09:01:24 +00:00
|
|
|
window.fetch('/api/v0/schedules/' + schedId, opts).then(function(resp) {
|
2019-06-22 23:11:14 +00:00
|
|
|
return resp.json().then(function(result) {
|
2019-06-23 09:01:24 +00:00
|
|
|
if (!result.webhooks) {
|
2019-06-22 23:11:14 +00:00
|
|
|
console.error(result);
|
|
|
|
window.alert('something went wrong: ' + JSON.stringify(result));
|
|
|
|
return;
|
|
|
|
}
|
2019-06-23 09:01:24 +00:00
|
|
|
state.account.schedules = state.account.schedules.filter(function(g) {
|
|
|
|
return g.id !== result.id;
|
2019-06-22 19:28:03 +00:00
|
|
|
});
|
2019-06-23 09:01:24 +00:00
|
|
|
displayAccount(state.account);
|
2019-06-22 19:28:03 +00:00
|
|
|
});
|
2019-06-22 23:11:14 +00:00
|
|
|
});
|
2019-06-22 19:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function displayAccount(data) {
|
|
|
|
state.account = data;
|
|
|
|
console.log('[debug] Display Account:');
|
|
|
|
console.log(data);
|
|
|
|
var $devs = $('.js-schedules');
|
|
|
|
$devs.innerHTML = '';
|
2019-06-23 06:44:50 +00:00
|
|
|
data.schedules.forEach(function(d) {
|
2019-06-23 09:01:24 +00:00
|
|
|
console.log('schedule', d);
|
2019-06-24 01:02:31 +00:00
|
|
|
var $dev = $.create($schedTpl);
|
2019-06-23 06:44:50 +00:00
|
|
|
$('.js-id', $dev).value = d.id;
|
|
|
|
$('.js-date', $dev).value = d.date;
|
|
|
|
$('.js-time', $dev).value = d.time;
|
|
|
|
$('.js-tz', $dev).value = d.tz;
|
2019-06-22 19:28:03 +00:00
|
|
|
d.webhooks.forEach(function(h) {
|
2019-06-23 06:44:50 +00:00
|
|
|
console.log('webhook', h);
|
2019-06-22 19:28:03 +00:00
|
|
|
var $hook = $.create($webhookTpl);
|
|
|
|
$('.js-id', $hook).innerText = h.id;
|
|
|
|
$('.js-comment', $hook).innerText = h.comment;
|
|
|
|
$('.js-method', $hook).innerText = h.method;
|
|
|
|
$('.js-url', $hook).innerText = h.url;
|
2019-06-23 06:44:50 +00:00
|
|
|
Object.keys(h.headers || {}).forEach(function(k) {
|
2019-06-22 19:28:03 +00:00
|
|
|
var $header = $.create($webhookHeaderTpl);
|
|
|
|
var v = h.headers[k];
|
|
|
|
$('.js-key', $header).innerText = k;
|
|
|
|
$('.js-value', $header).innerText = v;
|
|
|
|
$('.js-headers', $hook).innerHTML += $header.innerHTML;
|
|
|
|
});
|
2019-06-23 06:44:50 +00:00
|
|
|
$('.js-body-template', $hook).innerText = h.body || '';
|
2019-06-22 19:28:03 +00:00
|
|
|
$('.js-webhooks', $dev).innerHTML += $hook.innerHTML;
|
|
|
|
});
|
2019-06-23 06:44:50 +00:00
|
|
|
$devs.appendChild($dev);
|
2019-06-22 19:28:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
console.info('[tzdb] requesting');
|
|
|
|
window.fetch('./tzdb.json').then(function(resp) {
|
|
|
|
return resp.json().then(function(tzdb) {
|
|
|
|
console.info('[tzdb] received');
|
|
|
|
var tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
2019-06-22 23:11:14 +00:00
|
|
|
var options = $$('.js-tz option');
|
2019-06-22 19:28:03 +00:00
|
|
|
var valOpt = options[0].outerHTML; // UTC
|
2019-06-23 06:44:50 +00:00
|
|
|
//var spaceOpt = options[1].outerHTML; // ----
|
|
|
|
var innerHTML = $('.js-new-schedule .js-tz').innerHTML;
|
2019-06-22 23:11:14 +00:00
|
|
|
/*
|
2019-06-22 19:28:03 +00:00
|
|
|
innerHTML =
|
|
|
|
'<option selected value="' +
|
|
|
|
tz +
|
|
|
|
'"> ' +
|
|
|
|
tz +
|
|
|
|
'</option>' +
|
|
|
|
spaceOpt +
|
|
|
|
innerHTML.replace(/>UTC/, '> UTC');
|
2019-06-22 23:11:14 +00:00
|
|
|
*/
|
|
|
|
//$('.js-tz').innerHTML += spaceOpt;
|
|
|
|
//$('.js-tz').innerHTML += valOpt.replace(/UTC/g, 'custom');
|
2019-06-22 19:28:03 +00:00
|
|
|
Object.keys(tzdb)
|
|
|
|
.sort()
|
|
|
|
.forEach(function(k) {
|
|
|
|
var parts = k.split(' ');
|
|
|
|
//var sep = '── ' + parts[0];
|
|
|
|
var sep = parts[0];
|
|
|
|
if (parts[0] !== parts[1]) {
|
|
|
|
sep += ' / ' + parts[1] + ' (DST)';
|
|
|
|
}
|
|
|
|
//innerHTML += '<option disabled>' + sep + '</option>';
|
|
|
|
innerHTML += '<optgroup label="' + sep + '">';
|
|
|
|
var areas = tzdb[k];
|
|
|
|
areas.forEach(function(_tz) {
|
|
|
|
if (tz !== _tz) {
|
|
|
|
innerHTML += valOpt.replace(/UTC/g, _tz);
|
2019-06-22 23:11:14 +00:00
|
|
|
} else {
|
|
|
|
innerHTML += '<option selected value="' + tz + '">' + tz + '</option>';
|
2019-06-22 19:28:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
innerHTML += '</optgroup>';
|
|
|
|
});
|
2019-06-23 06:44:50 +00:00
|
|
|
$('.js-new-schedule .js-tz').innerHTML = innerHTML;
|
2019-06-22 19:28:03 +00:00
|
|
|
|
|
|
|
console.info('[tzdb] loaded');
|
|
|
|
run();
|
|
|
|
});
|
|
|
|
});
|
2019-06-22 23:11:14 +00:00
|
|
|
|
|
|
|
var allSchedules = [];
|
|
|
|
|
|
|
|
function getToken() {
|
|
|
|
return JSON.parse(localStorage.getItem('session')).access_token;
|
|
|
|
}
|
|
|
|
|
|
|
|
function doLogin() {
|
|
|
|
localStorage.setItem(
|
|
|
|
'session',
|
|
|
|
JSON.stringify({
|
|
|
|
access_token: $('.js-auth-token').value
|
|
|
|
})
|
|
|
|
);
|
|
|
|
$('.js-schedules-list').hidden = true;
|
|
|
|
return window
|
|
|
|
.fetch('/api/v0/schedules', {
|
|
|
|
headers: { Authorization: getToken() }
|
|
|
|
})
|
|
|
|
.then(function(resp) {
|
2019-06-23 06:08:05 +00:00
|
|
|
return resp
|
|
|
|
.clone()
|
|
|
|
.json()
|
|
|
|
.then(function(schedules) {
|
2019-06-23 06:44:50 +00:00
|
|
|
console.log('schedules');
|
|
|
|
console.log(schedules);
|
2019-06-23 06:08:05 +00:00
|
|
|
allSchedules = schedules;
|
|
|
|
renderSchedules(schedules);
|
2019-06-23 06:44:50 +00:00
|
|
|
state.account.schedules = schedules;
|
|
|
|
displayAccount(state.account);
|
|
|
|
$('.js-account').hidden = false;
|
2019-06-23 06:08:05 +00:00
|
|
|
})
|
|
|
|
.catch(function(e) {
|
|
|
|
console.error("Didn't parse JSON:");
|
|
|
|
console.error(e);
|
|
|
|
console.log(resp);
|
|
|
|
$('.js-schedules-list').hidden = false;
|
|
|
|
window.alert(resp.status + ': ' + resp.statusText);
|
|
|
|
return resp.text().then(function(text) {
|
|
|
|
window.alert(text);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(function(e) {
|
|
|
|
console.error('Request Error');
|
|
|
|
console.error(e);
|
|
|
|
window.alert('Network error. Are you online?');
|
2019-06-22 23:11:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderSchedules(schedules) {
|
|
|
|
document.querySelector('.js-schedules-output').innerText = JSON.stringify(schedules, null, 2);
|
|
|
|
}
|
|
|
|
|
2019-06-23 06:44:50 +00:00
|
|
|
console.log('whatever');
|
|
|
|
$('.js-auth-token').value = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
|
2019-06-22 19:28:03 +00:00
|
|
|
//window.addEventListener('load', run);
|
|
|
|
})();
|