Skip to content

Commit

Permalink
Merge pull request #230 from germanocaumo/poll-answers-number-group
Browse files Browse the repository at this point in the history
fix(poll): show number of votes and group answers
  • Loading branch information
germanocaumo authored Jan 5, 2023
2 parents b86979c + 83070fe commit 7b59804
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/components/chat/messages/system/poll/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const Result = ({

return(
<div className="poll-label">
{id + 1}: <span className="poll-bar">{getBar(percentage)}</span> {percentage}%
{id + 1}: {numVotes} <span className="poll-bar">{getBar(percentage)}</span> {percentage}%
</div>
);
})}
Expand Down
13 changes: 9 additions & 4 deletions src/utils/builder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parseFromString } from './data/xml2json';
import { files as config } from 'config';
import { getFileType } from './data';
import { getFileType, caseInsensitiveReducer } from './data';
import {
hasProperty,
isEmpty,
Expand Down Expand Up @@ -89,9 +89,14 @@ const buildCaptions = result => {
// TODO
const buildPolls = result => {
if (!result) return [];

let data = [];
data = result;

const data = result.map(r => {
const answers = r.answers.reduce(caseInsensitiveReducer, []);
return {
...r,
answers,
};
});

return data;
};
Expand Down
16 changes: 16 additions & 0 deletions src/utils/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ const getMessageType = (item) => {

const getTimestampAsMilliseconds = timestamp => timestamp * 1000;

const caseInsensitiveReducer = (acc, item) => {
const index = acc.findIndex(ans => ans.key.toLowerCase() === item.key.toLowerCase());
if(index !== -1) {
if(acc[index].numVotes >= item.numVotes) acc[index].numVotes += item.numVotes;
else {
const tempVotes = acc[index].numVotes;
acc[index] = item;
acc[index].numVotes += tempVotes;
}
} else {
acc.push(item);
}
return acc;
};

export {
buildFileURL,
getAvatarStyle,
Expand All @@ -173,4 +188,5 @@ export {
getPercentage,
getPollLabel,
getTimestampAsMilliseconds,
caseInsensitiveReducer
};

0 comments on commit 7b59804

Please sign in to comment.