mirror of
https://github.com/status-im/community-dapp.git
synced 2025-02-24 03:58:29 +00:00
Change api mock to async (#71)
This commit is contained in:
parent
ffa8d0c157
commit
2947309074
@ -1,4 +1,4 @@
|
|||||||
import React, { useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import styled from 'styled-components'
|
import styled from 'styled-components'
|
||||||
import { getCommunityDetails } from '../../helpers/apiMock'
|
import { getCommunityDetails } from '../../helpers/apiMock'
|
||||||
import { ButtonPrimary } from '../Button'
|
import { ButtonPrimary } from '../Button'
|
||||||
@ -11,22 +11,39 @@ import { ConfirmBtn } from './VoteConfirmModal'
|
|||||||
import { useContractFunction } from '@usedapp/core'
|
import { useContractFunction } from '@usedapp/core'
|
||||||
|
|
||||||
import { useContracts } from '../../hooks/useContracts'
|
import { useContracts } from '../../hooks/useContracts'
|
||||||
|
import { CommunityDetail } from '../../models/community'
|
||||||
|
|
||||||
interface ProposeModalProps {
|
interface ProposeModalProps {
|
||||||
availableAmount: number
|
availableAmount: number
|
||||||
setShowConfirmModal: (val: boolean) => void
|
setShowConfirmModal: (val: boolean) => void
|
||||||
setPublicKey: (publicKey: string) => void
|
setCommunityFound: (community: CommunityDetail | undefined) => void
|
||||||
publicKey: string
|
communityFound: CommunityDetail | undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ProposeModal({ availableAmount, setShowConfirmModal, setPublicKey, publicKey }: ProposeModalProps) {
|
export function ProposeModal({
|
||||||
|
availableAmount,
|
||||||
|
setShowConfirmModal,
|
||||||
|
setCommunityFound,
|
||||||
|
communityFound,
|
||||||
|
}: ProposeModalProps) {
|
||||||
const [proposingAmount, setProposingAmount] = useState(availableAmount)
|
const [proposingAmount, setProposingAmount] = useState(availableAmount)
|
||||||
const communityFound = getCommunityDetails(publicKey)
|
const [publicKey, setPublicKey] = useState('')
|
||||||
|
const [loading, setLoading] = useState(false)
|
||||||
const disabled = proposingAmount === 0
|
const disabled = proposingAmount === 0
|
||||||
|
|
||||||
const { votingContract } = useContracts()
|
const { votingContract } = useContracts()
|
||||||
const { send } = useContractFunction(votingContract, 'initializeVotingRoom')
|
const { send } = useContractFunction(votingContract, 'initializeVotingRoom')
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const getDetails = async (key: string) => {
|
||||||
|
setLoading(true)
|
||||||
|
setCommunityFound(await getCommunityDetails(key))
|
||||||
|
setLoading(false)
|
||||||
|
}
|
||||||
|
setCommunityFound(undefined)
|
||||||
|
getDetails(publicKey)
|
||||||
|
}, [publicKey])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CommunityProposing>
|
<CommunityProposing>
|
||||||
<CommunityKeyLabel>
|
<CommunityKeyLabel>
|
||||||
@ -69,7 +86,7 @@ export function ProposeModal({ availableAmount, setShowConfirmModal, setPublicKe
|
|||||||
<InfoText>To propose a community, it must have at least 42 members and have a ENS domain.</InfoText>
|
<InfoText>To propose a community, it must have at least 42 members and have a ENS domain.</InfoText>
|
||||||
</ProposingInfo>
|
</ProposingInfo>
|
||||||
)}
|
)}
|
||||||
|
{loading && publicKey && <div>LOADING</div>}
|
||||||
{communityFound && !communityFound.validForAddition ? (
|
{communityFound && !communityFound.validForAddition ? (
|
||||||
<ConfirmBtn
|
<ConfirmBtn
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
@ -4,13 +4,13 @@ import { useEthers } from '@usedapp/core'
|
|||||||
import { Modal } from '../Modal'
|
import { Modal } from '../Modal'
|
||||||
import { ProposeModal } from '../card/ProposeModal'
|
import { ProposeModal } from '../card/ProposeModal'
|
||||||
import { VoteConfirmModal } from '../card/VoteConfirmModal'
|
import { VoteConfirmModal } from '../card/VoteConfirmModal'
|
||||||
import { getCommunityDetails } from '../../helpers/apiMock'
|
import { CommunityDetail } from '../../models/community'
|
||||||
|
|
||||||
export function VotesInfo() {
|
export function VotesInfo() {
|
||||||
const { account } = useEthers()
|
const { account } = useEthers()
|
||||||
const [showProposeModal, setShowProposeModal] = useState(false)
|
const [showProposeModal, setShowProposeModal] = useState(false)
|
||||||
const [showConfirmModal, setShowConfirmModal] = useState(false)
|
const [showConfirmModal, setShowConfirmModal] = useState(false)
|
||||||
const [publicKey, setPublicKey] = useState('')
|
const [communityFound, setCommunityFound] = useState<undefined | CommunityDetail>(undefined)
|
||||||
|
|
||||||
const setNext = (val: boolean) => {
|
const setNext = (val: boolean) => {
|
||||||
setShowConfirmModal(val)
|
setShowConfirmModal(val)
|
||||||
@ -19,7 +19,7 @@ export function VotesInfo() {
|
|||||||
|
|
||||||
const clearKey = (val: boolean) => {
|
const clearKey = (val: boolean) => {
|
||||||
setShowConfirmModal(val)
|
setShowConfirmModal(val)
|
||||||
setPublicKey('')
|
setCommunityFound(undefined)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -33,24 +33,20 @@ export function VotesInfo() {
|
|||||||
heading="Add community to directory"
|
heading="Add community to directory"
|
||||||
setShowModal={(val: boolean) => {
|
setShowModal={(val: boolean) => {
|
||||||
setShowProposeModal(val)
|
setShowProposeModal(val)
|
||||||
setPublicKey('')
|
setCommunityFound(undefined)
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<ProposeModal
|
<ProposeModal
|
||||||
availableAmount={65245346}
|
availableAmount={65245346}
|
||||||
setShowConfirmModal={setNext}
|
setShowConfirmModal={setNext}
|
||||||
setPublicKey={setPublicKey}
|
setCommunityFound={setCommunityFound}
|
||||||
publicKey={publicKey}
|
communityFound={communityFound}
|
||||||
/>{' '}
|
/>{' '}
|
||||||
</Modal>
|
</Modal>
|
||||||
)}
|
)}
|
||||||
{showConfirmModal && (
|
{showConfirmModal && communityFound && (
|
||||||
<Modal setShowModal={clearKey}>
|
<Modal setShowModal={clearKey}>
|
||||||
<VoteConfirmModal
|
<VoteConfirmModal community={communityFound} selectedVote={{ verb: 'to add' }} setShowModal={clearKey} />
|
||||||
community={getCommunityDetails(publicKey)}
|
|
||||||
selectedVote={{ verb: 'to add' }}
|
|
||||||
setShowModal={clearKey}
|
|
||||||
/>
|
|
||||||
</Modal>
|
</Modal>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
@ -2,10 +2,15 @@ import { communities, communitiesInDirectory, communitiesUnderVote } from './api
|
|||||||
import { CommunityDetail, DirectorySortingEnum, VotingSortingEnum } from '../models/community'
|
import { CommunityDetail, DirectorySortingEnum, VotingSortingEnum } from '../models/community'
|
||||||
import { APIOptions } from '../models/api'
|
import { APIOptions } from '../models/api'
|
||||||
|
|
||||||
export function getCommunityDetails(publicKey: string) {
|
export function getCommunityDetailsSync(publicKey: string) {
|
||||||
return communities.filter((community) => community.publicKey == publicKey)[0]
|
return communities.filter((community) => community.publicKey == publicKey)[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getCommunityDetails(publicKey: string) {
|
||||||
|
await new Promise((r) => setTimeout(r, 3000))
|
||||||
|
return getCommunityDetailsSync(publicKey)
|
||||||
|
}
|
||||||
|
|
||||||
function filterCommunities(resolvedCommunities: CommunityDetail[], filterKeyword?: string) {
|
function filterCommunities(resolvedCommunities: CommunityDetail[], filterKeyword?: string) {
|
||||||
let filteredCommunities = undefined
|
let filteredCommunities = undefined
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
import { expect } from 'chai'
|
import { expect } from 'chai'
|
||||||
import { getCommunityDetails, getCommunitiesInDirectorySync } from '../src/helpers/apiMock'
|
import { getCommunityDetailsSync, getCommunitiesInDirectorySync } from '../src/helpers/apiMock'
|
||||||
import { communities } from '../src/helpers/apiMockData'
|
import { communities } from '../src/helpers/apiMockData'
|
||||||
import { DirectorySortingEnum } from '../src/models/community'
|
import { DirectorySortingEnum } from '../src/models/community'
|
||||||
|
|
||||||
describe('getCommunityDetails', () => {
|
describe('getCommunityDetails', () => {
|
||||||
it('success', async () => {
|
it('success', async () => {
|
||||||
expect(getCommunityDetails('0x344C19E3040Ec63A96b7aeB708C82a066315604B')).to.deep.eq(communities[0])
|
expect(getCommunityDetailsSync('0x344C19E3040Ec63A96b7aeB708C82a066315604B')).to.deep.eq(communities[0])
|
||||||
})
|
})
|
||||||
it('gets different community', async () => {
|
it('gets different community', async () => {
|
||||||
expect(getCommunityDetails('0xABA1EF51EF4bc360A9E8c9Ad2d787330b602EB24')).to.deep.eq(communities[1])
|
expect(getCommunityDetailsSync('0xABA1EF51EF4bc360A9E8c9Ad2d787330b602EB24')).to.deep.eq(communities[1])
|
||||||
})
|
})
|
||||||
it('empty', async () => {
|
it('empty', async () => {
|
||||||
expect(getCommunityDetails('0xabA1eF51ef4bc360a9e8C9aD2d787330B6q2eb24')).to.deep.eq(undefined)
|
expect(getCommunityDetailsSync('0xabA1eF51ef4bc360a9e8C9aD2d787330B6q2eb24')).to.deep.eq(undefined)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user