|
| 1 | +/* |
| 2 | + This is an SPA that displays information about NFTs on the BCH blockchain. |
| 3 | +*/ |
| 4 | + |
| 5 | +// Global npm libraries |
| 6 | +import React from 'react' |
| 7 | +import { Container, Row, Col } from 'react-bootstrap' |
| 8 | +import { useQueryParam, StringParam } from 'use-query-params' |
| 9 | + |
| 10 | +// Local libraries |
| 11 | +import './App.css' |
| 12 | +import LoadScripts from './components/load-scripts' |
| 13 | +import NFTs from './components/nfts' |
| 14 | +import WaitingModal from './components/waiting-modal' |
| 15 | +import AsyncLoad from './services/async-load' |
| 16 | +import ServerSelect from './components/servers' |
| 17 | +import Footer from './components/footer' |
| 18 | + |
| 19 | +// Token ID for Trout's NFTs |
| 20 | +const groupTokenId = '030563ddd65772d8e9b79b825529ed53c7d27037507b57c528788612b4911107' |
| 21 | + |
| 22 | +// Default restURL for a back-end server. |
| 23 | +let serverURL = 'https://free-bch.fullstack.cash' |
| 24 | + |
| 25 | +class App extends React.Component { |
| 26 | + constructor (props) { |
| 27 | + super(props) |
| 28 | + |
| 29 | + // Encasulate dependencies |
| 30 | + this.asyncLoad = new AsyncLoad() |
| 31 | + |
| 32 | + // Working array for storing modal output. |
| 33 | + this.modalBody = [] |
| 34 | + this.tokenData = {} |
| 35 | + |
| 36 | + this.state = { |
| 37 | + walletInitialized: false, |
| 38 | + wallet: false, |
| 39 | + modalBody: this.modalBody, |
| 40 | + hideSpinner: false |
| 41 | + } |
| 42 | + |
| 43 | + this.cnt = 0 |
| 44 | + } |
| 45 | + |
| 46 | + async componentDidMount () { |
| 47 | + try { |
| 48 | + this.addToModal('Loading minimal-slp-wallet') |
| 49 | + |
| 50 | + await this.asyncLoad.loadWalletLib() |
| 51 | + |
| 52 | + this.addToModal('Initializing wallet') |
| 53 | + |
| 54 | + const wallet = await this.asyncLoad.initWallet(serverURL) |
| 55 | + |
| 56 | + this.addToModal('Getting Group Token Information') |
| 57 | + |
| 58 | + // Get Group Token info |
| 59 | + const groupData = await this.asyncLoad.getGroupData(groupTokenId) |
| 60 | + // console.log(`groupData: ${JSON.stringify(groupData, null, 2)}`) |
| 61 | + |
| 62 | + this.addToModal('Getting NFT Information') |
| 63 | + |
| 64 | + /// Get NFT child info |
| 65 | + const nftData = [] |
| 66 | + for (let i = 0; i < groupData.nfts.length; i++) { |
| 67 | + const tokenData = await this.asyncLoad.getTokenData(groupData.nfts[i]) |
| 68 | + nftData.push(tokenData) |
| 69 | + } |
| 70 | + // console.log(`nft data: ${JSON.stringify(nftData, null, 2)}`) |
| 71 | + |
| 72 | + this.tokenData = { |
| 73 | + groupData, |
| 74 | + nftData |
| 75 | + } |
| 76 | + |
| 77 | + this.setState({ |
| 78 | + wallet, |
| 79 | + walletInitialized: true |
| 80 | + }) |
| 81 | + } catch (err) { |
| 82 | + this.modalBody = [ |
| 83 | + `Error: ${err.message}`, |
| 84 | + `Try selecting a different back end server using the drop-down menu at the bottom of the app.` |
| 85 | + ] |
| 86 | + |
| 87 | + this.setState({ |
| 88 | + modalBody: this.modalBody, |
| 89 | + hideSpinner: true |
| 90 | + }) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + render () { |
| 95 | + // console.log('App component rendered. this.state.wallet: ', this.state.wallet) |
| 96 | + |
| 97 | + return ( |
| 98 | + <> |
| 99 | + <GetRestUrl /> |
| 100 | + <LoadScripts /> |
| 101 | + {this.state.walletInitialized ? <InitializedView wallet={this.state.wallet} tokens={this.tokenData} /> : <UninitializedView modalBody={this.state.modalBody} hideSpinner={this.state.hideSpinner}/>} |
| 102 | + <ServerSelect /> |
| 103 | + <Footer /> |
| 104 | + </> |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + // Add a new line to the waiting modal. |
| 109 | + addToModal (inStr) { |
| 110 | + this.modalBody.push(inStr) |
| 111 | + |
| 112 | + this.setState({ |
| 113 | + modalBody: this.modalBody |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// This is rendered *before* the BCH wallet is initialized. |
| 119 | +function UninitializedView (props) { |
| 120 | + // console.log('UninitializedView props: ', props) |
| 121 | + |
| 122 | + const heading = 'Loading Blockchain Data...' |
| 123 | + |
| 124 | + return ( |
| 125 | + <Container style={{ backgroundColor: '#ddd' }}> |
| 126 | + <Row style={{ padding: '25px' }}> |
| 127 | + <Col> |
| 128 | + <h1 className='header'>NFT Explorer</h1> |
| 129 | + |
| 130 | + <WaitingModal heading={heading} body={props.modalBody} hideSpinner={props.hideSpinner} /> |
| 131 | + </Col> |
| 132 | + </Row> |
| 133 | + </Container> |
| 134 | + ) |
| 135 | +} |
| 136 | + |
| 137 | +// This is rendered *after* the BCH wallet is initialized. |
| 138 | +function InitializedView (props) { |
| 139 | + return ( |
| 140 | + <> |
| 141 | + <Container style={{ backgroundColor: '#ddd' }}> |
| 142 | + <Row style={{ padding: '25px' }}> |
| 143 | + <Col> |
| 144 | + <h1 className='header'>NFT Explorer</h1> |
| 145 | + </Col> |
| 146 | + </Row> |
| 147 | + </Container> |
| 148 | + <NFTs wallet={props.wallet} tokens={props.tokens} /> |
| 149 | + </> |
| 150 | + ) |
| 151 | +} |
| 152 | + |
| 153 | +function GetRestUrl (props) { |
| 154 | + const [restURL] = useQueryParam('restURL', StringParam) |
| 155 | + // console.log('restURL: ', restURL) |
| 156 | + |
| 157 | + serverURL = restURL |
| 158 | + |
| 159 | + return (<></>) |
| 160 | +} |
| 161 | + |
| 162 | +// function sleep (ms) { |
| 163 | +// return new Promise(resolve => setTimeout(resolve, ms)) |
| 164 | +// } |
| 165 | + |
| 166 | +export default App |
0 commit comments