From fadd569a03ec8b1d1e80d05b62cdf01713cb0980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Novo?= <34069419+TeknoPT@users.noreply.github.com> Date: Tue, 9 Jan 2024 16:47:26 +0000 Subject: [PATCH] Update to the polls - Added auto filtering to the list voting v1 - Auto 7 days of the poll. --- package.json | 2 +- src/lib/Components/Votes/CreatePoll.svelte | 6 +- src/lib/Components/Votes/PollDetails.svelte | 26 ++++--- src/routes/votes/+page.svelte | 79 +++++++++++++++++++++ static/version.json | 2 +- 5 files changed, 100 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 9e7335d..a20c115 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "phantasma-hub", - "version": "1.0.9", + "version": "1.0.10", "private": true, "scripts": { "dev": "npm run version:update && vite dev --cors true", diff --git a/src/lib/Components/Votes/CreatePoll.svelte b/src/lib/Components/Votes/CreatePoll.svelte index 2fd54e9..e7b65b2 100644 --- a/src/lib/Components/Votes/CreatePoll.svelte +++ b/src/lib/Components/Votes/CreatePoll.svelte @@ -74,7 +74,9 @@ function handleStartTimeChange() { let startDate = new Date(startTimeStr); - let formattedDate = moment(startDate.getTime() + 1000 * 60 * 60 * 24).format(DateTimeFormat); + let formattedDate = moment(startDate.getTime() + 1000 * 60 * 60 * 24 * 7).format( + DateTimeFormat + ); endTimeStr = formattedDate; validateSubject(); @@ -86,7 +88,7 @@ startTimeStr = moment(startTime.getTime()).format(DateTimeFormat); let startDate = new Date(startTimeStr); //endTime = moment(startDate.getTime() + 1000 * 60 * 60 * 24).format('yyyy-MM-DDThh:mm'); - endTimeStr = moment(startDate.getTime() + 1000 * 60 * 60 * 24).format(DateTimeFormat); + endTimeStr = moment(startDate.getTime() + 1000 * 60 * 60 * 24 * 7).format(DateTimeFormat); } function onSubjectChange() { diff --git a/src/lib/Components/Votes/PollDetails.svelte b/src/lib/Components/Votes/PollDetails.svelte index f65b3fe..e082c20 100644 --- a/src/lib/Components/Votes/PollDetails.svelte +++ b/src/lib/Components/Votes/PollDetails.svelte @@ -34,6 +34,7 @@ let choices: PollChoice[] = new Array(); let entries: PollValue[] = new Array(); let selected; + let pollStateColor: string; afterUpdate(async () => { Init(); @@ -64,19 +65,21 @@ function initPollState() { console.log('Init Poll State'); - if (PollState.Inactive == poll.state) { - console.log( - poll.startTime.value <= timeNow, - poll.endTime.value >= timeNow, - poll.startTime.value, - poll.endTime.value, - timeNow - ); - if (poll.startTime.value <= timeNow && poll.endTime.value >= timeNow) { - poll.state = PollState.Active; - } else if (timeNow >= poll.endTime.value) { + if (timeNow >= poll.startTime.value && poll.endTime.value >= timeNow) { + poll.state = PollState.Active; + pollStateColor = 'bg-blue-300'; + if (poll.endTime.value - timeNow <= 60 * 12) { + pollStateColor = 'bg-yellow-300'; + } + } else if (timeNow >= poll.endTime.value) { + if (poll.state == PollState.Consensus) { + pollStateColor = 'bg-green-300'; + } else { poll.state = PollState.Failure; + pollStateColor = 'bg-red-300'; } + } else { + pollStateColor = 'bg-blue-300'; } } @@ -95,6 +98,7 @@ description="Consensus Poll details for {poll.subject}{poll.endTime.value * 1000 >= Date.now() ? ` (Finish ${moment(poll.endTime.value * 1000).fromNow()})` : ''}." + class={pollStateColor} >
null}> diff --git a/src/routes/votes/+page.svelte b/src/routes/votes/+page.svelte index 1eef03c..c5e40df 100644 --- a/src/routes/votes/+page.svelte +++ b/src/routes/votes/+page.svelte @@ -6,6 +6,7 @@ import { getConsensusPoll, getConsensusPolls } from '$lib/Components/Wallet/VoteCommands'; import { IsPollCreated, PhantasmaAPIClient } from '$lib/store'; import { type ConsensusPoll, type Organization, PhantasmaAPI } from 'phantasma-ts/src'; + import { PollState, Timestamp } from 'phantasma-ts'; let api: PhantasmaAPI; PhantasmaAPIClient.subscribe(async (value) => { @@ -29,8 +30,86 @@ let pollSelected: ConsensusPoll | null = null; //let nullPoll: ConsensusPoll | null | undefined = null; + function fixPollsData() { + const timeNow = Timestamp.now; + + for (let i = 0; i < polls.length; i++) { + if (polls[i].state == PollState.Consensus || polls[i].state == PollState.Failure) { + continue; + } + + if (polls[i].startTime.value <= timeNow && polls[i].endTime.value >= timeNow) { + polls[i].state = PollState.Active; + } else if (timeNow >= polls[i].endTime.value) { + if (polls[i].state == PollState.Consensus) { + polls[i].state = PollState.Consensus; + } else { + polls[i].state = PollState.Failure; + } + } + } + } + async function getPolls() { polls = await getConsensusPolls(); + + // Fix polls + if (polls.length > 0) fixPollsData(); + + const timeNow = Timestamp.now; + + // sort polls + polls = polls.sort((a, b) => { + // Rule 1: Active polls should be at the top + if (a.state === PollState.Active && b.state !== PollState.Active) { + return -1; + } + if (b.state === PollState.Active && a.state !== PollState.Active) { + return 1; + } + + // Rule 5: Inactive polls within startTime and endTime should be above Consensus but below Active + const now = new Date(); + const aInactiveValid = + a.state === PollState.Inactive && + timeNow >= a.startTime.value && + timeNow <= a.endTime.value; + const bInactiveValid = + b.state === PollState.Inactive && + timeNow >= b.startTime.value && + timeNow <= b.endTime.value; + + if (aInactiveValid && !bInactiveValid) { + return -1; + } + if (bInactiveValid && !aInactiveValid) { + return 1; + } + + // Rule 2: Consensus polls should be below Active but above others + if (a.state === PollState.Consensus && b.state !== PollState.Consensus) { + return -1; + } + if (b.state === PollState.Consensus && a.state !== PollState.Consensus) { + return 1; + } + + // Rule 3 and 4: Failure polls and inactive polls with a past endTime should go down + if ( + a.state === PollState.Failure || + (a.state === PollState.Inactive && timeNow > a.endTime.value) + ) { + return 1; + } + if ( + b.state === PollState.Failure || + (b.state === PollState.Inactive && timeNow > b.endTime.value) + ) { + return -1; + } + + return 0; // No change for equal states or other conditions + }); } async function getPoll(subject: string) { diff --git a/static/version.json b/static/version.json index f8696f8..3541bde 100644 --- a/static/version.json +++ b/static/version.json @@ -1,3 +1,3 @@ { - "version": "1.0.9" + "version": "1.0.10" } \ No newline at end of file