2022-03-22 18:37:17 +03:00
|
|
|
import Component from '@glimmer/component';
|
2022-03-23 18:38:16 +03:00
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
2022-03-24 11:38:30 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
const MODE_OPTIONS = [{
|
|
|
|
name: 'Cadence',
|
|
|
|
value: 'cadence'
|
|
|
|
}, {
|
|
|
|
name: 'Tiers',
|
|
|
|
value: 'tiers'
|
|
|
|
}];
|
2022-03-22 18:37:17 +03:00
|
|
|
|
|
|
|
export default class ChartPaidMix extends Component {
|
2022-03-23 18:38:16 +03:00
|
|
|
@service dashboardStats;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this method when you need to fetch new data from the server. In this component, it will get called
|
|
|
|
* when the days parameter changes and on initialisation.
|
|
|
|
*/
|
|
|
|
@action
|
|
|
|
loadCharts() {
|
|
|
|
// The dashboard stats service will take care or reusing and limiting API-requests between charts
|
|
|
|
if (this.mode === 'cadence') {
|
|
|
|
this.dashboardStats.loadPaidMembersByCadence();
|
|
|
|
} else {
|
|
|
|
this.dashboardStats.loadPaidMembersByTier();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 11:38:30 +03:00
|
|
|
@tracked mode = 'cadence';
|
|
|
|
modeOptions = MODE_OPTIONS;
|
|
|
|
|
|
|
|
get selectedModeOption() {
|
|
|
|
return this.modeOptions.find(option => option.value === this.mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
onSwitchMode(selected) {
|
|
|
|
this.mode = selected.value;
|
|
|
|
|
|
|
|
if (this.loading) {
|
|
|
|
// We don't have the data yet for the newly selected mode
|
|
|
|
this.loadCharts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:38:16 +03:00
|
|
|
get loading() {
|
|
|
|
if (this.mode === 'cadence') {
|
|
|
|
return this.dashboardStats.paidMembersByCadence === null;
|
|
|
|
}
|
|
|
|
return this.dashboardStats.paidMembersByTier === null;
|
|
|
|
}
|
|
|
|
|
2022-03-22 18:37:17 +03:00
|
|
|
get chartType() {
|
|
|
|
return 'pie';
|
|
|
|
}
|
|
|
|
|
|
|
|
get chartData() {
|
2022-03-23 18:38:16 +03:00
|
|
|
if (this.mode === 'cadence') {
|
|
|
|
return {
|
|
|
|
labels: ['Monthly', 'Annual'],
|
|
|
|
datasets: [{
|
|
|
|
data: [this.dashboardStats.paidMembersByCadence.monthly, this.dashboardStats.paidMembersByCadence.annual],
|
|
|
|
fill: false,
|
|
|
|
backgroundColor: ['#14b8ff'],
|
|
|
|
tension: 0.1
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
}
|
2022-03-24 11:38:30 +03:00
|
|
|
|
|
|
|
const labels = this.dashboardStats.paidMembersByTier.map(stat => stat.tier.name);
|
|
|
|
const data = this.dashboardStats.paidMembersByTier.map(stat => stat.members);
|
|
|
|
|
|
|
|
return {
|
|
|
|
labels,
|
|
|
|
datasets: [{
|
|
|
|
data,
|
|
|
|
fill: false,
|
|
|
|
backgroundColor: ['#14b8ff'],
|
|
|
|
tension: 0.1
|
|
|
|
}]
|
|
|
|
};
|
2022-03-22 18:37:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get chartOptions() {
|
|
|
|
return {
|
|
|
|
legend: {
|
|
|
|
display: false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
get chartHeight() {
|
2022-03-24 17:02:13 +03:00
|
|
|
return 100;
|
2022-03-22 18:37:17 +03:00
|
|
|
}
|
|
|
|
}
|