move live-result sort to service

This commit is contained in:
KDSBrowne 2018-11-01 18:20:35 +00:00
parent deac142296
commit 9e483a181e
2 changed files with 29 additions and 25 deletions

View File

@ -5,6 +5,7 @@ import { defineMessages, injectIntl } from 'react-intl';
import { Session } from 'meteor/session';
import Button from '/imports/ui/components/button/component';
import { styles } from './styles';
import Service from './service';
const intlMessages = defineMessages({
usersTitle: {
@ -44,30 +45,6 @@ class LiveResult extends Component {
? [...users, ...responses.map(u => u.userId)]
: [...users];
const sortByResponse = (a, b) => {
if (a.answer.toLowerCase() > b.answer.toLowerCase()) {
return -1;
} else if (a.answer.toLowerCase() < b.answer.toLowerCase()) {
return 1;
}
return 0;
};
const sortByName = (a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) {
return -1;
} else if (a.name.toLowerCase() > b.name.toLowerCase()) {
return 1;
}
return 0;
};
const sortUsers = (a, b) => {
let sort = sortByResponse(a, b);
if (sort === 0) sort = sortByName(a, b);
return sort;
};
userAnswers = userAnswers.map(id => getUser(id))
.filter(user => user.connectionStatus === 'online')
.map((user) => {
@ -83,7 +60,7 @@ class LiveResult extends Component {
answer,
};
})
.sort(sortUsers)
.sort(Service.sortUsers)
.reduce((acc, user) => [
...acc,
<div className={styles.item} key={_.uniqueId('stats-')}>{user.name}</div>,

View File

@ -0,0 +1,27 @@
const sortUsers = (a, b) => {
const sortByResponse = (a, b) => {
if (a.answer.toLowerCase() > b.answer.toLowerCase()) {
return -1;
} else if (a.answer.toLowerCase() < b.answer.toLowerCase()) {
return 1;
}
return 0;
};
const sortByName = (a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) {
return -1;
} else if (a.name.toLowerCase() > b.name.toLowerCase()) {
return 1;
}
return 0;
};
let sort = sortByResponse(a, b);
if (sort === 0) sort = sortByName(a, b);
return sort;
};
export default {
sortUsers,
};