Ghost/ghost/admin/app/controllers/subscribers.js
Kevin Ansfield 352c4af1d7 Refactored usage of .get('property') with es5 getters
no issue
- ran [es5-getter-ember-codemod](https://github.com/rondale-sc/es5-getter-ember-codemod)
- [es5 getters RFC](https://github.com/emberjs/rfcs/blob/master/text/0281-es5-getters.md)
- updates the majority of `object.get('property')` with `object.property` with exceptions:
  - `.get('nested.property')` - it's not possible to determine if this is relying on "safe" path chaining for when `nested` doesn't exist
  - `.get('config.x')` and `.get('settings.x')` - both our `config` and `settings` services are proxy objects which do not support es5 getters
- this PR is not exhaustive, there are still a number of places where `.get('service.foo')` and similar could be replaced but it gets us a long way there in a quick and automated fashion
2019-03-06 13:54:14 +00:00

168 lines
5.1 KiB
JavaScript

/* eslint-disable ghost/ember/alias-model-in-controller */
import $ from 'jquery';
import Controller from '@ember/controller';
import PaginationMixin from 'ghost-admin/mixins/pagination';
import Table from 'ember-light-table';
import ghostPaths from 'ghost-admin/utils/ghost-paths';
import {assign} from '@ember/polyfills';
import {computed} from '@ember/object';
import {inject as service} from '@ember/service';
export default Controller.extend(PaginationMixin, {
session: service(),
queryParams: ['order', 'direction'],
order: 'created_at',
direction: 'desc',
paginationModel: 'subscriber',
total: 0,
table: null,
subscriberToDelete: null,
// paginationSettings is replaced by the pagination mixin so we need a
// getter/setter CP here so that we don't lose the dynamic order param
paginationSettings: computed('order', 'direction', {
get() {
let order = this.order;
let direction = this.direction;
let currentSettings = this._paginationSettings || {
limit: 30
};
return assign({}, currentSettings, {
order: `${order} ${direction}`
});
},
set(key, value) {
this._paginationSettings = value;
return value;
}
}),
columns: computed('order', 'direction', function () {
let order = this.order;
let direction = this.direction;
return [{
label: 'Email Address',
valuePath: 'email',
sorted: order === 'email',
ascending: direction === 'asc',
classNames: ['gh-subscribers-table-email-cell'],
cellClassNames: ['gh-subscribers-table-email-cell']
}, {
label: 'Subscription Date',
valuePath: 'createdAtUTC',
format(value) {
return value.format('MMMM DD, YYYY');
},
sorted: order === 'created_at',
ascending: direction === 'asc',
classNames: ['gh-subscribers-table-date-cell'],
cellClassNames: ['gh-subscribers-table-date-cell']
}, {
label: 'Status',
valuePath: 'status',
sorted: order === 'status',
ascending: direction === 'asc',
classNames: ['gh-subscribers-table-status-cell'],
cellClassNames: ['gh-subscribers-table-status-cell']
}, {
label: '',
sortable: false,
cellComponent: 'gh-subscribers-table-delete-cell',
align: 'right',
classNames: ['gh-subscribers-table-delete-cell'],
cellClassNames: ['gh-subscribers-table-delete-cell']
}];
}),
actions: {
loadFirstPage() {
let table = this.table;
return this._super(...arguments).then((results) => {
table.addRows(results);
return results;
});
},
loadNextPage() {
let table = this.table;
return this._super(...arguments).then((results) => {
table.addRows(results);
return results;
});
},
sortByColumn(column) {
let table = this.table;
if (column.sorted) {
this.setProperties({
order: column.get('valuePath').trim().replace(/UTC$/, '').underscore(),
direction: column.ascending ? 'asc' : 'desc'
});
table.setRows([]);
this.send('loadFirstPage');
}
},
addSubscriber(subscriber) {
this.table.insertRowAt(0, subscriber);
this.incrementProperty('total');
},
deleteSubscriber(subscriber) {
this.set('subscriberToDelete', subscriber);
},
confirmDeleteSubscriber() {
let subscriber = this.subscriberToDelete;
return subscriber.destroyRecord().then(() => {
this.set('subscriberToDelete', null);
this.table.removeRow(subscriber);
this.decrementProperty('total');
});
},
cancelDeleteSubscriber() {
this.set('subscriberToDelete', null);
},
reset() {
this.table.setRows([]);
this.send('loadFirstPage');
},
exportData() {
let exportUrl = ghostPaths().url.api('subscribers/csv');
let accessToken = this.get('session.data.authenticated.access_token');
let downloadURL = `${exportUrl}?access_token=${accessToken}`;
let iframe = $('#iframeDownload');
if (iframe.length === 0) {
iframe = $('<iframe>', {id: 'iframeDownload'}).hide().appendTo('body');
}
iframe.attr('src', downloadURL);
}
},
initializeTable() {
this.set('table', new Table(this.columns, this.subscribers));
},
// capture the total from the server any time we fetch a new page
didReceivePaginationMeta(meta) {
if (meta && meta.pagination) {
this.set('total', meta.pagination.total);
}
}
});