decide.nolog.cz/app/components/poll-evaluation-summary.js

97 lines
2.1 KiB
JavaScript
Raw Normal View History

import classic from 'ember-classic-decorator';
import { classNames } from '@ember-decorators/component';
2018-12-29 01:27:37 +01:00
import { computed } from '@ember/object';
import { inject as service } from '@ember/service';
import { readOnly, max, mapBy, gt } from '@ember/object/computed';
import Component from '@ember/component';
2018-12-29 01:27:37 +01:00
import { copy } from '@ember/object/internals';
import { isEmpty } from '@ember/utils';
@classic
@classNames('evaluation-summary')
export default class PollEvaluationSummary extends Component {
@service
intl;
2015-11-03 12:57:41 +01:00
@computed('users.[]')
get bestOptions() {
2015-11-03 12:57:41 +01:00
// can not evaluate answer type free text
if (this.get('poll.isFreeText')) {
return undefined;
2015-11-03 12:57:41 +01:00
}
// can not evaluate a poll without users
if (isEmpty(this.users)) {
return undefined;
2015-11-03 12:57:41 +01:00
}
let answers = this.poll.answers.reduce((answers, answer) => {
answers[answer.get('type')] = 0;
return answers;
}, {});
let evaluation = this.poll.options.map((option) => {
return {
answers: copy(answers),
option,
score: 0
};
});
let bestOptions = [];
this.users.forEach((user) => {
user.selections.forEach(({ type }, i) => {
evaluation[i].answers[type]++;
2015-11-03 12:57:41 +01:00
switch (type) {
2015-11-03 12:57:41 +01:00
case 'yes':
evaluation[i].score += 2;
2015-11-03 12:57:41 +01:00
break;
case 'maybe':
evaluation[i].score += 1;
2015-11-03 12:57:41 +01:00
break;
case 'no':
evaluation[i].score -= 2;
2015-11-03 12:57:41 +01:00
break;
}
});
});
evaluation.sort((a, b) => b.score - a.score);
2015-11-03 12:57:41 +01:00
let bestScore = evaluation[0].score;
for (let i = 0; i < evaluation.length; i++) {
2015-11-03 12:57:41 +01:00
if (
bestScore === evaluation[i].score
2015-11-03 12:57:41 +01:00
) {
bestOptions.push(
evaluation[i]
2015-11-03 12:57:41 +01:00
);
2016-01-20 03:19:10 +01:00
} else {
2015-11-03 12:57:41 +01:00
break;
}
}
return bestOptions;
}
@readOnly('intl.primaryLocale')
currentLocale;
2015-11-03 12:57:41 +01:00
@gt('bestOptions.length', 1)
multipleBestOptions;
2015-11-03 12:57:41 +01:00
@max('participationDates')
lastParticipationAt;
@mapBy('users', 'creationDate')
participationDates;
@readOnly('users.length')
participantsCount;
2015-11-03 12:57:41 +01:00
@readOnly('poll.users')
users;
}