diff --git a/app/assets/javascripts/admin/controllers/admin-web-hooks-show-events.js.es6 b/app/assets/javascripts/admin/controllers/admin-web-hooks-show-events.js.es6 index 9b4cc91f5..d91469d33 100644 --- a/app/assets/javascripts/admin/controllers/admin-web-hooks-show-events.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-web-hooks-show-events.js.es6 @@ -1,14 +1,65 @@ import { ajax } from 'discourse/lib/ajax'; import { popupAjaxError } from 'discourse/lib/ajax-error'; +import computed from 'ember-addons/ember-computed-decorators'; export default Ember.Controller.extend({ + pingDisabled: false, + incomingEventIds: [], + incomingCount: Ember.computed.alias("incomingEventIds.length"), + + @computed('incomingCount') + hasIncoming(incomingCount) { + return incomingCount > 0; + }, + + subscribe() { + this.messageBus.subscribe(`/web_hook_events/${this.get('model.extras.web_hook_id')}`, data => { + if (data.event_type === 'ping') { + this.set('pingDisabled', false); + } + this._addIncoming(data.web_hook_event_id); + }); + }, + + unsubscribe() { + this.messageBus.unsubscribe('/web_hook_events/*'); + }, + + _addIncoming(eventId) { + const incomingEventIds = this.get("incomingEventIds"); + + if (incomingEventIds.indexOf(eventId) === -1) { + incomingEventIds.pushObject(eventId); + } + }, + actions: { loadMore() { this.get('model').loadMore(); }, ping() { - ajax(`/admin/web_hooks/${this.get('model.extras.web_hook_id')}/ping`, {type: 'POST'}).catch(popupAjaxError); + this.set('pingDisabled', true); + + ajax(`/admin/web_hooks/${this.get('model.extras.web_hook_id')}/ping`, { + type: 'POST' + }).catch(error => { + this.set('pingDisabled', false); + popupAjaxError(error); + }); + }, + + showInserted() { + const webHookId = this.get('model.extras.web_hook_id'); + + ajax(`/admin/web_hooks/${webHookId}/events/bulk`, { + type: 'GET', + data: { ids: this.get('incomingEventIds') } + }).then(data => { + const objects = data.map(event => this.store.createRecord('web-hook-event', event)); + this.get("model").unshiftObjects(objects); + this.set("incomingEventIds", []); + }); } } }); diff --git a/app/assets/javascripts/admin/routes/admin-web-hooks-show-events.js.es6 b/app/assets/javascripts/admin/routes/admin-web-hooks-show-events.js.es6 index ea4e33ac6..89841d5a7 100644 --- a/app/assets/javascripts/admin/routes/admin-web-hooks-show-events.js.es6 +++ b/app/assets/javascripts/admin/routes/admin-web-hooks-show-events.js.es6 @@ -5,6 +5,11 @@ export default Discourse.Route.extend({ setupController(controller, model) { controller.set('model', model); + controller.subscribe(); + }, + + deactivate() { + this.controllerFor('adminWebHooks.showEvents').unsubscribe(); }, renderTemplate() { diff --git a/app/assets/javascripts/admin/templates/web-hooks-show-events.hbs b/app/assets/javascripts/admin/templates/web-hooks-show-events.hbs index 7cfb1bd79..9b4f1e80a 100644 --- a/app/assets/javascripts/admin/templates/web-hooks-show-events.hbs +++ b/app/assets/javascripts/admin/templates/web-hooks-show-events.hbs @@ -2,7 +2,7 @@ {{#link-to 'adminWebHooks' tagName='button' classNames='btn'}} {{fa-icon 'list'}} {{i18n 'admin.web_hooks.events.go_list'}} {{/link-to}} - {{d-button icon="send" label="admin.web_hooks.events.ping" action="ping"}} + {{d-button icon="send" label="admin.web_hooks.events.ping" action="ping" disabled=pingDisabled}} {{#link-to 'adminWebHooks.show' model.extras.web_hook_id tagName='button' classNames='btn'}} {{fa-icon 'edit'}} {{i18n 'admin.web_hooks.events.go_details'}} {{/link-to}} @@ -10,6 +10,12 @@
{{#if model}} + {{#if hasIncoming}} +
+ {{count-i18n key="admin.web_hooks.events.incoming" count=incomingCount}} + {{i18n 'click_to_show'}} +
+ {{/if}} {{#load-more selector=".web-hook-events li" action="loadMore"}}