From 81f13598d76f8cb78f880e7ab3b553309c0f6f7e Mon Sep 17 00:00:00 2001
From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com>
Date: Tue, 29 Jun 2021 14:57:23 +0200
Subject: [PATCH] Add useCommunityDetails (#73)
---
.../DApp/src/components/card/ProposeModal.tsx | 56 ++++++++-----------
.../DApp/src/components/votes/VotesInfo.tsx | 2 +-
.../DApp/src/hooks/useCommunityDetails.ts | 25 +++++++++
3 files changed, 50 insertions(+), 33 deletions(-)
create mode 100644 packages/DApp/src/hooks/useCommunityDetails.ts
diff --git a/packages/DApp/src/components/card/ProposeModal.tsx b/packages/DApp/src/components/card/ProposeModal.tsx
index 5df8e26..05be869 100644
--- a/packages/DApp/src/components/card/ProposeModal.tsx
+++ b/packages/DApp/src/components/card/ProposeModal.tsx
@@ -1,18 +1,36 @@
-import React, { useEffect, useState, useRef } from 'react'
+import React, { useState } from 'react'
import styled from 'styled-components'
-import { getCommunityDetails } from '../../helpers/apiMock'
import { ButtonPrimary } from '../Button'
import { CardCommunity } from '../Card'
import { Input } from '../Input'
import { VotePropose } from '../votes/VotePropose'
import { Warning } from '../votes/VoteWarning'
import { ConfirmBtn } from './VoteConfirmModal'
-
import { useContractFunction } from '@usedapp/core'
-
import { useContracts } from '../../hooks/useContracts'
import { CommunityDetail } from '../../models/community'
import { CommunitySkeleton } from '../skeleton/CommunitySkeleton'
+import { useCommunityDetails } from '../../hooks/useCommunityDetails'
+
+interface PublicKeyInputProps {
+ publicKey: string
+ setPublicKey: (val: string) => void
+}
+
+function PublicKeyInput({ publicKey, setPublicKey }: PublicKeyInputProps) {
+ return (
+
+ Community public key
+ {
+ setPublicKey(e.currentTarget.value)
+ }}
+ >
+
+ )
+}
interface ProposeModalProps {
availableAmount: number
@@ -29,39 +47,14 @@ export function ProposeModal({
}: ProposeModalProps) {
const [proposingAmount, setProposingAmount] = useState(0)
const [publicKey, setPublicKey] = useState('')
- const [loading, setLoading] = useState(false)
const disabled = proposingAmount === 0
- const loadingIdx = useRef(0)
+ const loading = useCommunityDetails(publicKey, setCommunityFound)
const { votingContract } = useContracts()
const { send } = useContractFunction(votingContract, 'initializeVotingRoom')
- useEffect(() => {
- const getDetails = async (key: string) => {
- const idx = ++loadingIdx.current
- setLoading(true)
- setCommunityFound(await getCommunityDetails(key))
- if (idx === loadingIdx.current) {
- setLoading(false)
- }
- }
- setCommunityFound(undefined)
- getDetails(publicKey)
- }, [publicKey])
-
- useEffect(() => setCommunityFound(undefined), [])
-
return (
-
- Community public key
- {
- setPublicKey(e.currentTarget.value)
- }}
- >
-
+
{publicKey && communityFound && (
@@ -111,7 +104,6 @@ export function ProposeModal({
{
setShowConfirmModal(false)
- setPublicKey('')
}}
>
OK, let’s move on! 🤙
diff --git a/packages/DApp/src/components/votes/VotesInfo.tsx b/packages/DApp/src/components/votes/VotesInfo.tsx
index 227b430..0585a62 100644
--- a/packages/DApp/src/components/votes/VotesInfo.tsx
+++ b/packages/DApp/src/components/votes/VotesInfo.tsx
@@ -30,7 +30,7 @@ export function VotesInfo() {
setShowConfirmModal={setNext}
setCommunityFound={setCommunityFound}
communityFound={communityFound}
- />{' '}
+ />
)}
{showConfirmModal && communityFound && (
diff --git a/packages/DApp/src/hooks/useCommunityDetails.ts b/packages/DApp/src/hooks/useCommunityDetails.ts
new file mode 100644
index 0000000..a7a3542
--- /dev/null
+++ b/packages/DApp/src/hooks/useCommunityDetails.ts
@@ -0,0 +1,25 @@
+import { useEffect, useRef, useState } from 'react'
+import { getCommunityDetails } from '../helpers/apiMock'
+import { CommunityDetail } from '../models/community'
+
+export function useCommunityDetails(publicKey: string, setCommunityDetail: (val: CommunityDetail | undefined) => void) {
+ const [loading, setLoading] = useState(false)
+ const loadingIdx = useRef(0)
+
+ useEffect(() => {
+ const getDetails = async (key: string) => {
+ const idx = ++loadingIdx.current
+ setLoading(true)
+ setCommunityDetail(await getCommunityDetails(key))
+ if (idx === loadingIdx.current) {
+ setLoading(false)
+ }
+ }
+ setCommunityDetail(undefined)
+ getDetails(publicKey)
+ }, [publicKey])
+
+ useEffect(() => setCommunityDetail(undefined), [])
+
+ return loading
+}