Add poll endTime (#15)

This commit is contained in:
Szymon Szlachtowicz 2021-08-17 14:29:03 +02:00 committed by GitHub
parent 25b6e744ca
commit c0efafce78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 12 deletions

View File

@ -95,6 +95,7 @@ class WakuVoting {
if (newPollInitMessages.length > 0) { if (newPollInitMessages.length > 0) {
this.timedPollInitMessages = [...newPollInitMessages, ...this.timedPollInitMessages] this.timedPollInitMessages = [...newPollInitMessages, ...this.timedPollInitMessages]
} }
this.timedPollInitMessages = this.timedPollInitMessages.filter((e) => e.endTime > Date.now())
return this.timedPollInitMessages return this.timedPollInitMessages
} }

View File

@ -22,9 +22,9 @@ export function createSignMsgParams(message: Message) {
}, },
message: { message: {
...message, ...message,
timestamp: new Date(message.timestamp).toLocaleDateString(), timestamp: new Date(message.timestamp).toLocaleString(),
pollType: message.pollType === PollType.WEIGHTED ? 'Weighted' : 'Non weighted', pollType: message.pollType === PollType.WEIGHTED ? 'Weighted' : 'Non weighted',
endTime: new Date(message.endTime).toLocaleDateString(), endTime: new Date(message.endTime).toLocaleString(),
}, },
primaryType: 'Mail', primaryType: 'Mail',
types: { types: {

View File

@ -29,7 +29,10 @@ export function Poll({ poll, wakuVoting, signer }: PollProps) {
<PollWrapper> <PollWrapper>
<PollTitle> <PollTitle>
<PollQuestion>{poll.poll.question}</PollQuestion> <PollQuestion>{poll.poll.question}</PollQuestion>
<TitleInfo>
<PollTypeWrapper>{poll.poll.pollType === PollType.WEIGHTED ? 'WEIGHTED' : 'NON WEIGHTED'}</PollTypeWrapper> <PollTypeWrapper>{poll.poll.pollType === PollType.WEIGHTED ? 'WEIGHTED' : 'NON WEIGHTED'}</PollTypeWrapper>
<DateWrapper>{new Date(poll.poll.endTime).toLocaleString()}</DateWrapper>
</TitleInfo>
</PollTitle> </PollTitle>
<PollAnswersWrapper> <PollAnswersWrapper>
{!userInVoters && ( {!userInVoters && (
@ -70,7 +73,7 @@ export function Poll({ poll, wakuVoting, signer }: PollProps) {
)} )}
</PollAnswersWrapper> </PollAnswersWrapper>
{!userInVoters && ( {!userInVoters && (
<button <VoteButton
onClick={() => { onClick={() => {
if (wakuVoting) { if (wakuVoting) {
wakuVoting.sendTimedPollVote( wakuVoting.sendTimedPollVote(
@ -84,12 +87,33 @@ export function Poll({ poll, wakuVoting, signer }: PollProps) {
> >
{' '} {' '}
Vote Vote
</button> </VoteButton>
)} )}
</PollWrapper> </PollWrapper>
) )
} }
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` const VoteCount = styled.div`
margin-left: auto; margin-left: auto;
margin-right: 5px; margin-right: 5px;
@ -108,7 +132,6 @@ const PollWrapper = styled.div`
const PollTitle = styled.div` const PollTitle = styled.div`
display: flex; display: flex;
padding: 10px; padding: 10px;
height: 20px;
border: 1px solid black; border: 1px solid black;
border-radius: 5px; border-radius: 5px;
` `
@ -117,16 +140,15 @@ const PollQuestion = styled.div`
display: block; display: block;
width: 200px; width: 200px;
margin-left: 10px; margin-left: 10px;
margin-top: auto; margin-top: 0px;
overflow: hidden; overflow: auto;
` `
const PollTypeWrapper = styled.div` const PollTypeWrapper = styled.div`
width: 150px; width: 150px;
margin-right: 10px; text-align: right;
margin-left: auto;
margin-top: auto;
color: green; color: green;
font-size: 14px;
font-weight: bold; font-weight: bold;
` `

View File

@ -5,6 +5,13 @@ import { PollList } from './PollList'
import styled from 'styled-components' import styled from 'styled-components'
import { PollType } from '@status-waku-voting/core/dist/esm/src/types/PollType' 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) const provider = new providers.Web3Provider((window as any).ethereum)
type ExampleProps = { type ExampleProps = {
@ -20,6 +27,7 @@ function Example({ appName }: ExampleProps) {
const [question, setQuestion] = useState('') const [question, setQuestion] = useState('')
const [showNewPollBox, setShowNewPollBox] = useState(false) const [showNewPollBox, setShowNewPollBox] = useState(false)
const [selectedType, setSelectedType] = useState(PollType.WEIGHTED) const [selectedType, setSelectedType] = useState(PollType.WEIGHTED)
const [endTimePicker, setEndTimePicker] = useState(new Date(new Date().getTime() + 10000000))
useEffect(() => { useEffect(() => {
;(window as any).ethereum.on('accountsChanged', async () => { ;(window as any).ethereum.on('accountsChanged', async () => {
@ -29,6 +37,7 @@ function Example({ appName }: ExampleProps) {
WakuVoting.create(appName, '0x01').then((e) => setWakuVoting(e)) WakuVoting.create(appName, '0x01').then((e) => setWakuVoting(e))
provider.send('eth_requestAccounts', []) provider.send('eth_requestAccounts', [])
}, []) }, [])
return ( return (
<Wrapper onClick={() => showNewPollBox && setShowNewPollBox(false)}> <Wrapper onClick={() => showNewPollBox && setShowNewPollBox(false)}>
{showNewPollBox && ( {showNewPollBox && (
@ -39,6 +48,12 @@ function Example({ appName }: ExampleProps) {
<CloseNewPollBoxButton onClick={() => setShowNewPollBox(false)}>X</CloseNewPollBoxButton> <CloseNewPollBoxButton onClick={() => setShowNewPollBox(false)}>X</CloseNewPollBoxButton>
</NewPollBoxTitle> </NewPollBoxTitle>
<input value={question} onChange={(e) => setQuestion(e.target.value)} /> <input value={question} onChange={(e) => setQuestion(e.target.value)} />
Poll end time
<input
type="datetime-local"
value={getLocaleIsoTime(endTimePicker)}
onChange={(e) => setEndTimePicker(new Date(e.target.value))}
/>
Answers Answers
<button onClick={() => setAnswers((answers) => [...answers, ''])}>Add answer</button> <button onClick={() => setAnswers((answers) => [...answers, ''])}>Add answer</button>
{answers.map((answer, idx) => ( {answers.map((answer, idx) => (
@ -74,7 +89,14 @@ function Example({ appName }: ExampleProps) {
</div> </div>
<SendButton <SendButton
onClick={async () => { onClick={async () => {
await wakuVoting?.createTimedPoll(signer, question, answers, selectedType) await wakuVoting?.createTimedPoll(
signer,
question,
answers,
selectedType,
undefined,
endTimePicker.getTime()
)
setAnswers([]) setAnswers([])
}} }}
> >