From c0d6e104eef15d9b68d70ac60466f76ad7460a17 Mon Sep 17 00:00:00 2001 From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com> Date: Thu, 23 Sep 2021 14:23:38 +0200 Subject: [PATCH] Refactor polling hooks (#92) --- .../src/components/PollList.tsx | 15 +++------------ packages/polling-hooks/src/hooks/usePollList.ts | 17 +++++++++++++++++ packages/polling-hooks/src/index.ts | 3 ++- 3 files changed, 22 insertions(+), 13 deletions(-) create mode 100644 packages/polling-hooks/src/hooks/usePollList.ts 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 }