Skip to content

Commit

Permalink
feat: add useGraphQLClient and useGroupHistoricalProposals
Browse files Browse the repository at this point in the history
  • Loading branch information
wgwz committed Jul 21, 2023
1 parent ec9df89 commit fcd389d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
7 changes: 0 additions & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@ import { Wallet } from 'store/wallet.store'
import { EnableKeplr } from 'pages/enable-keplr-page'
import { InstallKeplr } from 'pages/install-keplr-page'
import { Loading } from '@/molecules/loading'
import request from 'graphql-request'
import { useQuery } from '@tanstack/react-query'
import { AllProposalsDocument } from 'gql/graphql'

export default function App() {
useKeplr()
const snap = useSnapshot(Wallet)
const { data } = useQuery(['proposals'], async () =>
request('http://localhost:5000/indexer/graphql', AllProposalsDocument),
)
console.log(data?.allProposals?.nodes)

switch (snap.keplrStatus) {
case 'ready':
Expand Down
10 changes: 10 additions & 0 deletions src/graphqlRequestContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createContext, useContext } from 'react'
import { GraphQLClient } from 'graphql-request'

const client = new GraphQLClient('http://localhost:5000/indexer/graphql')

const GraphqlRequestContext = createContext(client)

export function useGraphQLClient() {
return useContext(GraphqlRequestContext)
}
23 changes: 23 additions & 0 deletions src/hooks/use-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
fetchVotesByProposal,
} from 'api/proposal.actions'
import { fetchValidators } from 'api/staking.actions'
import { ProposalsByGroupPolicyAddressDocument } from 'gql/graphql'
import { useGraphQLClient } from 'graphqlRequestContext'
import { Chain } from 'store/chain.store'
import { Wallet } from 'store/wallet.store'

Expand Down Expand Up @@ -90,6 +92,27 @@ export function useGroupProposals(groupId?: string) {
})
}

export function useGroupHistoricalProposals(groupId?: string) {
const { data: policies, isLoading } = useGroupPolicies(groupId)
const policyIds = policies?.map((p) => p.address) || []
const client = useGraphQLClient()
return useQuery({
queryKey: ['historicalProposals', groupId],
queryFn: async () => {
const proposals = await Promise.all(
policyIds.map(async (address) => {
const res = await client.request(ProposalsByGroupPolicyAddressDocument, {
groupPolicyAddress: address,
})
return res.allProposals?.nodes
}),
)
return proposals.flat()
},
enabled: !isLoading,
})
}

export function useBalances(address?: string) {
return useQuery({
queryKey: ['balances', address],
Expand Down
6 changes: 6 additions & 0 deletions src/pages/group-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useDerivedProposals } from 'hooks/use-derived-proposals'
import {
useBalances,
useGroup,
useGroupHistoricalProposals,
useGroupPolicies,
useGroupProposals,
} from 'hooks/use-query'
Expand All @@ -22,6 +23,8 @@ export default function GroupPage() {
const { data: group, isLoading: isLoadingGroup } = useGroup(groupId)
const { data: policies, isLoading: isLoadingPolicies } = useGroupPolicies(groupId)
const { data: proposals, isLoading: isLoadingProposals } = useGroupProposals(groupId)
const { data: historicalProposals, isLoading: isLoadingHistoricalProposals } =
useGroupHistoricalProposals(groupId)
const { toastSuccess, toastErr } = useTxToasts()

const groupPolicy = policies?.[0]
Expand All @@ -37,12 +40,15 @@ export default function GroupPage() {
isLoadingGroup ||
isLoadingPolicies ||
isLoadingProposals ||
isLoadingHistoricalProposals ||
isInitialLoadingBalances ||
isRefetchingBalances
) {
return <Loading />
}

console.log({ proposals, historicalProposals })

if (!group) {
logError('Group not found')
redirect('/')
Expand Down

0 comments on commit fcd389d

Please sign in to comment.