fix: prevent duplicate moment upvotes
This commit is contained in:
+33
-6
@@ -15,6 +15,7 @@ const MOMENT_ACTIONS = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let upvotedNames = readUpvotedNames();
|
let upvotedNames = readUpvotedNames();
|
||||||
|
const pendingUpvoteNames = new Set();
|
||||||
let actionListenerRegistered = false;
|
let actionListenerRegistered = false;
|
||||||
|
|
||||||
function readUpvotedNames() {
|
function readUpvotedNames() {
|
||||||
@@ -55,30 +56,56 @@ function incrementUpvoteCount(name) {
|
|||||||
|
|
||||||
function syncUpvotedButtons() {
|
function syncUpvotedButtons() {
|
||||||
document.querySelectorAll(UPVOTE_BUTTON_SELECTOR).forEach(btn => {
|
document.querySelectorAll(UPVOTE_BUTTON_SELECTOR).forEach(btn => {
|
||||||
btn.classList.toggle("active", upvoted(btn.dataset.momentName));
|
const name = btn.dataset.momentName;
|
||||||
|
const pending = pendingUpvoteNames.has(name);
|
||||||
|
|
||||||
|
btn.classList.toggle("active", upvoted(name));
|
||||||
|
btn.disabled = pending;
|
||||||
|
btn.setAttribute("aria-busy", String(pending));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function finishUpvote(name) {
|
||||||
|
pendingUpvoteNames.delete(name);
|
||||||
|
syncUpvotedButtons();
|
||||||
|
}
|
||||||
|
|
||||||
function handleUpvote(name) {
|
function handleUpvote(name) {
|
||||||
if (!name) return;
|
if (!name) return;
|
||||||
if (upvoted(name)) return;
|
if (upvoted(name) || pendingUpvoteNames.has(name)) return;
|
||||||
|
|
||||||
|
pendingUpvoteNames.add(name);
|
||||||
|
syncUpvotedButtons();
|
||||||
|
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
xhr.open("POST", UPVOTE_API);
|
xhr.open("POST", UPVOTE_API);
|
||||||
xhr.setRequestHeader("Content-Type", "application/json");
|
xhr.setRequestHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
xhr.onload = () => {
|
xhr.onload = () => {
|
||||||
if (xhr.status < 200 || xhr.status >= 300) return;
|
try {
|
||||||
|
if (xhr.status >= 200 && xhr.status < 300) {
|
||||||
recordUpvote(name);
|
recordUpvote(name);
|
||||||
incrementUpvoteCount(name);
|
incrementUpvoteCount(name);
|
||||||
syncUpvotedButtons();
|
}
|
||||||
|
} finally {
|
||||||
|
finishUpvote(name);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
xhr.onerror = function () {
|
function handleRequestError() {
|
||||||
|
finishUpvote(name);
|
||||||
alert(getMessage("networkError", "Network error. Please try again."));
|
alert(getMessage("networkError", "Network error. Please try again."));
|
||||||
};
|
}
|
||||||
|
|
||||||
|
xhr.onerror = handleRequestError;
|
||||||
|
xhr.ontimeout = handleRequestError;
|
||||||
|
xhr.onabort = () => finishUpvote(name);
|
||||||
|
|
||||||
|
try {
|
||||||
xhr.send(JSON.stringify({group: RESOURCE_GROUP, plural: RESOURCE_PLURAL, name: name}));
|
xhr.send(JSON.stringify({group: RESOURCE_GROUP, plural: RESOURCE_PLURAL, name: name}));
|
||||||
|
} catch {
|
||||||
|
handleRequestError();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function showComment(name) {
|
function showComment(name) {
|
||||||
|
|||||||
Reference in New Issue
Block a user