2022-09-24 21:45:12 +03:00
|
|
|
import AllSourcesModal from './modals/all-sources';
|
2022-09-22 16:39:44 +03:00
|
|
|
import Component from '@glimmer/component';
|
2022-09-24 21:45:12 +03:00
|
|
|
import {action} from '@ember/object';
|
2022-09-22 16:39:44 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
|
|
|
|
export default class SourceAttributionTable extends Component {
|
|
|
|
@service membersUtils;
|
2022-09-24 21:45:12 +03:00
|
|
|
@service modals;
|
2022-09-22 18:34:46 +03:00
|
|
|
|
2022-09-24 21:45:12 +03:00
|
|
|
@action
|
|
|
|
openAllSources() {
|
2022-09-28 12:13:25 +03:00
|
|
|
// add unavailableSource to sortedSources array only if it has value
|
|
|
|
const allSources = this.unavailableSource ? [...this.sortedSources, this.unavailableSource] : this.sortedSources;
|
|
|
|
|
2022-09-24 21:45:12 +03:00
|
|
|
this.modals.open(AllSourcesModal, {
|
2022-09-29 21:17:12 +03:00
|
|
|
sources: allSources,
|
2022-10-07 12:23:02 +03:00
|
|
|
unavailableSource: this.unavailableSource,
|
2022-09-29 21:17:12 +03:00
|
|
|
sortColumn: this.args.sortColumn
|
2022-09-26 20:59:30 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get unavailableSource() {
|
2022-09-28 12:13:25 +03:00
|
|
|
const emptySource = this.args.sources.find(sourceData => !sourceData.source);
|
|
|
|
if (!emptySource) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...emptySource,
|
|
|
|
source: 'Unavailable'
|
|
|
|
};
|
2022-09-24 21:45:12 +03:00
|
|
|
}
|
|
|
|
|
2022-09-28 12:13:25 +03:00
|
|
|
// Others data includes all sources except the first 5
|
2022-09-24 21:45:12 +03:00
|
|
|
get others() {
|
2022-09-28 12:13:25 +03:00
|
|
|
if (this.sortedSources.length < 5) {
|
2022-09-24 21:45:12 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-10-19 18:01:48 +03:00
|
|
|
if (this.sortedSources.length === 5 && !this.unavailableSource?.length) {
|
2022-09-28 12:13:25 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.sortedSources.slice(5).reduce((acc, source) => {
|
2022-09-22 18:34:46 +03:00
|
|
|
return {
|
2022-09-24 21:45:12 +03:00
|
|
|
signups: acc.signups + source.signups,
|
|
|
|
paidConversions: acc.paidConversions + source.paidConversions
|
2022-09-22 18:34:46 +03:00
|
|
|
};
|
2022-09-24 21:45:12 +03:00
|
|
|
}, {
|
2022-10-07 12:16:29 +03:00
|
|
|
signups: 0,
|
|
|
|
paidConversions: 0
|
2022-09-22 18:34:46 +03:00
|
|
|
});
|
2022-09-24 21:45:12 +03:00
|
|
|
}
|
|
|
|
|
2022-09-26 20:59:30 +03:00
|
|
|
get sortedSources() {
|
|
|
|
return this.args.sources?.filter(source => source.source).sort((a, b) => {
|
|
|
|
if (this.args.sortColumn === 'signups') {
|
|
|
|
return b.signups - a.signups;
|
|
|
|
} else {
|
|
|
|
return b.paidConversions - a.paidConversions;
|
|
|
|
}
|
|
|
|
}) || [];
|
|
|
|
}
|
2022-09-24 21:45:12 +03:00
|
|
|
|
2022-09-26 20:59:30 +03:00
|
|
|
get sources() {
|
2022-09-28 12:13:25 +03:00
|
|
|
if (this.sortedSources.length >= 5) {
|
|
|
|
return this.sortedSources.slice(0, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.unavailableSource ? [...this.sortedSources, this.unavailableSource] : this.sortedSources;
|
2022-09-22 18:34:46 +03:00
|
|
|
}
|
2022-09-22 16:39:44 +03:00
|
|
|
}
|