Ghost/ghost/admin/app/services/members-stats.js
Kevin Ansfield 147943f3a3 Fixed members list/chart not updating after members import
no issue

- move members loading code into `members` controller so that it's more accessible to other areas of the app
- add `refreshData()` to the `members` controller which forces members list and stats to be re-fetched
- call `members.refreshData()` after successful upload of members import file
- store range/days in `membersStats` service so that it's remembered across refreshes and component renders
2020-06-01 15:48:46 +01:00

48 lines
1.4 KiB
JavaScript

import Service from '@ember/service';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency-decorators';
import {tracked} from '@glimmer/tracking';
export default class MembersStatsService extends Service {
@service ajax;
@service ghostPaths;
@tracked days = '30';
@tracked stats = null;
fetch() {
let daysChanged = this._lastFetchedDays !== this.days;
let staleData = this._lastFetched && this._lastFetched - new Date() > 1 * 60 * 1000;
// return an already in-progress promise unless params have changed
if (this._fetchTask.isRunning && !this._forceRefresh && !daysChanged) {
return this._fetchTask.last;
}
// return existing stats unless data is > 1 min old
if (this.stats && !this._forceRefresh && !daysChanged && !staleData) {
return Promise.resolve(this.stats);
}
return this._fetchTask.perform();
}
invalidate() {
this._forceRefresh = true;
}
@task
*_fetchTask() {
let {days} = this;
this._lastFetchedDays = days;
this._lastFetched = new Date();
this._forceRefresh = false;
let statsUrl = this.ghostPaths.url.api('members/stats');
let stats = yield this.ajax.request(statsUrl, {data: {days}});
this.stats = stats;
return stats;
}
}