Sory by Least or Most votes

This commit is contained in:
QuickMythril 2025-01-09 15:02:52 -05:00
parent 37a9150f2d
commit 06a835b0b2

View File

@ -32,6 +32,8 @@ const loadMinterBoardPage = async () => {
<option value="newest" selected>Sort by Date</option>
<option value="name">Sort by Name</option>
<option value="recent-comments">Newest Comments</option>
<option value="least-votes">Least Votes</option>
<option value="most-votes">Most Votes</option>
</select>
<div id="cards-container" class="cards-container" style="margin-top: 20px;"></div>
<div id="publish-card-view" class="publish-card-view" style="display: none; text-align: left; padding: 20px;">
@ -270,7 +272,131 @@ const loadCards = async () => {
finalCards.sort((a, b) =>
(b.newestCommentTimestamp || 0) - (a.newestCommentTimestamp || 0)
)
}
} else if (selectedSort === 'least-votes') {
// For each card, fetch its poll data so we know how many admin + minter votes it has.
// Store those values on the card object so we can sort on them.
// Sort ascending by admin votes; if there's a tie, sort ascending by minter votes.
const minterGroupMembers = await fetchMinterGroupMembers()
const minterAdmins = await fetchMinterGroupAdmins()
// For each card, fetch the poll data & store counts on the card object.
for (const card of finalCards) {
try {
// We must fetch the card data from QDN to get the `poll` name
const cardDataResponse = await qortalRequest({
action: "FETCH_QDN_RESOURCE",
name: card.name,
service: "BLOG_POST",
identifier: card.identifier,
})
// If the card or poll is missing, skip
if (!cardDataResponse || !cardDataResponse.poll) {
card._adminVotes = 0
card._adminYes = 0
card._minterVotes = 0
card._minterYes = 0
continue
}
const pollResults = await fetchPollResults(cardDataResponse.poll)
const {
adminYes,
adminNo,
minterYes,
minterNo
} = await processPollData(
pollResults,
minterGroupMembers,
minterAdmins,
cardDataResponse.creator,
card.identifier
)
// Store the totals so we can sort on them
card._adminVotes = adminYes + adminNo
card._adminYes = adminYes
card._minterVotes = minterYes + minterNo
card._minterYes = minterYes
} catch (error) {
console.warn(`Error fetching or processing poll for card ${card.identifier}:`, error)
// If something fails, default to zero so it sorts "lowest"
card._adminVotes = 0
card._adminYes = 0
card._minterVotes = 0
card._minterYes = 0
}
}
// Now that each card has _adminVotes + _minterVotes, do an ascending sort:
finalCards.sort((a, b) => {
// admin votes ascending
const diffAdminTotal = a._adminVotes - b._adminVotes
if (diffAdminTotal !== 0) return diffAdminTotal
// admin YES ascending
const diffAdminYes = a._adminYes - b._adminYes
if (diffAdminYes !== 0) return diffAdminYes
// minter votes ascending
const diffMinterTotal = a._minterVotes - b._minterVotes
if (diffMinterTotal !== 0) return diffMinterTotal
// minter YES ascending
return a._minterYes - b._minterYes
})
} else if (selectedSort === 'most-votes') {
// Fetch poll data & store admin + minter votes as before.
// Sort descending by admin votes; if there's a tie, sort descending by minter votes.
const minterGroupMembers = await fetchMinterGroupMembers()
const minterAdmins = await fetchMinterGroupAdmins()
for (const card of finalCards) {
try {
const cardDataResponse = await qortalRequest({
action: "FETCH_QDN_RESOURCE",
name: card.name,
service: "BLOG_POST",
identifier: card.identifier,
})
if (!cardDataResponse || !cardDataResponse.poll) {
card._adminVotes = 0
card._adminYes = 0
card._minterVotes = 0
card._minterYes = 0
continue
}
const pollResults = await fetchPollResults(cardDataResponse.poll)
const {
adminYes,
adminNo,
minterYes,
minterNo
} = await processPollData(
pollResults,
minterGroupMembers,
minterAdmins,
cardDataResponse.creator,
card.identifier
)
card._adminVotes = adminYes + adminNo
card._adminYes = adminYes
card._minterVotes = minterYes + minterNo
card._minterYes = minterYes
} catch (error) {
console.warn(`Error fetching or processing poll for card ${card.identifier}:`, error)
card._adminVotes = 0
card._adminYes = 0
card._minterVotes = 0
card._minterYes = 0
}
}
// Sort descending by admin votes, then minter votes
finalCards.sort((a, b) => {
// admin votes descending
const diffAdminTotal = b._adminVotes - a._adminVotes
if (diffAdminTotal !== 0) return diffAdminTotal
// admin YES descending
const diffAdminYes = b._adminYes - a._adminYes
if (diffAdminYes !== 0) return diffAdminYes
// minter votes descending
const diffMinterTotal = b._minterVotes - a._minterVotes
if (diffMinterTotal !== 0) return diffMinterTotal
// minter YES descending
return b._minterYes - a._minterYes
})
}
// else 'newest' => do nothing, finalCards stays in newest-first order
// Display skeleton cards immediately