diff --git a/components/builder/BuilderComponent.js b/components/builder/BuilderComponent.js index 55f2a563..c2e972c3 100644 --- a/components/builder/BuilderComponent.js +++ b/components/builder/BuilderComponent.js @@ -1,13 +1,13 @@ +import { nanoid } from 'nanoid'; import Router from 'next/router'; import { useEffect, useState } from "react"; -import { Button, Col, Container, Form, Row } from "react-bootstrap"; +import { Button, Form } from "react-bootstrap"; import { v4 as uuidv4 } from "uuid"; -import { deployContract } from "../../services/contract" -import { getRecentBlock } from "../../services/utils"; import { getFileFromGaia, getMyStxAddress, getUserData, putFileToGaia } from "../../services/auth.js"; +import { deployContract } from "../../services/contract"; +import { getRecentBlock } from "../../services/utils"; import styles from "../../styles/Builder.module.css"; import PreviewComponent from "./Preview.component"; -import { nanoid } from 'nanoid' export default function BuilderComponent(props) { // Variables diff --git a/components/common/DashboardNavBarComponent.js b/components/common/DashboardNavBarComponent.js index bd3508dc..a778f147 100644 --- a/components/common/DashboardNavBarComponent.js +++ b/components/common/DashboardNavBarComponent.js @@ -35,28 +35,6 @@ export function DashboardNavBarComponent() { setDisplayUsername(_username); } - const loadBlockSurveyWidgetScript = () => { - // If script is not loaded, load first - if (!window.blocksurvey) { - let body = document.body; - let script = document.createElement("script"); - script.innerHTML = ""; - script.src = "https://blocksurvey.io/assets/js/blocksurvey-widget.js"; - script.async = true; - script.defer = true; - script.onload = () => { - window.blocksurvey = { - loaded: true, - }; - - feedbackButton.current.click(); - }; - body.appendChild(script); - } else { - feedbackButton.current.click(); - } - }; - // UI return ( <> @@ -72,11 +50,11 @@ export function DashboardNavBarComponent() { isUserSignedIn ?
- + - +
@@ -112,9 +90,8 @@ export function DashboardNavBarComponent() { { - loadBlockSurveyWidgetScript(); - }}> + href="https://github.com/BlockSurvey/ballot/issues" + target="_blank"> Share feedback { signOut() }}>Logout @@ -136,26 +113,6 @@ export function DashboardNavBarComponent() { handleCloseMyVotesPopup={handleCloseMyVotesPopup} handleShowMyVotesPopup={handleShowMyVotesPopup} /> - - {/* Adding BlockSurvey script */} - - ); } \ No newline at end of file diff --git a/components/common/HeaderComponent.js b/components/common/HeaderComponent.js index fbc47e3e..abc9f10e 100644 --- a/components/common/HeaderComponent.js +++ b/components/common/HeaderComponent.js @@ -43,7 +43,10 @@ export default function HeaderComponent(props) {
{/* Status */}
- {pollObject?.status == "draft" ? "Draft" : "Active"} {' '} + {pollObject?.status == "draft" ? "Draft" : + ((pollObject?.endAtDate && (new Date(pollObject?.endAtDate) < new Date())) ? + (Closed) : "Active")} + {' '}
{/* Created by */} diff --git a/components/common/InformationComponent.js b/components/common/InformationComponent.js index 3453b79b..128231e9 100644 --- a/components/common/InformationComponent.js +++ b/components/common/InformationComponent.js @@ -1,3 +1,4 @@ +import { Table } from "react-bootstrap"; import { Constants } from "../../common/constants"; import { convertToDisplayDateFormat, formStacksExplorerUrl } from "../../services/utils"; @@ -116,14 +117,26 @@ export default function InformationComponent(props) {
Current results
- {pollObject?.options?.map((option, index) => ( -
- {option?.value} - - {resultsByOption && resultsByOption[option.id] ? resultsByOption[option.id] : "-"} - -
- ))} + + + {pollObject?.options?.map((option, index) => ( + + + + + ))} + +
+ {option?.value} + + + {resultsByOption && resultsByOption[option.id] ? + <> + {resultsByOption[option.id]["percentage"]}% ({resultsByOption[option.id]["total"]}) + + : "-"} + +
diff --git a/components/common/MyVotesPopup.js b/components/common/MyVotesPopup.js index 3b067f19..bfe02802 100644 --- a/components/common/MyVotesPopup.js +++ b/components/common/MyVotesPopup.js @@ -125,7 +125,7 @@ export default function MyVotePopup(props) { ) : // Once data found votes && votes.length > 0 ? (
- +
diff --git a/components/dashboard/DashboardAllPollsComponent.js b/components/dashboard/DashboardAllPollsComponent.js index a094da9a..ef662e8b 100644 --- a/components/dashboard/DashboardAllPollsComponent.js +++ b/components/dashboard/DashboardAllPollsComponent.js @@ -1,9 +1,9 @@ -import { useEffect, useState } from "react"; -import { Col, Container, Row, Button } from "react-bootstrap"; -import styles from "../../styles/Dashboard.module.css"; -import { deleteFileToGaia, getFileFromGaia, getUserData, putFileToGaia } from "../../services/auth.js" import Link from "next/link"; +import { useEffect, useState } from "react"; +import { Button } from "react-bootstrap"; +import { deleteFileToGaia, getFileFromGaia, getUserData, putFileToGaia } from "../../services/auth.js"; import { convertToDisplayDateFormat } from "../../services/utils"; +import styles from "../../styles/Dashboard.module.css"; export default function DashboardAllPollsComponent() { // Variables @@ -66,7 +66,11 @@ export default function DashboardAllPollsComponent() {
- Status : {pollIndexObject?.status == "draft" ? "Draft" : "Active"} + Status : { + pollIndexObject?.status == "draft" ? "Draft" : + ((pollIndexObject?.endAt && (new Date(pollIndexObject?.endAt) < new Date())) ? + (Closed) : "Active") + } {', '} diff --git a/components/poll/PollComponent.js b/components/poll/PollComponent.js index d2548d69..cdc52721 100644 --- a/components/poll/PollComponent.js +++ b/components/poll/PollComponent.js @@ -38,8 +38,8 @@ export default function PollComponent(props) { const handleShow = () => setShow(true); const handleClose = () => setShow(false); - // Loading - const [isLoading, setIsLoading] = useState(false); + // Processing + const [isProcessing, setIsProcessing] = useState(false); // Functions useEffect(() => { @@ -127,7 +127,7 @@ export default function PollComponent(props) { const castMyVote = () => { if (pollObject?.publishedInfo?.contractAddress && pollObject?.publishedInfo?.contractName) { // Start processing - setIsLoading(true); + setIsProcessing(true); const contractAddress = pollObject?.publishedInfo?.contractAddress; const contractName = pollObject?.publishedInfo?.contractName; @@ -183,7 +183,8 @@ export default function PollComponent(props) { {/* Vote button */} {isUserSignedIn ? @@ -219,50 +220,68 @@ export default function PollComponent(props) { {/* Results */}
-
Votes ({total})
-
Title
+
Votes {total >= 0 ? <>({total}) : ""}
+
- + - {Object.keys(resultsByPosition).map((position, index) => ( - - + - + + + + )) + : + + + + ) + : + + - - ))} + }
Address OptionVoting PowerVoting power
{resultsByPosition[position]?.address && - - - {resultsByPosition[position]?.address} { } - - - - - + {total >= 0 ? + (total > 0 ? + Object.keys(resultsByPosition).map((position, index) => ( +
{resultsByPosition[position]?.address && + + + {resultsByPosition[position]?.address} { } + + + + + - } - {Object.keys(resultsByPosition[position]?.vote).map((optionId, voteIndex) => ( -
- {optionsMap[optionId] ? optionsMap[optionId] : "-"} -
- ))} + }
+ {Object.keys(resultsByPosition[position]?.vote).map((optionId, voteIndex) => ( +
+ {optionsMap[optionId] ? optionsMap[optionId] : "-"} +
+ ))} +
+ {Object.values(resultsByPosition[position]?.vote)?.[0]} +
+ No data found +
+ Loading 1
@@ -281,6 +300,7 @@ export default function PollComponent(props) { }
+ {/* Success message popup */} Information diff --git a/pages/p/[[...id]].js b/pages/p/[[...id]].js index c08e726c..c798dc2a 100644 --- a/pages/p/[[...id]].js +++ b/pages/p/[[...id]].js @@ -12,7 +12,6 @@ import { getMyStxAddress, getStacksAPIPrefix, userSession } from "../../services export default function Poll(props) { const { pollObject, pollId, gaiaAddress } = props; - // const [pollObject, setPollObject] = useState(); const [publicUrl, setPublicUrl] = useState(); const [optionsMap, setOptionsMap] = useState({}); const [resultsByOption, setResultsByOption] = useState({}); @@ -188,16 +187,21 @@ export default function Poll(props) { if (content && content.okay) { const results = cvToValue(parseReadOnlyResponse(content)).value; - let resultsObj = {}; - results?.options?.value.forEach((option, index) => { - resultsObj[option?.value] = results?.results?.value?.[index]?.value; - }); - setResultsByOption(resultsObj) - - const total = parseInt(results?.total?.value); + const total = parseInt(results?.["total-with-voting-power"]?.value ? (results?.["total-with-voting-power"]?.value) : (results?.total?.value)); setTotal(total); - getFirstTenResults((total > 10 ? 10 : total), contractAddress, contractName); + let resultsObj = {}; + results?.options?.value.forEach((option, index) => { + resultsObj[option?.value] = { + total: results?.results?.value?.[index]?.value, + percentage: results?.results?.value?.[index]?.value == 0 ? 0 : ((results?.results?.value?.[index]?.value / total) * 100) + }; + }); + setResultsByOption(resultsObj); + + getFirstTenResults((results?.total?.value > 10 ? 10 : results?.total?.value), contractAddress, contractName); + } else { + setTotal(0); } } }; diff --git a/services/contract.js b/services/contract.js index 34e4c694..442166df 100644 --- a/services/contract.js +++ b/services/contract.js @@ -119,6 +119,7 @@ function getRawContract() { (define-map users {id: principal} {count: uint, vote: (list &{noOfOptions} (string-ascii 36)), volume: (list &{noOfOptions} uint)}) (define-map register {id: uint} {user: principal, bns: (string-ascii 256), vote: (list &{noOfOptions} (string-ascii 36)), volume: (list &{noOfOptions} uint)}) (define-data-var total uint u0) + (define-data-var total-with-voting-power uint u0) (define-data-var options (list &{noOfOptions} (string-ascii 36)) (list)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -216,6 +217,9 @@ function getRawContract() { (map-set users {id: tx-sender} {count: u1, vote: vote, volume: volume}) (map-set register {id: next-total} {user: tx-sender, bns: bns, vote: vote, volume: volume}) + ;; Increase the total with voting power + (var-set total-with-voting-power (+ (unwrap-panic (element-at volume u0)) (var-get total-with-voting-power))) + ;; Increase the total (var-set total next-total) @@ -226,7 +230,7 @@ function getRawContract() { (define-read-only (get-results) (begin - (ok {total: (var-get total),options: (var-get options), results: (map get-single-result (var-get options))}) + (ok {total: (var-get total), total-with-voting-power: (var-get total-with-voting-power), options: (var-get options), results: (map get-single-result (var-get options))}) ) )