1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01:00
onion/js/stores/webhook_store.js

90 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-11-27 03:24:37 +01:00
'use strict';
import { alt } from '../alt';
2015-11-27 03:24:37 +01:00
import WebhookActions from '../actions/webhook_actions';
import WebhookSource from '../sources/webhook_source';
2015-11-27 03:24:37 +01:00
class WebhookStore {
constructor() {
this.webhooks = [];
this.webhookEvents = [];
this.webhookMeta = {
err: null
};
this.webhookEventsMeta = {
err: null
};
2015-11-27 03:24:37 +01:00
this.bindActions(WebhookActions);
this.registerAsync(WebhookSource);
}
onFetchWebhooks(invalidateCache) {
if (invalidateCache || !this.getInstance().isLoading()) {
this.getInstance().lookupWebhooks(invalidateCache);
}
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
2015-11-27 03:24:37 +01:00
}
onSuccessFetchWebhooks({ webhooks = [] }) {
this.webhookMeta.err = null;
2015-11-27 03:24:37 +01:00
this.webhooks = webhooks;
this.onFetchWebhookEvents(true);
2015-11-27 03:24:37 +01:00
}
onFetchWebhookEvents(invalidateCache) {
if (invalidateCache || !this.getInstance().isLoading()) {
this.getInstance().lookupWebhookEvents(invalidateCache);
}
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
}
onSuccessFetchWebhookEvents({ events }) {
this.webhookEventsMeta.err = null;
// remove all events that have already been used.
const usedEvents = this.webhooks
.reduce((tempUsedEvents, webhook) => {
tempUsedEvents.push(webhook.event.split('.')[0]);
return tempUsedEvents;
}, []);
this.webhookEvents = events.filter((event) => {
return usedEvents.indexOf(event) === -1;
});
2015-11-27 03:24:37 +01:00
}
onRemoveWebhook(webhookId) {
this.getInstance().performRemoveWebhook(webhookId);
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
}
onSuccessRemoveWebhook() {
this.getInstance().lookupWebhooks(true);
}
onErrorWebhooks(err) {
console.logGlobal(err);
this.webhookMeta.err = err;
}
onErrorWebhookEvents(err) {
console.logGlobal(err);
this.webhookEventsMeta.err = err;
2015-11-27 03:24:37 +01:00
}
}
export default alt.createStore(WebhookStore, 'WebhookStore');