diff --git a/packages/polling-components/src/components/PollList.tsx b/packages/polling-components/src/components/PollList.tsx index d3bb715..27d45b8 100644 --- a/packages/polling-components/src/components/PollList.tsx +++ b/packages/polling-components/src/components/PollList.tsx @@ -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([]) - const [dividedPolls, setDividedPolls] = useState([[], [], []]) - 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([[], [], []]) useEffect(() => { let arrayNo = 0 const newDividedPolls: DetailedTimedPoll[][] = [[], [], []] diff --git a/packages/polling-hooks/src/hooks/usePollList.ts b/packages/polling-hooks/src/hooks/usePollList.ts new file mode 100644 index 0000000..848ff08 --- /dev/null +++ b/packages/polling-hooks/src/hooks/usePollList.ts @@ -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([]) + useEffect(() => { + const interval = setInterval(async () => { + if (wakuPolling) { + const DetailedTimedPolls = await wakuPolling.getDetailedTimedPolls() + setPolls(DetailedTimedPolls) + } + }, 1000) + return () => clearInterval(interval) + }, [wakuPolling]) + return polls +} diff --git a/packages/polling-hooks/src/index.ts b/packages/polling-hooks/src/index.ts index 891f5f1..b540e0a 100644 --- a/packages/polling-hooks/src/index.ts +++ b/packages/polling-hooks/src/index.ts @@ -1,3 +1,4 @@ +import { usePollList } from './hooks/usePollList' import { useWakuPolling } from './hooks/useWakuPolling' -export { useWakuPolling } +export { useWakuPolling, usePollList }