2015-11-27 03:24:37 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import { alt } from '../alt';
|
|
|
|
import WebhookActions from '../actions/webhook_actions';
|
|
|
|
|
2015-11-27 13:36:42 +01:00
|
|
|
import WebhookSource from '../sources/webhook_source';
|
2015-11-27 03:24:37 +01:00
|
|
|
|
|
|
|
class WebhookStore {
|
|
|
|
constructor() {
|
2015-11-27 13:36:42 +01:00
|
|
|
this.webhooks = [];
|
|
|
|
this.webhookEvents = [];
|
|
|
|
this.webhookMeta = {
|
|
|
|
invalidateCache: false,
|
|
|
|
err: null,
|
|
|
|
idToDelete: null
|
|
|
|
};
|
|
|
|
this.webhookEventsMeta = {
|
|
|
|
invalidateCache: false,
|
|
|
|
err: null
|
|
|
|
};
|
|
|
|
|
2015-11-27 03:24:37 +01:00
|
|
|
this.bindActions(WebhookActions);
|
2015-11-27 13:36:42 +01:00
|
|
|
this.registerAsync(WebhookSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
onFetchWebhooks(invalidateCache) {
|
|
|
|
this.webhookMeta.invalidateCache = invalidateCache;
|
|
|
|
this.getInstance().lookupWebhooks();
|
2015-11-27 03:24:37 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 13:36:42 +01:00
|
|
|
onSuccessFetchWebhooks({ webhooks }) {
|
|
|
|
this.webhookMeta.invalidateCache = false;
|
|
|
|
this.webhookMeta.err = null;
|
2015-11-27 03:24:37 +01:00
|
|
|
this.webhooks = webhooks;
|
2015-11-27 13:36:42 +01:00
|
|
|
|
|
|
|
this.webhookEventsMeta.invalidateCache = true;
|
|
|
|
this.getInstance().lookupWebhookEvents();
|
2015-11-27 03:24:37 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 13:36:42 +01:00
|
|
|
onFetchWebhookEvents(invalidateCache) {
|
|
|
|
this.webhookEventsMeta.invalidateCache = invalidateCache;
|
|
|
|
this.getInstance().lookupWebhookEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
onSuccessFetchWebhookEvents({ events }) {
|
|
|
|
this.webhookEventsMeta.invalidateCache = false;
|
|
|
|
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(id) {
|
2015-11-27 13:36:42 +01:00
|
|
|
this.webhookMeta.invalidateCache = true;
|
|
|
|
this.webhookMeta.idToDelete = id;
|
|
|
|
|
|
|
|
if(!this.getInstance().isLoading()) {
|
|
|
|
this.getInstance().performRemoveWebhook();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onSuccessRemoveWebhook() {
|
|
|
|
this.webhookMeta.idToDelete = null;
|
|
|
|
if(!this.getInstance().isLoading()) {
|
|
|
|
this.getInstance().lookupWebhooks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|