From ce48e3955de12dee5f4182f6aa341a553f484d5a Mon Sep 17 00:00:00 2001 From: Lisandro Corbalan Date: Mon, 30 Mar 2020 14:20:29 -0300 Subject: [PATCH] - Polls List - Fix infinite loop getting the data - Polls List - Improve cache --- src/containers/Polls/index.tsx | 23 ++++++++++++++--------- src/containers/Polls/queries.tsx | 2 +- src/utils/makerdao.ts | 5 ++++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/containers/Polls/index.tsx b/src/containers/Polls/index.tsx index c405f78..df15cd7 100644 --- a/src/containers/Polls/index.tsx +++ b/src/containers/Polls/index.tsx @@ -20,8 +20,8 @@ const getHomeVariables = data => { const ErrorEl = () =>
ERROR: There was an error trying to fetch the data.
function PollsInfo(props) { - const [resultVariables, setResultVariables] = useState(getHomeVariables({ governanceInfo: {} })) - const cachedData = lscache.get('polls') || [] + const [cachedData, setCachedData] = useState(lscache.get('polls') || []) + const [dataFetched, setDataFetched] = useState(false) const [data, setData] = useState(cachedData) const [pollsBalances, setBalances] = useState({}) @@ -30,7 +30,11 @@ function PollsInfo(props) { const initialSort = React.useMemo(() => [{ id: 'date', desc: true }], []) const { data: gData, ...gResult } = useQuery(GOVERNANCE_INFO_QUERY) - const pollsData = useQuery(POLLS_FIRST_QUERY, { variables: resultVariables }) + + const pollsData = useQuery(POLLS_FIRST_QUERY, { + variables: gData && getHomeVariables(gData), + skip: !gData || cachedData.length > 0, + }) const getPoll = row => { if (row.id) props.history.push(`/poll/${row.id}`) } @@ -71,18 +75,19 @@ function PollsInfo(props) { useEffect(() => { lscache.set('polls', data, DEFAULT_CACHE_TTL) + setCachedData(data) }, [data]) useEffect(() => { - if (gData) setResultVariables(getHomeVariables(gData)) - }, [gData]) - - useEffect(() => { - if (data.length > 0 && mkrSupply) { + if (data.length > 0 && mkrSupply && !dataFetched) { + setDataFetched(true) getPollsData(data).then(result => { const polls = result.filter(Boolean) Promise.all( - polls.map(poll => { + polls.map((poll: any) => { + if (poll.plurality && poll.participation) { + return Promise.resolve(poll) + } return getPollData(poll, pollsBalances).then(data => { return { ...poll, plurality: setPlurality(data), participation: getParticipation(data, mkrSupply) } }) diff --git a/src/containers/Polls/queries.tsx b/src/containers/Polls/queries.tsx index a55802d..731b848 100644 --- a/src/containers/Polls/queries.tsx +++ b/src/containers/Polls/queries.tsx @@ -18,7 +18,7 @@ const pollsDetailFragment = gql` ` const getPollsData = (pageIndex, pageSize, offset, ordering) => { return ` - polls_${pageIndex}: polls(first: ${pageSize}, skip: ${offset}, ${ordering}) { + polls_${pageIndex}: polls(first: ${pageSize}, skip: ${offset}, ${ordering}, where: {id_not_in: [9,8,11,6]}) { ...pollsDetailTotal } ` diff --git a/src/utils/makerdao.ts b/src/utils/makerdao.ts index 6049621..2ef130d 100644 --- a/src/utils/makerdao.ts +++ b/src/utils/makerdao.ts @@ -168,12 +168,15 @@ export function getPollsData(polls) { return Promise.all( polls.map(async poll => { try { + if (poll.fetched) { + return poll + } // Poll black list if (!['https://url.com'].includes(poll.url)) { const pollDocument = await fetchPollFromUrl(poll.url) if (pollDocument) { const documentData = await formatYamlToJson(pollDocument) - const pollData = { ...poll, ...documentData } + const pollData = { ...poll, ...documentData, fetched: true } pollData.active = isPollActive(pollData.startDate, pollData.endDate) pollData.source = POLLING_EMITTER