2015-11-27 03:24:37 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import { alt } from '../alt';
|
2016-01-15 13:55:58 +01:00
|
|
|
|
2015-11-27 03:24:37 +01:00
|
|
|
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 = {
|
2016-01-15 13:55:58 +01:00
|
|
|
err: null
|
2015-11-27 13:36:42 +01:00
|
|
|
};
|
|
|
|
this.webhookEventsMeta = {
|
|
|
|
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) {
|
2016-01-15 13:55:58 +01:00
|
|
|
if (invalidateCache || !this.getInstance().isLoading()) {
|
|
|
|
this.getInstance().lookupWebhooks(invalidateCache);
|
|
|
|
}
|
2016-01-15 13:56:56 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2015-12-10 15:41:55 +01:00
|
|
|
onSuccessFetchWebhooks({ webhooks = [] }) {
|
2015-11-27 13:36:42 +01:00
|
|
|
this.webhookMeta.err = null;
|
2015-11-27 03:24:37 +01:00
|
|
|
this.webhooks = webhooks;
|
2015-11-27 13:36:42 +01:00
|
|
|
|
2016-01-15 13:55:58 +01:00
|
|
|
this.onFetchWebhookEvents(true);
|
2015-11-27 03:24:37 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 13:36:42 +01:00
|
|
|
onFetchWebhookEvents(invalidateCache) {
|
2016-01-15 13:55:58 +01:00
|
|
|
if (invalidateCache || !this.getInstance().isLoading()) {
|
|
|
|
this.getInstance().lookupWebhookEvents(invalidateCache);
|
|
|
|
}
|
2016-01-15 13:56:56 +01:00
|
|
|
|
|
|
|
// Prevent alt from sending an empty change event when a request is sent
|
|
|
|
// off to the source
|
|
|
|
this.preventDefault();
|
2015-11-27 13:36:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onSuccessFetchWebhookEvents({ events }) {
|
|
|
|
this.webhookEventsMeta.err = null;
|
|
|
|
|
|
|
|
// remove all events that have already been used.
|
|
|
|
const usedEvents = this.webhooks
|
2016-01-15 13:55:58 +01:00
|
|
|
.reduce((tempUsedEvents, webhook) => {
|
|
|
|
tempUsedEvents.push(webhook.event.split('.')[0]);
|
|
|
|
return tempUsedEvents;
|
|
|
|
}, []);
|
2015-11-27 13:36:42 +01:00
|
|
|
|
|
|
|
this.webhookEvents = events.filter((event) => {
|
|
|
|
return usedEvents.indexOf(event) === -1;
|
|
|
|
});
|
2015-11-27 03:24:37 +01:00
|
|
|
}
|
|
|
|
|
2016-01-15 13:55:58 +01:00
|
|
|
onRemoveWebhook(webhookId) {
|
|
|
|
this.getInstance().performRemoveWebhook(webhookId);
|
2015-11-27 13:36:42 +01:00
|
|
|
|
2016-01-15 13:56:56 +01:00
|
|
|
// Prevent alt from sending an empty change event when a request is sent
|
|
|
|
// off to the source
|
|
|
|
this.preventDefault();
|
2015-11-27 13:36:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onSuccessRemoveWebhook() {
|
2016-01-15 13:55:58 +01:00
|
|
|
this.getInstance().lookupWebhooks(true);
|
2015-11-27 13:36:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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');
|