forked from quic-issues/427e7578-d7bf-49c8-aee9-2dd999e25316
* fix keep old state and only update on legal actions
This commit is contained in:
@@ -11,6 +11,10 @@ export function PollList(
|
||||
onVote: (optionId: string) => void,
|
||||
onDelete: (optionId: string) => void,
|
||||
): HTMLElement {
|
||||
|
||||
var currentOptions : { [x: string]: any; } | undefined = undefined
|
||||
var currentVotes : { [x: string]: any; } | undefined = undefined
|
||||
|
||||
const wrapper = document.createElement("div");
|
||||
wrapper.className = "poll-list-wrapper";
|
||||
|
||||
@@ -42,25 +46,29 @@ export function PollList(
|
||||
votes: number;
|
||||
voted: boolean;
|
||||
}> = [];
|
||||
if (currentOptions && currentVotes){
|
||||
|
||||
// Tally votes per option
|
||||
const tally = new Map<string, number>();
|
||||
for (const optionId of yVotes.values()) {
|
||||
tally.set(optionId, (tally.get(optionId) ?? 0) + 1);
|
||||
// Tally votes per option
|
||||
const tally = new Map<string, number>();
|
||||
for (const optionId of Object.values(currentVotes)) {
|
||||
tally.set(optionId, (tally.get(optionId) ?? 0) + 1);
|
||||
}
|
||||
|
||||
const myVote = currentVotes[userId] ?? null;
|
||||
|
||||
Object.entries(currentOptions).forEach(([id,record]) => {
|
||||
console.log(`${record}: ${id}`)
|
||||
entries.push({
|
||||
id,
|
||||
name: record.label,
|
||||
votes: tally.get(id) ?? 0,
|
||||
voted: myVote === id,
|
||||
});
|
||||
});
|
||||
|
||||
entries.sort((a, b) => b.votes - a.votes || a.name.localeCompare(b.name));
|
||||
}
|
||||
|
||||
const myVote = yVotes.get(userId) ?? null;
|
||||
|
||||
yOptions.forEach((record, id) => {
|
||||
entries.push({
|
||||
id,
|
||||
name: record.label,
|
||||
votes: tally.get(id) ?? 0,
|
||||
voted: myVote === id,
|
||||
});
|
||||
});
|
||||
|
||||
entries.sort((a, b) => b.votes - a.votes || a.name.localeCompare(b.name));
|
||||
return entries;
|
||||
}
|
||||
|
||||
@@ -119,9 +127,10 @@ export function PollList(
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
yOptions.observe(enforceAppendOnly(yOptions,render));
|
||||
yVotes.observe(enforceAppendOnly(yVotes,render));
|
||||
yOptions.observe(enforceAppendOnly(yOptions,(update : { [x: string]: any; }) => {currentOptions = update}, render));
|
||||
yVotes.observe(enforceAppendOnly(yVotes,(update : { [x: string]: any; }) => {currentVotes = update},render));
|
||||
currentOptions=yOptions.toJSON()
|
||||
currentVotes=yVotes.toJSON()
|
||||
render();
|
||||
|
||||
return wrapper;
|
||||
|
||||
Reference in New Issue
Block a user