From c0efafce78f7bed87cd621a269554fca7419cd7c Mon Sep 17 00:00:00 2001
From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com>
Date: Tue, 17 Aug 2021 14:29:03 +0200
Subject: [PATCH] Add poll endTime (#15)
---
packages/core/src/index.ts | 1 +
packages/core/src/models/PollInitMsg.ts | 4 +--
packages/react-components/src/Poll.tsx | 40 +++++++++++++++++++------
packages/react-components/src/index.tsx | 24 ++++++++++++++-
4 files changed, 57 insertions(+), 12 deletions(-)
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 710b86c..aa13634 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -95,6 +95,7 @@ class WakuVoting {
if (newPollInitMessages.length > 0) {
this.timedPollInitMessages = [...newPollInitMessages, ...this.timedPollInitMessages]
}
+ this.timedPollInitMessages = this.timedPollInitMessages.filter((e) => e.endTime > Date.now())
return this.timedPollInitMessages
}
diff --git a/packages/core/src/models/PollInitMsg.ts b/packages/core/src/models/PollInitMsg.ts
index 109552f..0c20dd6 100644
--- a/packages/core/src/models/PollInitMsg.ts
+++ b/packages/core/src/models/PollInitMsg.ts
@@ -22,9 +22,9 @@ export function createSignMsgParams(message: Message) {
},
message: {
...message,
- timestamp: new Date(message.timestamp).toLocaleDateString(),
+ timestamp: new Date(message.timestamp).toLocaleString(),
pollType: message.pollType === PollType.WEIGHTED ? 'Weighted' : 'Non weighted',
- endTime: new Date(message.endTime).toLocaleDateString(),
+ endTime: new Date(message.endTime).toLocaleString(),
},
primaryType: 'Mail',
types: {
diff --git a/packages/react-components/src/Poll.tsx b/packages/react-components/src/Poll.tsx
index 71ec616..8988869 100644
--- a/packages/react-components/src/Poll.tsx
+++ b/packages/react-components/src/Poll.tsx
@@ -29,7 +29,10 @@ export function Poll({ poll, wakuVoting, signer }: PollProps) {
{poll.poll.question}
- {poll.poll.pollType === PollType.WEIGHTED ? 'WEIGHTED' : 'NON WEIGHTED'}
+
+ {poll.poll.pollType === PollType.WEIGHTED ? 'WEIGHTED' : 'NON WEIGHTED'}
+ {new Date(poll.poll.endTime).toLocaleString()}
+
{!userInVoters && (
@@ -70,7 +73,7 @@ export function Poll({ poll, wakuVoting, signer }: PollProps) {
)}
{!userInVoters && (
-
+
)}
)
}
+const DateWrapper = styled.div`
+ font-size: 14px;
+ text-align: right;
+`
+
+const TitleInfo = styled.div`
+ display: flex;
+ flex-direction: column;
+ margin-right: 10px;
+ margin-left: auto;
+ margin-top: auto;
+`
+
+const VoteButton = styled.button`
+ width: 100px;
+ border-radius: 5px;
+ font-size: 20px;
+ font-weight: bold;
+ font-family: 'Times New Roman', Times, serif;
+`
+
const VoteCount = styled.div`
margin-left: auto;
margin-right: 5px;
@@ -108,7 +132,6 @@ const PollWrapper = styled.div`
const PollTitle = styled.div`
display: flex;
padding: 10px;
- height: 20px;
border: 1px solid black;
border-radius: 5px;
`
@@ -117,16 +140,15 @@ const PollQuestion = styled.div`
display: block;
width: 200px;
margin-left: 10px;
- margin-top: auto;
- overflow: hidden;
+ margin-top: 0px;
+ overflow: auto;
`
const PollTypeWrapper = styled.div`
width: 150px;
- margin-right: 10px;
- margin-left: auto;
- margin-top: auto;
+ text-align: right;
color: green;
+ font-size: 14px;
font-weight: bold;
`
diff --git a/packages/react-components/src/index.tsx b/packages/react-components/src/index.tsx
index d06025e..b541663 100644
--- a/packages/react-components/src/index.tsx
+++ b/packages/react-components/src/index.tsx
@@ -5,6 +5,13 @@ import { PollList } from './PollList'
import styled from 'styled-components'
import { PollType } from '@status-waku-voting/core/dist/esm/src/types/PollType'
+function getLocaleIsoTime(dateTime: Date) {
+ const MS_PER_MINUTE = 60000
+ const milliseconds = dateTime.getTime() - dateTime.getTimezoneOffset() * MS_PER_MINUTE
+ const newDate = new Date(milliseconds)
+ return newDate.toISOString().slice(0, -8)
+}
+
const provider = new providers.Web3Provider((window as any).ethereum)
type ExampleProps = {
@@ -20,6 +27,7 @@ function Example({ appName }: ExampleProps) {
const [question, setQuestion] = useState('')
const [showNewPollBox, setShowNewPollBox] = useState(false)
const [selectedType, setSelectedType] = useState(PollType.WEIGHTED)
+ const [endTimePicker, setEndTimePicker] = useState(new Date(new Date().getTime() + 10000000))
useEffect(() => {
;(window as any).ethereum.on('accountsChanged', async () => {
@@ -29,6 +37,7 @@ function Example({ appName }: ExampleProps) {
WakuVoting.create(appName, '0x01').then((e) => setWakuVoting(e))
provider.send('eth_requestAccounts', [])
}, [])
+
return (
showNewPollBox && setShowNewPollBox(false)}>
{showNewPollBox && (
@@ -39,6 +48,12 @@ function Example({ appName }: ExampleProps) {
setShowNewPollBox(false)}>X
setQuestion(e.target.value)} />
+ Poll end time
+ setEndTimePicker(new Date(e.target.value))}
+ />
Answers
{answers.map((answer, idx) => (
@@ -74,7 +89,14 @@ function Example({ appName }: ExampleProps) {
{
- await wakuVoting?.createTimedPoll(signer, question, answers, selectedType)
+ await wakuVoting?.createTimedPoll(
+ signer,
+ question,
+ answers,
+ selectedType,
+ undefined,
+ endTimePicker.getTime()
+ )
setAnswers([])
}}
>