Refactor polling hooks (#92)

This commit is contained in:
Szymon Szlachtowicz 2021-09-23 14:23:38 +02:00 committed by GitHub
parent 58300680e8
commit c0d6e104ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 13 deletions

View File

@ -4,7 +4,7 @@ import React, { useEffect, useState } from 'react'
import { Poll } from './Poll'
import styled from 'styled-components'
import { Theme } from '@status-waku-voting/react-components'
import { usePollList } from '@status-waku-voting/polling-hooks'
type PollListProps = {
theme: Theme
wakuPolling: WakuPolling | undefined
@ -12,18 +12,9 @@ type PollListProps = {
}
export function PollList({ wakuPolling, account, theme }: PollListProps) {
const [polls, setPolls] = useState<DetailedTimedPoll[]>([])
const [dividedPolls, setDividedPolls] = useState<DetailedTimedPoll[][]>([[], [], []])
useEffect(() => {
const interval = setInterval(async () => {
if (wakuPolling) {
const DetailedTimedPolls = await wakuPolling.getDetailedTimedPolls()
setPolls(DetailedTimedPolls)
}
}, 1000)
return () => clearInterval(interval)
}, [wakuPolling])
const polls = usePollList(wakuPolling)
const [dividedPolls, setDividedPolls] = useState<DetailedTimedPoll[][]>([[], [], []])
useEffect(() => {
let arrayNo = 0
const newDividedPolls: DetailedTimedPoll[][] = [[], [], []]

View File

@ -0,0 +1,17 @@
import { WakuPolling } from '@status-waku-voting/core'
import { DetailedTimedPoll } from '@status-waku-voting/core/dist/esm/src/models/DetailedTimedPoll'
import React, { useEffect, useState } from 'react'
export function usePollList(wakuPolling: WakuPolling | undefined) {
const [polls, setPolls] = useState<DetailedTimedPoll[]>([])
useEffect(() => {
const interval = setInterval(async () => {
if (wakuPolling) {
const DetailedTimedPolls = await wakuPolling.getDetailedTimedPolls()
setPolls(DetailedTimedPolls)
}
}, 1000)
return () => clearInterval(interval)
}, [wakuPolling])
return polls
}

View File

@ -1,3 +1,4 @@
import { usePollList } from './hooks/usePollList'
import { useWakuPolling } from './hooks/useWakuPolling'
export { useWakuPolling }
export { useWakuPolling, usePollList }