Skip to content

Commit

Permalink
basic apollo queries
Browse files Browse the repository at this point in the history
  • Loading branch information
tenfinney committed May 19, 2022
1 parent a7a664c commit 05d27d8
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ yarn-error.log*
*.tsbuildinfo
.yarn/install-state.gz
.yarn/cache/
.vercel/
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
"window.autoDetectColorScheme": true,
"workbench.colorTheme": "Default Dark+",
"window.closeWhenEmpty": true,
"window.zoomLevel": 4
"window.zoomLevel": 6
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@
"@typescript-eslint/parser": "latest",
"eslint": "8.12.0",
"typescript": "4.6.2"
},
"dependencies": {
"@apollo/client": "^3.6.4",
"graphql": "^16.5.0"
}
}
7 changes: 6 additions & 1 deletion packages/ui/components/TokensTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from '@chakra-ui/react'
import { httpURL } from 'lib/helpers'
import { TokenState } from 'lib/types'
import Link from 'next/link'
import NextLink from 'next/link'
import Markdown from 'react-markdown'

Expand Down Expand Up @@ -133,7 +134,11 @@ const URITd:React.FC<Token> = ({ token }) => (
)

const TotalTd:React.FC<Token> = ({ token }) => (
<Td>{token.total?.toString() ?? <Spinner/>}</Td>
<Td>
<Link href={`/owners/${token.id}`}>
{token.total?.toString() ?? <Spinner/>}
</Link>
</Td>
)

const ActionsTd:React.FC<Token> = ({ token }) => (
Expand Down
1 change: 1 addition & 0 deletions packages/ui/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type CodedError = Error & { code: number }

export type TokenState = {
id: string
gating?: boolean
uri?: string
metadata?: ERC1155Metadata
total?: number
Expand Down
44 changes: 27 additions & 17 deletions packages/ui/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,37 @@ import type { AppProps } from 'next/app'
import Head from 'next/head'
import { ChakraProvider } from '@chakra-ui/react'
import { Web3ContextProvider } from 'lib/hooks'
import {
ApolloClient,
InMemoryCache,
ApolloProvider,
useQuery,
gql
} from "@apollo/client";

const App = ({ Component, pageProps }: AppProps) => {
const prod = process.env.NODE_ENV === 'production'
const client = new ApolloClient({
uri: 'https://api.thegraph.com/subgraphs/name/alberthaotan/nft-matic',
cache: new InMemoryCache(),
})

return (
<ChakraProvider>
<Head>
<link
rel="shortcut icon"
href={`${prod ? '/chievemints' : ''}/favicon.png`}
/>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
</Head>
const App = ({ Component, pageProps }: AppProps) => (
<ChakraProvider>
<Head>
<link
rel="shortcut icon"
href="favicon.png"
/>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
</Head>
<ApolloProvider {...{ client }}>
<Web3ContextProvider>
<Component {...pageProps} />
</Web3ContextProvider>
</ChakraProvider>
)
}
</ApolloProvider>
</ChakraProvider>
)

export default App
36 changes: 32 additions & 4 deletions packages/ui/pages/disburse/[nftId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const Disburse: NextPage = () => {
const [raw, setRaw] = useState('')
const [action, setAction] = useState('whitelist')
const {
ensProvider, address, roContract, connected, connect
ensProvider, address, roContract, rwContract, connected, connect, userProvider
} = useWeb3()
const [addresses, setAddresses] = useState<Array<string | ReactNode>>([])
const toast = useToast()
Expand Down Expand Up @@ -129,12 +129,23 @@ const Disburse: NextPage = () => {
},
[roContract, tokenId],
)
console.debug({rwContract, userProvider})

const submit = useCallback(async (evt) => {
evt.preventDefault()

if(!rwContract) {
toast({
title: `Contract Error!`,
description: 'Token is not Connected.',
status: 'error',
isClosable: true,
duration: 10000
})
return
}
try {
const skip = evt.target.skip.checked
// const skip = evt.target.skip.checked
const addrs = await Promise.all(
split(raw)
.map(async (name: string) => {
Expand All @@ -148,10 +159,20 @@ const Disburse: NextPage = () => {
switch(action) {
case 'mint': {
console.debug('minting', { addrs })
const tx = await rwContract?.['mint(address[],uint256,bytes)'](
addrs, tokenId, []
)
await tx.wait()
break
}
case 'whitelist': {
console.debug('whitelist', { addrs })
addrs.map(async (addr) => {
const minterRole = await roContract?.roleValueForName('Minter')
const tx = await rwContract?.['mint(address,uint256,uint256,bytes)'](
addr, tokenId, 1, []
)
})
break
}
}
Expand All @@ -164,7 +185,7 @@ const Disburse: NextPage = () => {
duration: 10000
})
}
}, [action, addresses, ensProvider])
}, [action, addresses, ensProvider, roContract, rwContract, tokenId])

if(error) {
return (
Expand Down Expand Up @@ -246,7 +267,14 @@ const Disburse: NextPage = () => {
</Checkbox>
</FormControl>
<FormControl textAlign="center">
<Button type="submit" colorScheme="green">Distribute</Button>
{console.debug({rwContract})}
{!rwContract ? (
<Button onClick={connect}>
Connect
</Button>
) : (
<Button type="submit" colorScheme="green">Distribute</Button>
)}
</FormControl>
</Stack>
</Container>
Expand Down
39 changes: 39 additions & 0 deletions packages/ui/pages/owners/[nftId].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useRouter } from 'next/router'
import { gql, useQuery } from '@apollo/client'
import { ListItem, OrderedList } from '@chakra-ui/react'

const NFT_OWNERS = gql`
query NFTOwners {
nfts(where:{ contract: "0x2fd05e332fcb602772337a5684b189f26a92cfab" }) {
contract {
id
}
ownership {
id
owner
quantity
}
}
}
`
export const Owners = () => {
const { query: { nftId } } = useRouter()
const { loading, error, data } = useQuery(NFT_OWNERS)
console.debug({loading, error, data})
if (loading) return 'Loading…'
if (error) return `Error! ${error.message}`
return (
<OrderedList>
{data.nfts.map(({ ownership }) => {
console.debug({ownership})
return (
<ListItem>
{`${ownership.owner} (${ownership.quantity})`}
</ListItem>
)
})}
</OrderedList>
)
}

export default Owners
Loading

0 comments on commit 05d27d8

Please sign in to comment.