better error handling and user interaction

This commit is contained in:
hippoz 2020-10-13 22:17:09 +03:00
parent d3c153dc26
commit 53952adcc4

View file

@ -54,7 +54,14 @@
},
methods: {
sendMessage: async function(webhook, message) {
if (!webhook.url || !webhook.name || !message) return;
if (!webhook.url || !webhook.name) {
alert('Invalid webhook does not have url or name property');
return;
}
if (!message) {
alert('Message is required');
return
}
const res = await fetch(webhook.url, {
method: 'POST',
@ -66,7 +73,10 @@
})
});
console.log(await res.text());
if (res.status !== 200) {
alert(`Request failed with status code ${res.status} and content "${await res.text()}" (POST ${webhook.url})`);
return;
}
},
createWebhook: function() {
const name = prompt('Webhook name');
@ -78,14 +88,19 @@
this.webhooks.push({ name, url });
this.saveToLocalstorage();
alert(`Webhook "${name}" successfully created`);
},
deleteWebhookByIndex: function(index) {
this.webhooks.splice(index, 1);
},
deleteCurrentWebhook: function() {
this.selectedWebhook = -1;
this.deleteWebhookByIndex(this.selectedWebhook);
this.saveToLocalstorage();
const yn = confirm(`Delete webhook ${this.selectedWebhookObject.name}?`);
if (yn) {
this.selectedWebhook = -1;
this.deleteWebhookByIndex(this.selectedWebhook);
this.saveToLocalstorage();
alert(`Webhook successfully deleted`);
}
},
saveToLocalstorage: function() {
localStorage.setItem('webhooks', JSON.stringify(this.webhooks));
@ -99,14 +114,14 @@
try {
const webhooksJSON = JSON.parse(savedWebhooks);
if (!webhooksJSON) {
console.error('Parsing localstorage data returned undefined, clearing localstorage...', e);
alert('Parsing localstorage data returned undefined, clearing localstorage...', e);
this.webhooks = [];
localStorage.clear();
return;
}
this.webhooks = webhooksJSON;
} catch(e) {
console.error('Error while parsing saved localstorage data, clearing localstorage...', e);
alert('Error while parsing saved localstorage data, clearing localstorage...', e);
this.webhooks = [];
localStorage.clear();
return;