2022-02-09 19:43:58 +03:00
|
|
|
import {Resource} from 'ember-could-get-used-to-this';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {task} from 'ember-concurrency';
|
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
export default class MembersCount extends Resource {
|
2022-05-05 00:51:34 +03:00
|
|
|
@service membersCountCache;
|
2022-05-11 20:56:23 +03:00
|
|
|
@service session;
|
2022-02-09 19:43:58 +03:00
|
|
|
|
|
|
|
@tracked count = null;
|
|
|
|
|
|
|
|
get value() {
|
|
|
|
return {
|
|
|
|
isLoading: this.fetchMembersTask.isRunning,
|
|
|
|
count: this.count
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
setup() {
|
|
|
|
const query = this.args.named.query || {};
|
|
|
|
this._query = query;
|
|
|
|
this.fetchMembersTask.perform({query});
|
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
|
|
|
// required due to a weird invalidation issue when using Ember Data with ember-could-get-used-to-this
|
|
|
|
// TODO: re-test after upgrading to ember-resources
|
|
|
|
if (this.args.named.query !== this._query) {
|
|
|
|
const query = this.args.named.query || {};
|
|
|
|
this._query = query;
|
|
|
|
this.fetchMembersTask.perform({query});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*fetchMembersTask({query} = {}) {
|
2022-05-11 20:56:23 +03:00
|
|
|
// Only Admins/Owners have access to the /members/ endpoint to fetch a count.
|
|
|
|
// For other roles simply leave it as `null` so templates can react accordingly
|
|
|
|
if (!this.session.user.isAdmin) {
|
|
|
|
this.count = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-05 00:51:34 +03:00
|
|
|
const count = yield this.membersCountCache.count(query);
|
|
|
|
this.count = count;
|
2022-02-09 19:43:58 +03:00
|
|
|
}
|
|
|
|
}
|