Skip to content

Commit 984cfa8

Browse files
authored
Merge pull request #124 from cosmology-tech/telescope-with-contracts
Add react query sample code to Telescope with contracts example
2 parents 2391dd0 + d43379f commit 984cfa8

File tree

12 files changed

+1091
-82
lines changed

12 files changed

+1091
-82
lines changed

examples/telescope-with-contracts/components/sell-nfts.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export const SellNfts = () => {
6060
const transferModalControl = useDisclosure();
6161
const burnModalControl = useDisclosure();
6262
const updatePriceModalControl = useDisclosure();
63+
const updatePriceModalRQControl = useDisclosure();
6364
const removeListingModalControl = useDisclosure();
6465

6566
useEffect(() => {
@@ -252,6 +253,7 @@ export const SellNfts = () => {
252253
transferNft: transferModalControl.onOpen,
253254
removeListing: removeListingModalControl.onOpen,
254255
updatePrice: updatePriceModalControl.onOpen,
256+
updatePriceRQ: updatePriceModalRQControl.onOpen,
255257
}}
256258
/>
257259
)}
@@ -289,6 +291,18 @@ export const SellNfts = () => {
289291
update={update}
290292
token={selectedToken}
291293
price={price}
294+
isRQ={false}
295+
/>
296+
)}
297+
298+
{selectedToken && selectedCollection && price && (
299+
<UpdatePriceModal
300+
modalControl={updatePriceModalRQControl}
301+
collection={selectedCollection}
302+
update={update}
303+
token={selectedToken}
304+
price={price}
305+
isRQ={true}
292306
/>
293307
)}
294308

examples/telescope-with-contracts/components/ui/modal/nft-detail-modal.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const NftDetailModal = ({
5252
burn: () => void;
5353
transferNft: () => void;
5454
updatePrice: () => void;
55+
updatePriceRQ: () => void;
5556
removeListing: () => void;
5657
};
5758
}) => {
@@ -162,7 +163,6 @@ export const NftDetailModal = ({
162163
/>
163164
)}
164165
<SplitText left="Owned by" right={ownerName} mb="18px" />
165-
166166
{token.forSale && token.saleType && (
167167
<SplitText
168168
left={priceText[token.saleType]}
@@ -175,7 +175,6 @@ export const NftDetailModal = ({
175175
mb="24px"
176176
/>
177177
)}
178-
179178
{token.forSale ? (
180179
<Flex gap="16px" mb="16px">
181180
<NormalButton
@@ -203,7 +202,17 @@ export const NftDetailModal = ({
203202
mb="16px"
204203
/>
205204
)}
206-
205+
{token.forSale && (
206+
<Flex gap="16px" mb="16px">
207+
<NormalButton
208+
onClick={openModals.updatePriceRQ}
209+
size={{ h: '36px', w: '100%' }}
210+
leftIcon={<Icon as={AiOutlineTag} boxSize={4} />}
211+
text="Update Price(React Query)"
212+
type="solid"
213+
/>
214+
</Flex>
215+
)}
207216
<Flex gap="16px">
208217
<SimpleButton
209218
content="Transfer"

examples/telescope-with-contracts/components/ui/modal/update-price-modal.tsx

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
Flex,
1111
} from '@chakra-ui/react';
1212
import BigNumber from 'bignumber.js';
13-
import { useState } from 'react';
13+
import { useState, useEffect } from 'react';
1414
import { coin, exponent, marketplaceContract } from 'config';
1515
import { useColor, useTransactionToast } from 'hooks';
1616
import { isGtZero, toDisplayAmount, toRawAmount } from 'utils';
@@ -20,22 +20,32 @@ import { LargeButton } from '../nft/buttons';
2020
import { SplitText } from '../nft/nft-cards';
2121
import { Subtitle, Fees } from '../nft';
2222
import { useContracts } from '../../../src/codegen/contracts-context';
23+
import { useMarketplaceUpdateAskPriceMutation } from 'src/codegen/Marketplace.react-query';
2324

2425
export const UpdatePriceModal = ({
2526
modalControl,
2627
collection,
2728
token,
2829
price,
2930
update,
31+
isRQ,
3032
}: {
3133
token: Token;
3234
price: number;
35+
isRQ: boolean;
3336
modalControl: UseDisclosureReturn;
3437
collection: Collection;
3538
update: () => void;
3639
}) => {
3740
const [inputValue, setInputValue] = useState('');
3841
const [isLoading, setIsLoading] = useState(false);
42+
const {
43+
isSuccess: isRQSuccess,
44+
isError: isRQError,
45+
error: rqError,
46+
isLoading: isRQLoading,
47+
mutate: updateAskPrice,
48+
} = useMarketplaceUpdateAskPriceMutation();
3949

4050
const { showToast } = useTransactionToast();
4151

@@ -75,6 +85,44 @@ export const UpdatePriceModal = ({
7585
}
7686
};
7787

88+
const handleRQUpdateClick = async () => {
89+
try {
90+
console.log("react query's working.");
91+
92+
const marketplaceClient =
93+
marketplace.getSigningClient(marketplaceContract);
94+
updateAskPrice({
95+
client: marketplaceClient,
96+
msg: {
97+
collection: token.collectionAddr,
98+
price: {
99+
amount: toRawAmount(inputValue, exponent),
100+
denom: coin.base,
101+
},
102+
tokenId: parseInt(token.tokenId),
103+
},
104+
});
105+
} catch (ex) {
106+
showToast(TxResult.Failed, ex);
107+
console.error(ex);
108+
}
109+
};
110+
111+
useEffect(() => {
112+
if (isRQSuccess) {
113+
showToast(TxResult.Success);
114+
update();
115+
closeModal();
116+
}
117+
}, [isRQSuccess]);
118+
119+
useEffect(() => {
120+
if (isRQError) {
121+
showToast(TxResult.Failed, rqError);
122+
console.error(rqError);
123+
}
124+
}, [isRQError, rqError]);
125+
78126
const { textColor, bgColor } = useColor();
79127

80128
return (
@@ -140,9 +188,9 @@ export const UpdatePriceModal = ({
140188

141189
<LargeButton
142190
width="100%"
143-
btnContent="Update"
144-
handleClick={handleUpdateClick}
145-
isLoading={isLoading}
191+
btnContent={`Update${isRQ ? '(ReactQuery)' : ''}`}
192+
handleClick={isRQ ? handleRQUpdateClick : handleUpdateClick}
193+
isLoading={isLoading || isRQLoading}
146194
disabled={
147195
!inputValue ||
148196
new BigNumber(inputValue).isEqualTo(0) ||

examples/telescope-with-contracts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"react": "18.2.0",
4949
"react-dom": "18.2.0",
5050
"react-icons": "4.8.0",
51+
"react-query": "3.39.3",
5152
"stargazejs": "0.14.0"
5253
},
5354
"devDependencies": {

examples/telescope-with-contracts/pages/_app.tsx

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import { GasPrice } from '@cosmjs/stargate';
1414
import { SignerOptions } from '@cosmos-kit/core';
1515
import { defaultTheme } from 'config';
1616
import '@interchain-ui/react/styles';
17+
import { QueryClientProvider, QueryClient } from '@tanstack/react-query';
18+
19+
const queryClient = new QueryClient();
1720

1821
const client = new ApolloClient({
1922
uri: 'https://constellations-api.mainnet.stargaze-apis.com/graphql',
@@ -37,28 +40,30 @@ function CreateCosmosApp({ Component, pageProps }: AppProps) {
3740

3841
return (
3942
<ChakraProvider theme={defaultTheme}>
40-
<ChainProvider
41-
chains={chains}
42-
assetLists={assets}
43-
wallets={[...keplrWallets, ...cosmostationWallets, ...leapWallets]}
44-
walletConnectOptions={{
45-
signClient: {
46-
projectId: 'a8510432ebb71e6948cfd6cde54b70f7',
47-
relayUrl: 'wss://relay.walletconnect.org',
48-
metadata: {
49-
name: 'CosmosKit Template',
50-
description: 'CosmosKit dapp template',
51-
url: 'https://docs.cosmoskit.com/',
52-
icons: [],
43+
<QueryClientProvider client={queryClient}>
44+
<ChainProvider
45+
chains={chains}
46+
assetLists={assets}
47+
wallets={[...keplrWallets, ...cosmostationWallets, ...leapWallets]}
48+
walletConnectOptions={{
49+
signClient: {
50+
projectId: 'a8510432ebb71e6948cfd6cde54b70f7',
51+
relayUrl: 'wss://relay.walletconnect.org',
52+
metadata: {
53+
name: 'CosmosKit Template',
54+
description: 'CosmosKit dapp template',
55+
url: 'https://docs.cosmoskit.com/',
56+
icons: [],
57+
},
5358
},
54-
},
55-
}}
56-
signerOptions={signerOptions}
57-
>
58-
<ApolloProvider client={client}>
59-
<Component {...pageProps} />
60-
</ApolloProvider>
61-
</ChainProvider>
59+
}}
60+
signerOptions={signerOptions}
61+
>
62+
<ApolloProvider client={client}>
63+
<Component {...pageProps} />
64+
</ApolloProvider>
65+
</ChainProvider>
66+
</QueryClientProvider>
6267
</ChakraProvider>
6368
);
6469
}

examples/telescope-with-contracts/scripts/codegen.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ telescope({
7979
messageComposer: {
8080
enabled: true,
8181
},
82+
reactQuery: {
83+
enabled: true,
84+
version: 'v4',
85+
mutations: true,
86+
},
8287
useContractsHooks: {
8388
enabled: true
8489
}

0 commit comments

Comments
 (0)