Keep vote UI rendered while syncing scores
This commit is contained in:
@@ -5,11 +5,13 @@ const state = {
|
|||||||
authMode: "login",
|
authMode: "login",
|
||||||
me: null,
|
me: null,
|
||||||
phase: null,
|
phase: null,
|
||||||
|
prevPhase: null,
|
||||||
counts: null,
|
counts: null,
|
||||||
mySuggestions: [],
|
mySuggestions: [],
|
||||||
allSuggestions: [],
|
allSuggestions: [],
|
||||||
myVotes: [],
|
myVotes: [],
|
||||||
results: []
|
results: [],
|
||||||
|
votesRendered: false
|
||||||
};
|
};
|
||||||
|
|
||||||
const $ = (id) => document.getElementById(id);
|
const $ = (id) => document.getElementById(id);
|
||||||
@@ -56,11 +58,13 @@ function setAuthMode(mode) {
|
|||||||
function clearUserState() {
|
function clearUserState() {
|
||||||
state.me = null;
|
state.me = null;
|
||||||
state.phase = null;
|
state.phase = null;
|
||||||
|
state.prevPhase = null;
|
||||||
state.counts = null;
|
state.counts = null;
|
||||||
state.mySuggestions = [];
|
state.mySuggestions = [];
|
||||||
state.allSuggestions = [];
|
state.allSuggestions = [];
|
||||||
state.myVotes = [];
|
state.myVotes = [];
|
||||||
state.results = [];
|
state.results = [];
|
||||||
|
state.votesRendered = false;
|
||||||
const adminCard = $("admin-card");
|
const adminCard = $("admin-card");
|
||||||
if (adminCard) adminCard.classList.add("hidden");
|
if (adminCard) adminCard.classList.add("hidden");
|
||||||
}
|
}
|
||||||
@@ -80,8 +84,12 @@ async function loadState() {
|
|||||||
const [me, stateData] = await Promise.all([api.me(), api.state()]);
|
const [me, stateData] = await Promise.all([api.me(), api.state()]);
|
||||||
state.isAuthenticated = true;
|
state.isAuthenticated = true;
|
||||||
state.me = me;
|
state.me = me;
|
||||||
|
state.prevPhase = state.phase;
|
||||||
state.phase = stateData.currentPhase;
|
state.phase = stateData.currentPhase;
|
||||||
state.counts = stateData;
|
state.counts = stateData;
|
||||||
|
if (state.prevPhase !== state.phase && state.phase === "Vote") {
|
||||||
|
state.votesRendered = false;
|
||||||
|
}
|
||||||
setAuthUI(true);
|
setAuthUI(true);
|
||||||
renderWelcome();
|
renderWelcome();
|
||||||
renderPhasePill();
|
renderPhasePill();
|
||||||
@@ -103,10 +111,14 @@ async function loadRevealData() {
|
|||||||
|
|
||||||
async function loadVoteData() {
|
async function loadVoteData() {
|
||||||
if (state.phase !== "Vote") return;
|
if (state.phase !== "Vote") return;
|
||||||
// avoid re-rendering vote cards mid-drag: only update scores map
|
|
||||||
const votes = await api.myVotes();
|
const votes = await api.myVotes();
|
||||||
state.myVotes = votes;
|
state.myVotes = votes;
|
||||||
syncVoteScores();
|
if (!state.votesRendered) {
|
||||||
|
renderVotes();
|
||||||
|
state.votesRendered = true;
|
||||||
|
} else {
|
||||||
|
syncVoteScores();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadResults() {
|
async function loadResults() {
|
||||||
@@ -196,6 +208,18 @@ function renderVotes() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function syncVoteScores() {
|
||||||
|
const votesMap = Object.fromEntries(state.myVotes.map((v) => [v.suggestionId, v.score]));
|
||||||
|
Object.entries(votesMap).forEach(([id, score]) => {
|
||||||
|
const slider = document.querySelector(`input[type=range][data-id="${id}"]`);
|
||||||
|
const scoreLabel = $("score-" + id);
|
||||||
|
if (slider && score != null) {
|
||||||
|
slider.value = score;
|
||||||
|
if (scoreLabel) scoreLabel.textContent = score;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function renderResults() {
|
function renderResults() {
|
||||||
const container = $("results-list");
|
const container = $("results-list");
|
||||||
container.innerHTML = "";
|
container.innerHTML = "";
|
||||||
@@ -381,8 +405,12 @@ async function refreshPhaseData() {
|
|||||||
try {
|
try {
|
||||||
await loadState();
|
await loadState();
|
||||||
await Promise.all([loadSuggestData(), loadRevealData(), loadResults()]);
|
await Promise.all([loadSuggestData(), loadRevealData(), loadResults()]);
|
||||||
// vote data only refreshes via explicit calls to avoid interrupting slider drags
|
if (state.phase === "Vote") {
|
||||||
if (state.phase !== "Vote") {
|
if (!state.votesRendered) {
|
||||||
|
await loadVoteData();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
state.votesRendered = false;
|
||||||
await loadVoteData();
|
await loadVoteData();
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
Reference in New Issue
Block a user