diff --git a/app/controllers/events/view.js b/app/controllers/events/view.js index fb848050a06..3f7d57104b1 100644 --- a/app/controllers/events/view.js +++ b/app/controllers/events/view.js @@ -1,28 +1,24 @@ import Controller from '@ember/controller'; +import { inject as service } from '@ember/service'; import { action } from '@ember/object'; -export default class extends Controller { +export default class EventsViewController extends Controller { + @service eventCopier; + isCopying = false; + @action - copyEvent() { - this.set('isCopying', true); - this.loader - .post(`events/${this.model.id}/copy`, {}) - .then(copiedEvent => { - this.transitionToRoute('events.view.edit', copiedEvent.identifier); - this.notify.success(this.l10n.t('Event copied successfully'), - { - id: 'event_copy_succ' - }); - }) - .catch(e => { - console.error('Error while copying event', e); - this.notify.error(this.l10n.t('Copying of event failed'), - { - id: 'event_copy_fail' - }); - }) - .finally(() => { - this.set('isCopying', false); - }); + async copyEvent() { + this.isCopying = true; + + try { + const copiedEvent = await this.eventCopier.copy(this.model.id); + this.transitionToRoute('events.view.edit', copiedEvent.identifier); + this.eventCopier.success(); + } catch (e) { + console.error('Error copying event', e); + this.eventCopier.error(); + } finally { + this.isCopying = false; + } } } diff --git a/app/services/event-copier.js b/app/services/event-copier.js new file mode 100644 index 00000000000..d91d791807a --- /dev/null +++ b/app/services/event-copier.js @@ -0,0 +1,20 @@ +import Service from '@ember/service'; +import { inject as service } from '@ember/service'; + +export default class EventCopierService extends Service { + @service loader; + @service notify; + @service l10n; + + async copy(eventId) { + return this.loader.post(`events/${eventId}/copy`, {}); + } + + success() { + this.notify.success(this.l10n.t('Event copied successfully'), { id: 'event_copy_succ' }); + } + + error() { + this.notify.error(this.l10n.t('Copying of event failed'), { id: 'event_copy_fail' }); + } +}